Browse Source

Fixed remaining unit tests
Fixed types being overwritten

Tigran Gabrielyan 12 years ago
parent
commit
5e013b13fa

+ 9 - 14
src/Database/Expression/QueryExpression.php

@@ -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());
 		}
 	}
 
@@ -419,6 +418,8 @@ class QueryExpression implements ExpressionInterface, Countable {
 	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);
 
@@ -430,14 +431,14 @@ class QueryExpression implements ExpressionInterface, Countable {
 				$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;
 			}
 
@@ -447,7 +448,7 @@ class QueryExpression implements ExpressionInterface, Countable {
 			}
 
 			if (!$numericKey) {
-				$this->_conditions[] = $this->_parseCondition($k, $c, $types);
+				$this->_conditions[] = $this->_parseCondition($k, $c);
 			}
 		}
 	}
@@ -462,11 +463,9 @@ class QueryExpression implements ExpressionInterface, Countable {
  * @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);
@@ -474,10 +473,6 @@ class QueryExpression implements ExpressionInterface, Countable {
 		if (count($parts) > 1) {
 			list($expression, $operator) = $parts;
 		}
-		
-		if (!empty($types)) {
-			$this->typeMap()->types($types);
-		}
 
 		$type = $this->typeMap()->type($expression);
 		$multi = false;

+ 1 - 1
src/Database/TypeMap.php

@@ -29,7 +29,7 @@ class TypeMap {
  * @param array $defaults
  */
 	public function __construct(array $defaults = []) {
-			$this->defaults($defaults);
+		$this->defaults($defaults);
 	}
 
 /**

+ 4 - 4
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)
  *
@@ -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) {

+ 61 - 8
tests/TestCase/ORM/Association/BelongsToManyTest.php

@@ -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;
@@ -222,11 +223,17 @@ class BelongsToManyTest extends TestCase {
 			'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'
 			]
@@ -235,12 +242,19 @@ class BelongsToManyTest extends TestCase {
 		$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'
 			]
@@ -269,11 +283,17 @@ class BelongsToManyTest extends TestCase {
 			'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'
 			]
@@ -282,12 +302,19 @@ class BelongsToManyTest extends TestCase {
 		$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'
 			]
@@ -310,12 +337,18 @@ class BelongsToManyTest extends TestCase {
 			'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'
 			]
@@ -324,12 +357,19 @@ class BelongsToManyTest extends TestCase {
 		$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'
 			]
@@ -367,11 +407,17 @@ class BelongsToManyTest extends TestCase {
 		$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'
 			]
@@ -382,12 +428,19 @@ class BelongsToManyTest extends TestCase {
 		$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'
 			]

+ 43 - 6
tests/TestCase/ORM/Association/BelongsToTest.php

@@ -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\ORM\Association\BelongsTo;
@@ -94,12 +95,18 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new BelongsTo('Companies', $config);
 		$field = new IdentifierExpression('Clients.company_id');
+		$typeMap = new TypeMap([
+			'Companies.id' => 'integer',
+			'id' => 'integer',
+			'Companies.company_name' => 'string',
+			'company_name' => 'string',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Companies' => [
 				'conditions' => new QueryExpression([
 					'Companies.is_active' => true,
 					['Companies.id' => $field]
-				], ['Companies.id' => 'integer']),
+				], $typeMap),
 				'table' => 'companies',
 				'type' => 'LEFT'
 			]
@@ -124,11 +131,17 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 			'conditions' => ['Companies.is_active' => true]
 		];
 		$association = new BelongsTo('Companies', $config);
+		$typeMap = new TypeMap([
+			'Companies.id' => 'integer',
+			'id' => 'integer',
+			'Companies.company_name' => 'string',
+			'company_name' => 'string',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Companies' => [
 				'conditions' => new QueryExpression([
 					'Companies.is_active' => false
-				]),
+				], $typeMap),
 				'type' => 'LEFT',
 				'table' => 'companies',
 			]
@@ -159,12 +172,18 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new BelongsTo('Companies', $config);
 		$field = new IdentifierExpression('Clients.company_id');
+		$typeMap = new TypeMap([
+			'Companies.id' => 'integer',
+			'id' => 'integer',
+			'Companies.company_name' => 'string',
+			'company_name' => 'string',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Companies' => [
 				'conditions' => new QueryExpression([
 					'Companies.is_active' => true,
 					['Companies.id' => $field]
-				], ['Companies.id' => 'integer']),
+				], $typeMap),
 				'type' => 'LEFT',
 				'table' => 'companies',
 			]
@@ -188,13 +207,19 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new BelongsTo('Companies', $config);
 		$field = new IdentifierExpression('Clients.company_id');
+		$typeMap = new TypeMap([
+			'Companies.id' => 'integer',
+			'id' => 'integer',
+			'Companies.company_name' => 'string',
+			'company_name' => 'string',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Companies' => [
 				'conditions' => new QueryExpression([
 					'a' => 1,
 					'Companies.is_active' => true,
 					['Companies.id' => $field]
-				], ['Companies.id' => 'integer']),
+				], $typeMap),
 				'type' => 'LEFT',
 				'table' => 'companies',
 			]
@@ -226,12 +251,18 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new BelongsTo('Companies', $config);
 		$field = new IdentifierExpression('Clients.company_id');
+		$typeMap = new TypeMap([
+			'Companies.id' => 'integer',
+			'id' => 'integer',
+			'Companies.company_name' => 'string',
+			'company_name' => 'string',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Companies' => [
 				'conditions' => new QueryExpression([
 					'Companies.is_active' => true,
 					['Companies.id' => $field]
-				], ['Companies.id' => 'integer']),
+				], $typeMap),
 				'table' => 'companies',
 				'type' => 'INNER'
 			]
@@ -308,12 +339,18 @@ class BelongsToTest extends \Cake\TestSuite\TestCase {
 		$association = new BelongsTo('Companies', $config);
 		$field1 = new IdentifierExpression('Clients.company_id');
 		$field2 = new IdentifierExpression('Clients.company_tenant_id');
+		$typeMap = new TypeMap([
+			'Companies.id' => 'integer',
+			'id' => 'integer',
+			'Companies.company_name' => 'string',
+			'company_name' => 'string',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Companies' => [
 				'conditions' => new QueryExpression([
 					'Companies.is_active' => true,
 					['Companies.id' => $field1, 'Companies.tenant_id' => $field2]
-				], ['Companies.id' => 'integer']),
+				], $typeMap),
 				'table' => 'companies',
 				'type' => 'LEFT'
 			]

+ 46 - 5
tests/TestCase/ORM/Association/HasManyTest.php

@@ -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;
@@ -478,12 +479,20 @@ class HasManyTest extends \Cake\TestSuite\TestCase {
 
 		$field = new IdentifierExpression('Articles.author_id');
 		$association = new HasMany('Articles', $config);
+		$typeMap = new TypeMap([
+			'Articles.id' => 'integer',
+			'id' => 'integer',
+			'Articles.title' => 'string',
+			'title' => 'string',
+			'Articles.author_id' => 'integer',
+			'author_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Articles' => [
 				'conditions' => new QueryExpression([
 					'Articles.is_active' => true,
 					['Authors.id' => $field]
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'articles'
 			]
@@ -509,11 +518,19 @@ class HasManyTest extends \Cake\TestSuite\TestCase {
 			'conditions' => ['Articles.is_active' => true]
 		];
 		$association = new HasMany('Articles', $config);
+		$typeMap = new TypeMap([
+			'Articles.id' => 'integer',
+			'id' => 'integer',
+			'Articles.title' => 'string',
+			'title' => 'string',
+			'Articles.author_id' => 'integer',
+			'author_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Articles' => [
 				'conditions' => new QueryExpression([
 					'Articles.is_active' => false
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'articles'
 			]
@@ -544,12 +561,20 @@ class HasManyTest extends \Cake\TestSuite\TestCase {
 		];
 		$field = new IdentifierExpression('Articles.author_id');
 		$association = new HasMany('Articles', $config);
+		$typeMap = new TypeMap([
+			'Articles.id' => 'integer',
+			'id' => 'integer',
+			'Articles.title' => 'string',
+			'title' => 'string',
+			'Articles.author_id' => 'integer',
+			'author_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Articles' => [
 				'conditions' => new QueryExpression([
 					'Articles.is_active' => true,
 					['Authors.id' => $field]
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'articles'
 			]
@@ -576,12 +601,20 @@ class HasManyTest extends \Cake\TestSuite\TestCase {
 		$field1 = new IdentifierExpression('Articles.author_id');
 		$field2 = new IdentifierExpression('Articles.author_site_id');
 		$association = new HasMany('Articles', $config);
+		$typeMap = new TypeMap([
+			'Articles.id' => 'integer',
+			'id' => 'integer',
+			'Articles.title' => 'string',
+			'title' => 'string',
+			'Articles.author_id' => 'integer',
+			'author_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Articles' => [
 				'conditions' => new QueryExpression([
 					'Articles.is_active' => true,
 					['Authors.id' => $field1, 'Authors.site_id' => $field2]
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'articles'
 			]
@@ -628,13 +661,21 @@ class HasManyTest extends \Cake\TestSuite\TestCase {
 		];
 		$field = new IdentifierExpression('Articles.author_id');
 		$association = new HasMany('Articles', $config);
+		$typeMap = new TypeMap([
+			'Articles.id' => 'integer',
+			'id' => 'integer',
+			'Articles.title' => 'string',
+			'title' => 'string',
+			'Articles.author_id' => 'integer',
+			'author_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Articles' => [
 				'conditions' => new QueryExpression([
 					'a' => 1,
 					'Articles.is_active' => true,
 					['Authors.id' => $field],
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'articles'
 			]

+ 46 - 5
tests/TestCase/ORM/Association/HasOneTest.php

@@ -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\ORM\Association\HasOne;
@@ -94,12 +95,20 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new HasOne('Profiles', $config);
 		$field = new IdentifierExpression('Profiles.user_id');
+		$typeMap = new TypeMap([
+			'Profiles.id' => 'integer',
+			'id' => 'integer',
+			'Profiles.first_name' => 'string',
+			'first_name' => 'string',
+			'Profiles.user_id' => 'integer',
+			'user_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Profiles' => [
 				'conditions' => new QueryExpression([
 					'Profiles.is_active' => true,
 					['Users.id' => $field],
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'profiles'
 			]
@@ -126,11 +135,19 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 			'conditions' => ['Profiles.is_active' => true]
 		];
 		$association = new HasOne('Profiles', $config);
+		$typeMap = new TypeMap([
+			'Profiles.id' => 'integer',
+			'id' => 'integer',
+			'Profiles.first_name' => 'string',
+			'first_name' => 'string',
+			'Profiles.user_id' => 'integer',
+			'user_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Profiles' => [
 				'conditions' => new QueryExpression([
 					'Profiles.is_active' => false
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'profiles'
 			]
@@ -161,12 +178,20 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new HasOne('Profiles', $config);
 		$field = new IdentifierExpression('Profiles.user_id');
+		$typeMap = new TypeMap([
+			'Profiles.id' => 'integer',
+			'id' => 'integer',
+			'Profiles.first_name' => 'string',
+			'first_name' => 'string',
+			'Profiles.user_id' => 'integer',
+			'user_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Profiles' => [
 				'conditions' => new QueryExpression([
 					'Profiles.is_active' => true,
 					['Users.id' => $field],
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'profiles'
 			]
@@ -190,13 +215,21 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 		];
 		$association = new HasOne('Profiles', $config);
 		$field = new IdentifierExpression('Profiles.user_id');
+		$typeMap = new TypeMap([
+			'Profiles.id' => 'integer',
+			'id' => 'integer',
+			'Profiles.first_name' => 'string',
+			'first_name' => 'string',
+			'Profiles.user_id' => 'integer',
+			'user_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Profiles' => [
 				'conditions' => new QueryExpression([
 					'a' => 1,
 					'Profiles.is_active' => true,
 					['Users.id' => $field],
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'profiles'
 			]
@@ -230,12 +263,20 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 		$association = new HasOne('Profiles', $config);
 		$field1 = new IdentifierExpression('Profiles.user_id');
 		$field2 = new IdentifierExpression('Profiles.user_site_id');
+		$typeMap = new TypeMap([
+			'Profiles.id' => 'integer',
+			'id' => 'integer',
+			'Profiles.first_name' => 'string',
+			'first_name' => 'string',
+			'Profiles.user_id' => 'integer',
+			'user_id' => 'integer',
+		]);
 		$query->expects($this->once())->method('join')->with([
 			'Profiles' => [
 				'conditions' => new QueryExpression([
 					'Profiles.is_active' => true,
 					['Users.id' => $field1, 'Users.site_id' => $field2],
-				]),
+				], $typeMap),
 				'type' => 'INNER',
 				'table' => 'profiles'
 			]