Browse Source

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
mark_story 11 years ago
parent
commit
da2af6dfa2
1 changed files with 32 additions and 0 deletions
  1. 32 0
      tests/TestCase/ORM/Association/HasOneTest.php

+ 32 - 0
tests/TestCase/ORM/Association/HasOneTest.php

@@ -30,6 +30,20 @@ use Cake\ORM\TableRegistry;
 class HasOneTest extends \Cake\TestSuite\TestCase {
 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
  * Set up
  *
  *
  * @return void
  * @return void
@@ -378,4 +392,22 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
 		}]);
 		}]);
 	}
 	}
 
 
+/**
+ * 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);
+	}
+
 }
 }