From 0ec30be076a51f8760f83fb0cb24f3e5c1d3cf77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez=20Urdaneta?= Date: Wed, 14 Jul 2010 22:10:56 -0430 Subject: [PATCH] Improving tests for model associations lazy loading --- .../libs/model/model_integration.test.php | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index a352273ac..c74109bac 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -57,7 +57,7 @@ class ModelIntegrationTest extends BaseModelTest { * @return void */ public function testAssociationLazyLoading() { - $this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment'); + $this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags'); $Article = new ArticleFeatured(); $this->assertTrue(isset($Article->belongsTo['User'])); $this->assertFalse(property_exists($Article, 'User')); @@ -79,6 +79,57 @@ class ModelIntegrationTest extends BaseModelTest { $this->assertTrue(property_exists($Article, 'Tag')); $this->assertTrue(isset($Article->Tag)); $this->assertType('Tag', $Article->Tag); + $this->assertTrue(property_exists($Article, 'ArticleFeaturedsTag')); + $this->assertType('AppModel', $Article->ArticleFeaturedsTag); + $this->assertEquals($Article->hasAndBelongsToMany['Tag']['joinTable'], 'article_featureds_tags'); + $this->assertEquals($Article->hasAndBelongsToMany['Tag']['associationForeignKey'], 'tag_id'); + } + +/** + * testAssociationLazyLoadWithHABTM + * + * @group lazyloading + * @return void + */ + public function testAssociationLazyLoadWithHABTM() { + $this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag'); + $Article = new ArticleB(); + $this->assertTrue(isset($Article->hasAndBelongsToMany['TagB'])); + $this->assertFalse(property_exists($Article, 'TagB')); + $this->assertType('TagB', $Article->TagB); + + //Dynamic "with" models are not lazy loaded + $this->assertTrue(property_exists($Article, 'ArticlesTag')); + $this->assertType('AppModel', $Article->ArticlesTag); + + $UuidTag = new UuidTag(); + $this->assertTrue(isset($UuidTag->hasAndBelongsToMany['Fruit'])); + $this->assertFalse(property_exists($UuidTag, 'Fruit')); + $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); + $this->assertTrue(isset($UuidTag->Fruit)); + //But non-dynamic with models are lazy loaded + $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); + $this->assertTrue(isset($UuidTag->FruitsUuidTag)); + $this->assertType('FruitsUuidTag', $UuidTag->FruitsUuidTag); + } + +/** + * testAssociationLazyLoadWithBindModel + * + * @group lazyloading + * @return void + */ + public function testAssociationLazyLoadWithBindModel() { + $this->loadFixtures('Article', 'User'); + $Article = new ArticleB(); + + $this->assertFalse(isset($Article->belongsTo['User'])); + $this->assertFalse(property_exists($Article, 'User')); + + $Article->bindModel(array('belongsTo' => array('User'))); + $this->assertTrue(isset($Article->belongsTo['User'])); + $this->assertFalse(property_exists($Article, 'User')); + $this->assertType('User', $Article->User); } /**