Skip to content

Commit

Permalink
Fixed remaining unit tests
Browse files Browse the repository at this point in the history
Fixed types being overwritten
  • Loading branch information
tigrang committed Mar 26, 2014
1 parent cd70659 commit 5e013b1
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 43 deletions.
23 changes: 9 additions & 14 deletions src/Database/Expression/QueryExpression.php
Expand Up @@ -64,11 +64,10 @@ class QueryExpression implements ExpressionInterface, Countable {
* @see QueryExpression::add() for more details on $conditions and $types
*/
public function __construct($conditions = [], $types = [], $conjunction = 'AND') {
$typeMap = ($types instanceof TypeMap) ? $types : new TypeMap($types);
$this->typeMap($typeMap);
$this->typeMap($types);
$this->type(strtoupper($conjunction));
if (!empty($conditions)) {
$this->add($conditions);
$this->add($conditions, $this->typeMap()->types());
}
}

Expand Down Expand Up @@ -419,6 +418,8 @@ public function iterateParts(callable $callable) {
protected function _addConditions(array $conditions, array $types) {
$operators = ['and', 'or', 'xor'];

$typeMap = $this->typeMap()->types($types);

foreach ($conditions as $k => $c) {
$numericKey = is_numeric($k);

Expand All @@ -430,14 +431,14 @@ protected function _addConditions(array $conditions, array $types) {
$this->_conditions[] = $c;
continue;
}

if ($numericKey && is_array($c) || in_array(strtolower($k), $operators)) {
$this->_conditions[] = new self($c, $this->typeMap()->types($types), $numericKey ? 'AND' : $k);
$this->_conditions[] = new self($c, $typeMap, $numericKey ? 'AND' : $k);
continue;
}

if (strtolower($k) === 'not') {
$this->_conditions[] = new UnaryExpression(new self($c, $this->typeMap()->types($types)), [], 'NOT');
$this->_conditions[] = new UnaryExpression(new self($c, $typeMap), [], 'NOT');
continue;
}

Expand All @@ -447,7 +448,7 @@ protected function _addConditions(array $conditions, array $types) {
}

if (!$numericKey) {
$this->_conditions[] = $this->_parseCondition($k, $c, $types);
$this->_conditions[] = $this->_parseCondition($k, $c);
}
}
}
Expand All @@ -462,22 +463,16 @@ protected function _addConditions(array $conditions, array $types) {
* @param string $field The value from with the actual field and operator will
* be extracted.
* @param mixed $value The value to be bound to a placeholder for the field
* @param array $types List of types where the field can be found so the value
* can be converted accordingly.
* @return string|QueryExpression
*/
protected function _parseCondition($field, $value, $types) {
protected function _parseCondition($field, $value) {
$operator = '=';
$expression = $field;
$parts = explode(' ', trim($field), 2);

if (count($parts) > 1) {
list($expression, $operator) = $parts;
}

if (!empty($types)) {
$this->typeMap()->types($types);
}

$type = $this->typeMap()->type($expression);
$multi = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/TypeMap.php
Expand Up @@ -29,7 +29,7 @@ class TypeMap {
* @param array $defaults
*/
public function __construct(array $defaults = []) {
$this->defaults($defaults);
$this->defaults($defaults);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Database/TypeMapTrait.php
@@ -1,7 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -28,20 +26,22 @@ trait TypeMapTrait {
/**
* Setter/Getter for type map
*
* @param array|TypeMap $typeMap Creates a TypeMap if array, otherwise sets the given TypeMap
* @return this|TypeMap
*/
public function typeMap($typeMap = null) {
$this->_typeMap = ($this->_typeMap) ?: new TypeMap();
if ($typeMap === null) {
$this->_typeMap = ($this->_typeMap) ?: new TypeMap();
return $this->_typeMap;
}
$this->_typeMap = $typeMap;
$this->_typeMap = is_array($typeMap) ? (new TypeMap)->types($typeMap) : $typeMap;
return $this;
}

/**
* Allows setting default types when chaining query
*
* @param array $types
* @return this|array
*/
public function defaultTypes(array $types = null) {
Expand Down
69 changes: 61 additions & 8 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Test\TestCase\ORM\Association;

use Cake\Database\TypeMap;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\Expression\TupleComparison;
Expand Down Expand Up @@ -222,11 +223,17 @@ public function testAttachTo() {
'conditions' => ['Tags.name' => 'cake']
];
$association = new BelongsToMany('Tags', $config);
$typeMap = new TypeMap([
'Tags.id' => 'integer',
'id' => 'integer',
'Tags.name' => 'string',
'name' => 'string',
]);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => new QueryExpression([
'Tags.name' => 'cake'
], ['Tags.name' => 'string']),
], $typeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -235,12 +242,19 @@ public function testAttachTo() {
$field1 = new IdentifierExpression('ArticlesTags.article_id');
$field2 = new IdentifierExpression('ArticlesTags.tag_id');

$typeMap = new TypeMap([
'ArticlesTags.article_id' => 'integer',
'article_id' => 'integer',
'ArticlesTags.tag_id' => 'integer',
'tag_id' => 'integer',
]);

$query->expects($this->at(2))->method('join')->with([
'ArticlesTags' => [
'conditions' => new QueryExpression([
['Articles.id' => $field1],
['Tags.id' => $field2]
]),
], $typeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down Expand Up @@ -269,11 +283,17 @@ public function testAttachToNoFields() {
'conditions' => ['Tags.name' => 'cake']
];
$association = new BelongsToMany('Tags', $config);
$typeMap = new TypeMap([
'Tags.id' => 'integer',
'id' => 'integer',
'Tags.name' => 'string',
'name' => 'string',
]);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => new QueryExpression([
'Tags.name' => 'cake'
], ['Tags.name' => 'string']),
], $typeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -282,12 +302,19 @@ public function testAttachToNoFields() {
$field1 = new IdentifierExpression('ArticlesTags.article_id');
$field2 = new IdentifierExpression('ArticlesTags.tag_id');

$typeMap = new TypeMap([
'ArticlesTags.article_id' => 'integer',
'article_id' => 'integer',
'ArticlesTags.tag_id' => 'integer',
'tag_id' => 'integer',
]);

$query->expects($this->at(1))->method('join')->with([
'ArticlesTags' => [
'conditions' => new QueryExpression([
['Articles.id' => $field1],
['Tags.id' => $field2]
]),
], $typeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand All @@ -310,12 +337,18 @@ public function testAttachToWithQueryBuilder() {
'conditions' => ['Tags.name' => 'cake']
];
$association = new BelongsToMany('Tags', $config);
$typeMap = new TypeMap([
'Tags.id' => 'integer',
'id' => 'integer',
'Tags.name' => 'string',
'name' => 'string',
]);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => new QueryExpression([
'a' => 1,
'Tags.name' => 'cake',
], ['Tags.name' => 'string']),
], $typeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -324,12 +357,19 @@ public function testAttachToWithQueryBuilder() {
$field1 = new IdentifierExpression('ArticlesTags.article_id');
$field2 = new IdentifierExpression('ArticlesTags.tag_id');

$typeMap = new TypeMap([
'ArticlesTags.article_id' => 'integer',
'article_id' => 'integer',
'ArticlesTags.tag_id' => 'integer',
'tag_id' => 'integer',
]);

$query->expects($this->at(2))->method('join')->with([
'ArticlesTags' => [
'conditions' => new QueryExpression([
['Articles.id' => $field1],
['Tags.id' => $field2]
]),
], $typeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down Expand Up @@ -367,11 +407,17 @@ public function testAttachToMultiPrimaryKey() {
$this->article->primaryKey(['id', 'site_id']);
$this->tag->primaryKey(['id', 'my_site_id']);
$association = new BelongsToMany('Tags', $config);
$typeMap = new TypeMap([
'Tags.id' => 'integer',
'id' => 'integer',
'Tags.name' => 'string',
'name' => 'string',
]);
$query->expects($this->at(0))->method('join')->with([
'Tags' => [
'conditions' => new QueryExpression([
'Tags.name' => 'cake'
], ['Tags.name' => 'string']),
], $typeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -382,12 +428,19 @@ public function testAttachToMultiPrimaryKey() {
$fieldC = new IdentifierExpression('ArticlesTags.tag_id');
$fieldD = new IdentifierExpression('ArticlesTags.tag_site_id');

$typeMap = new TypeMap([
'ArticlesTags.article_id' => 'integer',
'article_id' => 'integer',
'ArticlesTags.tag_id' => 'integer',
'tag_id' => 'integer',
]);

$query->expects($this->at(1))->method('join')->with([
'ArticlesTags' => [
'conditions' => new QueryExpression([
['Articles.id' => $fieldA, 'Articles.site_id' => $fieldB],
['Tags.id' => $fieldC, 'Tags.my_site_id' => $fieldD]
]),
], $typeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down

0 comments on commit 5e013b1

Please sign in to comment.