Skip to content
Permalink
Browse files

Move tests to the class they belong with.

Put tests for eagerloading associations into ResultSet as the hydration
of entities happens there.
  • Loading branch information...
markstory committed Jul 11, 2014
1 parent 752eb41 commit 5489813a20ead867654397b2b37474f66c8e727d
@@ -410,11 +410,11 @@ protected function _groupResult($row) {
}
if (!$hasData) {
continue;
$results[$alias] = null;
}
}
if ($this->_hydrate && $assoc['canBeJoined']) {
if ($this->_hydrate && $results[$alias] !== null && $assoc['canBeJoined']) {
$entity = new $assoc['entityClass']($results[$alias], $options);
$entity->clean();
$results[$alias] = $entity;
@@ -446,22 +446,4 @@ public function testAttachToBeforeFindExtraOptions() {
}]);
}
/**
* Test that eagerLoader leaves empty associations unpopulated.
*
* @return void
*/
public function testEagerLoaderLeavesEmptyAssocation() {
$this->loadFixtures('Article', 'Comment');
$comments = TableRegistry::get('Comments');
$comments->belongsTo('Articles');
// Clear the articles table so we can trigger an empty belongsTo
$articles = TableRegistry::get('Articles');
$articles->deleteAll([]);
$comment = $comments->get(1, ['contain' => ['Articles']]);
$this->assertNull($comment->article);
}
}
@@ -29,20 +29,6 @@
*/
class HasOneTest extends \Cake\TestSuite\TestCase {
/**
* Fixtures to use.
*
* @var array
*/
public $fixtures = ['core.article', 'core.comment'];
/**
* Don't autoload fixtures as most tests uses mocks.
*
* @var bool
*/
public $autoFixture = false;
/**
* Set up
*
@@ -392,22 +378,4 @@ public function testAttachToBeforeFindExtraOptions() {
}]);
}
/**
* Test that eagerLoader leaves empty associations unpopulated.
*
* @return void
*/
public function testEagerLoaderLeavesEmptyAssocation() {
$this->loadFixtures('Article', 'Comment');
$articles = TableRegistry::get('Articles');
$articles->hasOne('Comments');
// Clear the comments table so we can trigger an empty hasOne.
$comments = TableRegistry::get('Comments');
$comments->deleteAll([]);
$article = $articles->get(1, ['contain' => ['Comments']]);
$this->assertNull($article->comment);
}
}
@@ -20,14 +20,15 @@
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* ResultSet test case.
*/
class ResultSetTest extends TestCase {
public $fixtures = ['core.article'];
public $fixtures = ['core.article', 'core.comment'];
/**
* setup
@@ -249,4 +250,56 @@ public function testDebugInfo() {
$this->assertSame($expected, $results->__debugInfo());
}
/**
* Test that eagerLoader leaves empty associations unpopulated.
*
* @return void
*/
public function testBelongsToEagerLoaderLeavesEmptyAssocation() {
$comments = TableRegistry::get('Comments');
$comments->belongsTo('Articles');
// Clear the articles table so we can trigger an empty belongsTo
$this->table->deleteAll([]);
$comment = $comments->find()->where(['Comments.id' => 1])
->contain(['Articles'])
->hydrate(false)
->first();
$this->assertEquals(1, $comment['id']);
$this->assertNotEmpty($comment['comment']);
$this->assertNull($comment['article']);
$comment = $comments->get(1, ['contain' => ['Articles']]);
$this->assertNull($comment->article);
$this->assertEquals(1, $comment->id);
$this->assertNotEmpty($comment->comment);
}
/**
* Test that eagerLoader leaves empty associations unpopulated.
*
* @return void
*/
public function testHasOneEagerLoaderLeavesEmptyAssocation() {
$this->table->hasOne('Comments');
// Clear the comments table so we can trigger an empty hasOne.
$comments = TableRegistry::get('Comments');
$comments->deleteAll([]);
$article = $this->table->get(1, ['contain' => ['Comments']]);
$this->assertNull($article->comment);
$this->assertEquals(1, $article->id);
$this->assertNotEmpty($article->title);
$article = $this->table->find()->where(['Articles.id' => 1])
->contain(['Comments'])
->hydrate(false)
->first();
$this->assertNull($article['comment']);
$this->assertEquals(1, $article['id']);
$this->assertNotEmpty($article['title']);
}
}

0 comments on commit 5489813

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