Skip to content
Permalink
Browse files

Add test for associated objects being null

When a hasOne association is missing null should be returned. Giving an
empty entity can be really confusing and makes validation much harder
that it should be.

Refs #3806
Refs #3902
  • Loading branch information...
markstory committed Jul 8, 2014
1 parent fbed75a commit da2af6dfa2aa859e981b77e021f216e1df6a4320
Showing with 32 additions and 0 deletions.
  1. +32 −0 tests/TestCase/ORM/Association/HasOneTest.php
@@ -29,6 +29,20 @@
*/
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
*
@@ -378,4 +392,22 @@ 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);
}
}

0 comments on commit da2af6d

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