Skip to content

Commit

Permalink
Making all tests pass when using autoQuoting
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 6, 2013
1 parent a88f451 commit df6e8ec
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
3 changes: 1 addition & 2 deletions Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -16,7 +16,6 @@
*/
namespace Cake\ORM\Association;

use Cake\Database\Expression\Comparison;
use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Query;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -120,7 +119,7 @@ public abstract function eagerLoader(array $options);
* clause for getting the results on the target table.
*
* @param array $options list of options passed to attachTo method
* @return string|array
* @return array
*/
protected function _joinCondition(array $options) {
$field = sprintf(
Expand Down
17 changes: 10 additions & 7 deletions Cake/ORM/Association/HasOne.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\ORM\Association;

use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Association;
use Cake\ORM\Association\DependentDeleteTrait;
use Cake\ORM\Query;
Expand Down Expand Up @@ -67,15 +68,17 @@ public function foreignKey($key = null) {
* clause for getting the results on the target table.
*
* @param array $options list of options passed to attachTo method
* @return string|array
* @return array
*/
protected function _joinCondition(array $options) {
return sprintf('%s.%s = %s.%s',
$this->_sourceTable->alias(),
$this->_sourceTable->primaryKey(),
$this->_targetTable->alias(),
$options['foreignKey']
);
$field = sprintf('%s.%s',
$this->_sourceTable->alias(),
$this->_sourceTable->primaryKey()
);
$value = new FieldExpression(sprintf(
'%s.%s', $this->_targetTable->alias(), $options['foreignKey']
));
return [$field => $value];
}

}
7 changes: 5 additions & 2 deletions Cake/Test/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\Expression\FieldExpression;
use Cake\ORM\Association\HasOne;
use Cake\ORM\Query;
use Cake\ORM\Table;
Expand Down Expand Up @@ -84,11 +85,12 @@ public function testAttachTo() {
'conditions' => ['Profile.is_active' => true]
];
$association = new HasOne('Profile', $config);
$field = new FieldExpression('Profile.user_id');
$query->expects($this->once())->method('join')->with([
'Profile' => [
'conditions' => [
'Profile.is_active' => true,
'User.id = Profile.user_id',
['User.id' => $field],
],
'type' => 'INNER',
'table' => 'profiles'
Expand Down Expand Up @@ -150,11 +152,12 @@ public function testAttachToNoFields() {
'conditions' => ['Profile.is_active' => true]
];
$association = new HasOne('Profile', $config);
$field = new FieldExpression('Profile.user_id');
$query->expects($this->once())->method('join')->with([
'Profile' => [
'conditions' => [
'Profile.is_active' => true,
'User.id = Profile.user_id',
['User.id' => $field],
],
'type' => 'INNER',
'table' => 'profiles'
Expand Down
53 changes: 46 additions & 7 deletions Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -18,6 +18,7 @@

use Cake\Core\Configure;
use Cake\Database\ConnectionManager;
use Cake\Database\Expression\FieldExpression;
use Cake\Database\Expression\OrderByExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query;
Expand Down Expand Up @@ -113,55 +114,69 @@ public function testContainToJoinsOneLevel() {
->with(['client' => [
'table' => 'clients',
'type' => 'LEFT',
'conditions' => ['client.id = foo.client_id']
'conditions' => [
['client.id' => new FieldExpression('foo.client_id')]
]
]])
->will($this->returnValue($query));

$query->expects($this->at(1))->method('join')
->with(['order' => [
'table' => 'orders',
'type' => 'INNER',
'conditions' => ['client.id = order.client_id']
'conditions' => [
['client.id' => new FieldExpression('order.client_id')]
]
]])
->will($this->returnValue($query));

$query->expects($this->at(2))->method('join')
->with(['orderType' => [
'table' => 'order_types',
'type' => 'LEFT',
'conditions' => ['orderType.id = order.order_type_id']
'conditions' => [
['orderType.id' => new FieldExpression('order.order_type_id')]
]
]])
->will($this->returnValue($query));

$query->expects($this->at(3))->method('join')
->with(['stuff' => [
'table' => 'things',
'type' => 'INNER',
'conditions' => ['order.id = stuff.order_id']
'conditions' => [
['order.id' => new FieldExpression('stuff.order_id')]
]
]])
->will($this->returnValue($query));

$query->expects($this->at(4))->method('join')
->with(['stuffType' => [
'table' => 'stuff_types',
'type' => 'LEFT',
'conditions' => ['stuffType.id = stuff.stuff_type_id']
'conditions' => [
['stuffType.id' => new FieldExpression('stuff.stuff_type_id')]
]
]])
->will($this->returnValue($query));

$query->expects($this->at(5))->method('join')
->with(['company' => [
'table' => 'organizations',
'type' => 'LEFT',
'conditions' => ['company.id = client.organization_id']
'conditions' => [
['company.id' => new FieldExpression('client.organization_id')]
]
]])
->will($this->returnValue($query));

$query->expects($this->at(6))->method('join')
->with(['category' => [
'table' => 'categories',
'type' => 'LEFT',
'conditions' => ['category.id = company.category_id']
'conditions' => [
['category.id' => new FieldExpression('company.category_id')]
]
]])
->will($this->returnValue($query));

Expand Down Expand Up @@ -196,6 +211,7 @@ public function testContainToFieldsPredefined() {
'client__telephone' => 'client.telephone',
'order__total' => 'order.total', 'order__placed' => 'order.placed'
];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);
}

Expand All @@ -217,13 +233,15 @@ public function testContainToFieldsDefault() {
'order__id' => 'order.id', 'order__total' => 'order.total',
'order__placed' => 'order.placed'
];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);

$contains['client']['fields'] = ['name'];
$query = new Query($this->connection, $this->table);
$query->select('foo.id')->contain($contains)->sql();
$select = $query->clause('select');
$expected = ['foo__id' => 'foo.id', 'client__name' => 'client.name'];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);

$contains['client']['fields'] = [];
Expand All @@ -237,9 +255,30 @@ public function testContainToFieldsDefault() {
'client__name' => 'client.name',
'client__phone' => 'client.phone',
];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);
}

/**
* Helper function sued to quoted both keys and values in an array in case
* the test suite is running with auto quoting enabled
*
* @param array $elements
* @return array
*/
protected function _quoteArray($elements) {
if ($this->connection->driver()->autoQuoting()) {
$quoter = function($e) {
return $this->connection->driver()->quoteIdentifier($e);
};
return array_combine(
array_map($quoter, array_keys($elements)),
array_map($quoter, array_values($elements))
);
}
return $elements;
}

/**
* Tests that results are grouped correctly when using contain()
* and results are not hydrated
Expand Down

0 comments on commit df6e8ec

Please sign in to comment.