Skip to content
Permalink
Browse files

Updated doc block with FCQN

Updated test to include a reset being passed
Add a test to catch invalid argument exception.
  • Loading branch information...
InfosecGene committed Mar 5, 2018
1 parent 89d45aa commit 69e7c0408d06b00e53115a84a61572a4f7797a45
Showing with 103 additions and 56 deletions.
  1. +8 −6 src/ORM/Query.php
  2. +95 −50 tests/TestCase/ORM/QueryTest.php
@@ -17,7 +17,6 @@
use ArrayObject;
use Cake\Database\ExpressionInterface;
use Cake\Database\Query as DatabaseQuery;
use Cake\Database\Schema\TableSchema;
use Cake\Database\TypedResultInterface;
use Cake\Database\TypeMap;
use Cake\Database\ValueBinder;
@@ -200,22 +199,25 @@ public function select($fields = [], $overwrite = false)
* Excluded fields should not be aliased names.
*
*
* @param Table|Association $table The table to use to get an array of columns
* @param \Cake\ORM\Table|\Cake\ORM\Association $table The table to use to get an array of columns
* @param array $excludedFields The un-aliased column names you do not want selected from $table
* @param bool $overwrite Whether to reset/remove previous selected fields
* @return Query
* @throws \InvalidArgumentException If Association|Table is not passed in first argument
*/
public function selectAllExcept($table, array $excludedFields, $overwrite = false)
{
if ($table instanceof Association) {
$table = $table->getTarget();
}
$aliasedFields = [];
if ($table instanceof Table) {
$fields = array_diff($table->getSchema()->columns(), $excludedFields);
$aliasedFields = $this->aliasFields($fields);
if (!($table instanceof Table)) {
throw new \InvalidArgumentException('You must provide either an Association or a Table object');
}
$fields = array_diff($table->getSchema()->columns(), $excludedFields);
$aliasedFields = $this->aliasFields($fields);
return $this->select($aliasedFields, $overwrite);
}
@@ -1565,8 +1565,8 @@ public function testCountWithSubselect()
$counter = $table->ArticlesTags->find();
$counter->select([
'total' => $counter->func()->count('*')
])
'total' => $counter->func()->count('*')
])
->where([
'ArticlesTags.tag_id' => 1,
'ArticlesTags.article_id' => new IdentifierExpression('Articles.id')
@@ -2004,9 +2004,11 @@ public function testContainWithClosure()
$query = new Query($this->connection, $table);
$query
->select()
->contain(['articles' => function ($q) {
return $q->where(['articles.id' => 1]);
}]);
->contain([
'articles' => function ($q) {
return $q->where(['articles.id' => 1]);
}
]);
$ids = [];
foreach ($query as $entity) {
@@ -2287,23 +2289,25 @@ public function testFormatBelongsToRecords()
$table->belongsTo('authors');
$query = $table->find()
->contain(['authors' => function ($q) {
return $q
->formatResults(function ($authors) {
return $authors->map(function ($author) {
$author->idCopy = $author->id;
return $author;
});
})
->formatResults(function ($authors) {
return $authors->map(function ($author) {
$author->idCopy = $author->idCopy + 2;
return $author;
->contain([
'authors' => function ($q) {
return $q
->formatResults(function ($authors) {
return $authors->map(function ($author) {
$author->idCopy = $author->id;
return $author;
});
})
->formatResults(function ($authors) {
return $authors->map(function ($author) {
$author->idCopy = $author->idCopy + 2;
return $author;
});
});
});
}]);
}
]);
$query->formatResults(function ($results) {
return $results->combine('id', 'author.idCopy');
@@ -2374,15 +2378,17 @@ public function testFormatDeepDistantAssociationRecords()
$articles->hasMany('articlesTags');
$articles->getAssociation('articlesTags')->getTarget()->belongsTo('tags');
$query = $table->find()->contain(['articles.articlesTags.tags' => function ($q) {
return $q->formatResults(function ($results) {
return $results->map(function ($tag) {
$tag->name .= ' - visited';
$query = $table->find()->contain([
'articles.articlesTags.tags' => function ($q) {
return $q->formatResults(function ($results) {
return $results->map(function ($tag) {
$tag->name .= ' - visited';
return $tag;
return $tag;
});
});
});
}]);
}
]);
$query->mapReduce(function ($row, $key, $mr) {
foreach ((array)$row->articles as $article) {
@@ -2450,9 +2456,11 @@ public function testContainInAssociationQuery()
$query = $table->find()
->order(['Articles.id' => 'ASC'])
->contain(['Articles' => function ($q) {
return $q->contain('Authors');
}]);
->contain([
'Articles' => function ($q) {
return $q->contain('Authors');
}
]);
$results = $query->extract('article.author.name')->toArray();
$expected = ['mariano', 'mariano', 'larry', 'larry'];
$this->assertEquals($expected, $results);
@@ -2561,11 +2569,13 @@ public function testEagerLoaded()
{
$table = $this->getTableLocator()->get('authors');
$table->hasMany('articles');
$query = $table->find()->contain(['articles' => function ($q) {
$this->assertTrue($q->isEagerLoaded());
$query = $table->find()->contain([
'articles' => function ($q) {
$this->assertTrue($q->isEagerLoaded());
return $q;
}]);
return $q;
}
]);
$this->assertFalse($query->isEagerLoaded());
$table->getEventManager()->on('Model.beforeFind', function ($e, $q, $o, $primary) {
@@ -2589,11 +2599,13 @@ public function testIsEagerLoaded()
{
$table = $this->getTableLocator()->get('authors');
$table->hasMany('articles');
$query = $table->find()->contain(['articles' => function ($q) {
$this->assertTrue($q->isEagerLoaded());
$query = $table->find()->contain([
'articles' => function ($q) {
$this->assertTrue($q->isEagerLoaded());
return $q;
}]);
return $q;
}
]);
$this->assertFalse($query->isEagerLoaded());
$table->getEventManager()->on('Model.beforeFind', function ($e, $q, $o, $primary) {
@@ -2750,10 +2762,12 @@ public function testAutoFieldsWithContainQueryBuilder()
->select(['myField' => '(SELECT 2 + 2)'])
->enableAutoFields(true)
->enableHydration(false)
->contain(['Authors' => function ($q) {
return $q->select(['compute' => '(SELECT 2 + 20)'])
->enableAutoFields(true);
}])
->contain([
'Authors' => function ($q) {
return $q->select(['compute' => '(SELECT 2 + 20)'])
->enableAutoFields(true);
}
])
->first();
$this->assertArrayHasKey('myField', $result);
@@ -3351,8 +3365,8 @@ public function testInnerJoinWith()
});
$expected = [
[
'id' => 1,
'name' => 'mariano'
'id' => 1,
'name' => 'mariano'
]
];
$this->assertEquals($expected, $results->enableHydration(false)->toArray());
@@ -3375,8 +3389,8 @@ public function testInnerJoinWithNested()
});
$expected = [
[
'id' => 3,
'name' => 'larry'
'id' => 3,
'name' => 'larry'
]
];
$this->assertEquals($expected, $results->enableHydration(false)->toArray());
@@ -3606,9 +3620,11 @@ public function testSelectAllExceptWithContains()
$result = $table
->find()
->contain(['Comments' => function ($query) use ($table) {
return $query->selectAllExcept($table->Comments, ['published']);
}])
->contain([
'Comments' => function ($query) use ($table) {
return $query->selectAllExcept($table->Comments, ['published']);
}
])
->selectAllExcept($table, ['body'])
->first();
$this->assertNull($result->comments[0]->published);
@@ -3654,5 +3670,34 @@ public function testSelectAllExceptWithMulitpleCalls()
'Articles__published' => 'Articles.published'
];
$this->assertEquals($expected, $selectedFields);
$result = $table
->find()
->selectAllExcept($table, ['body'])
->selectAllExcept($table, ['published', 'body'], true);
$selectedFields = $result->clause('select');
$expected = [
'Articles__id' => 'Articles.id',
'Articles__author_id' => 'Articles.author_id',
'Articles__title' => 'Articles.title',
];
$this->assertEquals($expected, $selectedFields);
}
/**
* Test that given the wrong first parameter, Invalid argument exception is thrown
*
* @return void
*/
public function testSelectAllExceptThrowsInvalidArgument()
{
$table = $this->getTableLocator()->get('Articles');
try {
$table
->find()
->selectAllExcept([], ['body']);
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true);
}
}
}

0 comments on commit 69e7c04

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