Skip to content

Commit

Permalink
Move tests to the class they belong with.
Browse files Browse the repository at this point in the history
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 5489813
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/ORM/ResultSet.php
Expand Up @@ -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;
Expand Down
18 changes: 0 additions & 18 deletions tests/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -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);
}

}
32 changes: 0 additions & 32 deletions tests/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -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
*
Expand Down Expand 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);
}

}
55 changes: 54 additions & 1 deletion tests/TestCase/ORM/ResultSetTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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.