Skip to content
Permalink
Browse files

Use fewer mocks in BelongsTo tests.

We don't need this many mocks, and using concrete objects gives more
realistic test scenarios.
  • Loading branch information...
markstory committed Dec 12, 2015
1 parent d187e53 commit e96d1b0ac94fd257ba3bce022ad6eb5509445eab
Showing with 80 additions and 89 deletions.
  1. +80 −89 tests/TestCase/ORM/Association/BelongsToTest.php
@@ -38,13 +38,6 @@ class BelongsToTest extends TestCase
*/
public $fixtures = ['core.articles', 'core.authors', 'core.comments'];
/**
* Don't autoload fixtures as most tests uses mocks.
*
* @var bool
*/
public $autoFixture = false;
/**
* Set up
*
@@ -148,30 +141,33 @@ public function testCustomAlias()
*/
public function testAttachTo()
{
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$association = new BelongsTo('Companies', $config);
$field = new IdentifierExpression('Clients.company_id');
$query->expects($this->once())->method('join')->with([
$query = $this->client->query();
$association->attachTo($query);
$expected = [
'Companies__id' => 'Companies.id',
'Companies__company_name' => 'Companies.company_name'
];
$this->assertEquals($expected, $query->clause('select'));
$expected = [
'Companies' => [
'alias' => 'Companies',
'table' => 'companies',
'type' => 'LEFT',
'conditions' => new QueryExpression([
'Companies.is_active' => true,
['Companies.id' => $field]
], $this->companiesTypeMap),
'table' => 'companies',
'type' => 'LEFT'
['Companies.id' => new IdentifierExpression('Clients.company_id')]
], $this->companiesTypeMap)
]
]);
$query->expects($this->once())->method('select')->with([
'Companies__id' => 'Companies.id',
'Companies__company_name' => 'Companies.company_name'
]);
$association->attachTo($query);
];
$this->assertEquals($expected, $query->clause('join'));
}
/**
@@ -181,26 +177,80 @@ public function testAttachTo()
*/
public function testAttachToNoFields()
{
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$query = $this->client->query();
$association = new BelongsTo('Companies', $config);
$association->attachTo($query, ['includeFields' => false]);
$this->assertEmpty($query->clause('select'), 'no fields should be added.');
}
/**
* Tests that using belongsto with a table having a multi column primary
* key will work if the foreign key is passed
*
* @return void
*/
public function testAttachToMultiPrimaryKey()
{
$this->company->primaryKey(['id', 'tenant_id']);
// $query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => ['company_id', 'company_tenant_id'],
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$association = new BelongsTo('Companies', $config);
$field = new IdentifierExpression('Clients.company_id');
$query->expects($this->once())->method('join')->with([
$query = $this->client->query();
$association->attachTo($query);
$expected = [
'Companies__id' => 'Companies.id',
'Companies__company_name' => 'Companies.company_name'
];
$this->assertEquals($expected, $query->clause('select'));
$field1 = new IdentifierExpression('Clients.company_id');
$field2 = new IdentifierExpression('Clients.company_tenant_id');
$expected = [
'Companies' => [
'conditions' => new QueryExpression([
'Companies.is_active' => true,
['Companies.id' => $field]
['Companies.id' => $field1, 'Companies.tenant_id' => $field2]
], $this->companiesTypeMap),
'type' => 'LEFT',
'table' => 'companies',
'type' => 'LEFT',
'alias' => 'Companies'
]
]);
$query->expects($this->never())->method('select');
$association->attachTo($query, ['includeFields' => false]);
];
$this->assertEquals($expected, $query->clause('join'));
}
/**
* Tests that using belongsto with a table having a multi column primary
* key will work if the foreign key is passed
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot match provided foreignKey for "Companies", got "(company_id)" but expected foreign key for "(id, tenant_id)"
* @return void
*/
public function testAttachToMultiPrimaryKeyMistmatch()
{
$this->company->primaryKey(['id', 'tenant_id']);
$query = $this->client->query();
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$association = new BelongsTo('Companies', $config);
$association->attachTo($query);
}
/**
@@ -252,64 +302,6 @@ public function testSaveAssociatedOnlyEntities()
$this->assertNull($entity->author_id);
}
/**
* Tests that using belongsto with a table having a multi column primary
* key will work if the foreign key is passed
*
* @return void
*/
public function testAttachToMultiPrimaryKey()
{
$this->company->primaryKey(['id', 'tenant_id']);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => ['company_id', 'company_tenant_id'],
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$association = new BelongsTo('Companies', $config);
$field1 = new IdentifierExpression('Clients.company_id');
$field2 = new IdentifierExpression('Clients.company_tenant_id');
$query->expects($this->once())->method('join')->with([
'Companies' => [
'conditions' => new QueryExpression([
'Companies.is_active' => true,
['Companies.id' => $field1, 'Companies.tenant_id' => $field2]
], $this->companiesTypeMap),
'table' => 'companies',
'type' => 'LEFT'
]
]);
$query->expects($this->once())->method('select')->with([
'Companies__id' => 'Companies.id',
'Companies__company_name' => 'Companies.company_name'
]);
$association->attachTo($query);
}
/**
* Tests that using belongsto with a table having a multi column primary
* key will work if the foreign key is passed
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot match provided foreignKey for "Companies", got "(company_id)" but expected foreign key for "(id, tenant_id)"
* @return void
*/
public function testAttachToMultiPrimaryKeyMistmatch()
{
$this->company->primaryKey(['id', 'tenant_id']);
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
'targetTable' => $this->company,
'conditions' => ['Companies.is_active' => true]
];
$association = new BelongsTo('Companies', $config);
$association->attachTo($query);
}
/**
* Tests that property is being set using the constructor options.
*
@@ -346,7 +338,6 @@ public function testPropertyNoPlugin()
*/
public function testAttachToBeforeFind()
{
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
@@ -362,7 +353,7 @@ public function testAttachToBeforeFind()
$this->isInstanceOf('\ArrayObject'),
false
);
$association->attachTo($query);
$association->attachTo($this->client->query());
}
/**
@@ -373,7 +364,6 @@ public function testAttachToBeforeFind()
*/
public function testAttachToBeforeFindExtraOptions()
{
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
@@ -390,6 +380,7 @@ public function testAttachToBeforeFindExtraOptions()
$options,
false
);
$query = $this->client->query();
$association->attachTo($query, ['queryBuilder' => function ($q) {
return $q->applyOptions(['something' => 'more']);
}]);

0 comments on commit e96d1b0

Please sign in to comment.
You can’t perform that action at this time.