Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rebuild lazy loading so there will be more compatibility with differe…
…nt scenarios.
- Loading branch information
Showing
2 changed files
with
129 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
App::import('Libs', 'LazyModel.LazyModel'); | ||
abstract class LazyAppModel extends LazyModel { | ||
} | ||
|
||
class Article extends LazyAppModel { | ||
public $belongsTo = array('User'); | ||
public $hasAndBelongsToMany = array('Tag'); | ||
} | ||
|
||
class User extends LazyAppModel { | ||
public $hasMany = array('Article'); | ||
} | ||
|
||
class Tag extends LazyAppModel { | ||
public $hasAndBelongsToMany = array('Article'); | ||
} | ||
|
||
class LazyModelTestCase extends CakeTestCase { | ||
public $fixtures = array('core.article', 'core.user', 'core.tag', 'core.articles_tag'); | ||
|
||
public function testLazyLoadingNonHABTM() { | ||
$user = ClassRegistry::init('User'); | ||
|
||
$this->assertFalse(property_exists($user, 'Article')); | ||
|
||
$user->Article->create(); | ||
|
||
$this->assertTrue(property_exists($user, 'Article')); | ||
} | ||
|
||
public function testLazyLoadingHABTM() { | ||
$article = ClassRegistry::init('Article'); | ||
|
||
$this->assertTrue(property_exists($article, 'Tag')); | ||
$this->assertTrue(property_exists($article, 'ArticlesTag')); | ||
$this->assertFalse(property_exists($article, 'User')); | ||
|
||
$article->User->create(); | ||
|
||
$this->assertTrue(property_exists($article, 'Tag')); | ||
$this->assertTrue(property_exists($article, 'ArticlesTag')); | ||
$this->assertTrue(property_exists($article, 'User')); | ||
} | ||
|
||
public function testNoRecursion() { | ||
$article = ClassRegistry::init('Article'); | ||
$results = $article->find('all', array('recursive' => -1)); | ||
|
||
$this->assertEqual(Set::extract('/Article/id', $results), array(1,2,3)); | ||
$this->assertEqual(array_keys($results[0]), array('Article')); | ||
} | ||
|
||
public function testRecursionLevel0() { | ||
$article = ClassRegistry::init('Article'); | ||
$results = $article->find('all', array('recursive' => 0)); | ||
|
||
$this->assertEqual(Set::extract('/Article/id', $results), array(1,2,3)); | ||
$this->assertEqual(Set::extract('/User/id', $results), array(1,3,1)); | ||
$this->assertEqual(array_keys($results[0]), array('Article', 'User')); | ||
} | ||
|
||
public function testRecursionLevel1() { | ||
$article = ClassRegistry::init('Article'); | ||
$results = $article->find('all', array('recursive' => 1)); | ||
|
||
$this->assertEqual(Set::extract('/Article/id', $results), array(1,2,3)); | ||
$this->assertEqual(Set::extract('/User/id', $results), array(1,3,1)); | ||
$this->assertEqual(Set::extract('/0/Tag/id', $results), array(1,2)); | ||
$this->assertEqual(array_keys($results[0]), array('Article', 'User', 'Tag')); | ||
} | ||
|
||
public function testContainable() { | ||
$user = ClassRegistry::init('User'); | ||
$user->Behaviors->attach('Containable'); | ||
|
||
$results = $user->find('all', array('contain' => array('Article' => array('limit' => 1)))); | ||
|
||
$this->assertEqual(Set::extract('/Article/id', $results), array(1,2)); | ||
$this->assertEqual(array_keys($results[0]), array('User', 'Article')); | ||
} | ||
|
||
public function endTest() { | ||
ClassRegistry::flush(); | ||
} | ||
} | ||
?> |