Skip to content

Commit

Permalink
Fix CS issues
Browse files Browse the repository at this point in the history
Cleanup tests
  • Loading branch information
tigrang committed Mar 27, 2014
1 parent 5e013b1 commit 8829e54
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 329 deletions.
4 changes: 2 additions & 2 deletions src/Database/Expression/QueryExpression.php
Expand Up @@ -431,7 +431,7 @@ 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, $typeMap, $numericKey ? 'AND' : $k);
continue;
Expand Down Expand Up @@ -511,4 +511,4 @@ protected function _bindMultiplePlaceholders($field, $values, $type) {
return implode(', ', $params);
}

}
}
53 changes: 46 additions & 7 deletions src/Database/TypeMap.php
Expand Up @@ -14,41 +14,78 @@
*/
namespace Cake\Database;

/**
* Implements default and single-use mappings for columns to their associated types
*/
class TypeMap {

/**
* Associative array with the default fields and their types this query might contain
* used to avoid repetition when calling multiple times functions inside this class that
* may require a custom type for a specific field.
*
* @var array
*/
protected $_defaults;

/**
* Associative array with the fields and their types that override defaults this query might contain
* used to avoid repetition when calling multiple times functions inside this class that
* may require a custom type for a specific field.
*
* @var array
*/
protected $_types = [];

/**
* Creates an instance with the given defaults
*
* @param array $defaults
*/
public function __construct(array $defaults = []) {
$this->defaults($defaults);
}

/**
* Set/Get defaults
* Configures a map of default fields and their associated types to be
* used as the default list of types for every function in this class
* with a $types param. Useful to avoid repetition when calling the same
* functions using the same fields and types.
*
* @var this|array
* If called with no arguments it will return the currently configured types.
*
* ## Example
*
* {{{
* $query->defaults(['created' => 'datetime', 'is_visible' => 'boolean']);
* }}}
*
* @param array $defaults associative array where keys are field names and values
* are the correspondent type.
* @return this|array
*/
public function defaults(array $defaults = null) {
if ($defaults === null) {
return $this->_defaults;
return $this->_defaults;
}
$this->_defaults = $defaults;
return $this;
}

/**
* Set/Get types
* Configures a map of fields and their associated types for single-use.
*
* If called with no arguments it will return the currently configured types.
*
* ## Example
*
* {{{
* $query->types(['created' => 'time']);
* }}}
*
* @var this|array
* @param array $defaults associative array where keys are field names and values
* are the correspondent type.
* @return this|array
*/
public function types(array $types = null) {
if ($types === null) {
Expand All @@ -59,9 +96,11 @@ public function types(array $types = null) {
}

/**
* Get column type
* Returns the type of the given column. If there is no single use type is configured,
* the column type will be looked for inside the default mapping. If neither exist,
* null will be returned.
*
* @var string
* @var null|string
*/
public function type($column) {
if (isset($this->_types[$column])) {
Expand Down
8 changes: 6 additions & 2 deletions src/Database/TypeMapTrait.php
Expand Up @@ -16,6 +16,9 @@

use Cake\Database\TypeMap;

/*
* Represents a class that holds a TypeMap object
*/
trait TypeMapTrait {

/**
Expand All @@ -24,7 +27,8 @@ trait TypeMapTrait {
protected $_typeMap;

/**
* Setter/Getter for type map
* Creates a new TypeMap if $typeMap is an array, otherwise returns the existing type map
* or exchanges it for the given one.
*
* @param array|TypeMap $typeMap Creates a TypeMap if array, otherwise sets the given TypeMap
* @return this|TypeMap
Expand All @@ -34,7 +38,7 @@ public function typeMap($typeMap = null) {
if ($typeMap === null) {
return $this->_typeMap;
}
$this->_typeMap = is_array($typeMap) ? (new TypeMap)->types($typeMap) : $typeMap;
$this->_typeMap = is_array($typeMap) ? new TypeMap($typeMap) : $typeMap;
return $this;
}

Expand Down
82 changes: 21 additions & 61 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -16,10 +16,10 @@
*/
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;
use Cake\Database\TypeMap;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity;
Expand Down Expand Up @@ -72,6 +72,18 @@ public function setUp() {
]
]
]);
$this->tagsTypeMap = new TypeMap([
'Tags.id' => 'integer',
'id' => 'integer',
'Tags.name' => 'string',
'name' => 'string',
]);
$this->articlesTagsTypeMap = new TypeMap([
'ArticlesTags.article_id' => 'integer',
'article_id' => 'integer',
'ArticlesTags.tag_id' => 'integer',
'tag_id' => 'integer',
]);
}

/**
Expand Down Expand Up @@ -223,17 +235,11 @@ 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'
], $typeMap),
], $this->tagsTypeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -242,19 +248,12 @@ 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),
], $this->articlesTagsTypeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down Expand Up @@ -283,17 +282,11 @@ 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'
], $typeMap),
], $this->tagsTypeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -302,19 +295,12 @@ 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),
], $this->articlesTagsTypeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand All @@ -337,18 +323,12 @@ 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',
], $typeMap),
], $this->tagsTypeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -357,19 +337,12 @@ 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),
], $this->articlesTagsTypeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down Expand Up @@ -407,17 +380,11 @@ 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'
], $typeMap),
], $this->tagsTypeMap),
'type' => 'INNER',
'table' => 'tags'
]
Expand All @@ -428,19 +395,12 @@ 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),
], $this->articlesTagsTypeMap),
'type' => 'INNER',
'table' => 'articles_tags'
]
Expand Down

0 comments on commit 8829e54

Please sign in to comment.