diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 56386b584..d2c4f69b8 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -31,6 +31,11 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { } uses('model'.DS.'model', 'model'.DS.'datasources'.DS.'datasource', 'model'.DS.'datasources'.DS.'dbo_source', 'model'.DS.'datasources'.DS.'dbo'.DS.'dbo_mysql'); + +if (!class_exists('AppModel')) { + loadModel(); +} + /** * Short description for class. * @@ -299,6 +304,46 @@ class Author extends CakeTestModel { return $results; } } +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Project extends CakeTestModel { + var $name = 'Project'; + var $hasMany = array('Thread'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Thread extends CakeTestModel { + var $name = 'Thread'; + var $hasMany = array('Message'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Message extends CakeTestModel { + var $name = 'Message'; + var $hasOne = array('Bid'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Bid extends CakeTestModel { + var $name = 'Bid'; + var $belongsTo = array('Message'); +} /** * Short description for class. * @@ -310,7 +355,8 @@ class ModelTest extends CakeTestCase { var $fixtures = array( 'core.category', 'core.category_thread', 'core.user', 'core.article', 'core.featured', 'core.article_featureds_tags', 'core.article_featured', 'core.tag', 'core.articles_tag', 'core.comment', 'core.attachment', - 'core.apple', 'core.sample', 'core.another_article', 'core.advertisement', 'core.home', 'core.post', 'core.author' + 'core.apple', 'core.sample', 'core.another_article', 'core.advertisement', 'core.home', 'core.post', 'core.author', + 'core.project', 'core.thread', 'core.message', 'core.bid' ); function start() { @@ -324,6 +370,72 @@ class ModelTest extends CakeTestCase { Configure::write('debug', $this->debug); } + function testHasManyOptimization() { + $this->Project =& new Project(); + + $this->Project->recursive = 3; + $result = $this->Project->findAll(); + $expected = array( + array( + 'Project' => array( + 'id' => 1, 'name' => 'Project 1' + ), + 'Thread' => array( + array( + 'id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1', + 'Message' => array( + array( + 'id' => 1, 'thread' => 1, 'name' => 'Thread 1, Message 1', + 'Bid' => array( + 'id' => 1, 'message_id' => 1, 'name' => 'Bid 1.1' + ) + ) + ) + ), + array( + 'id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2', + 'Message' => array( + array( + 'id' => 2, 'thread' => 2, 'name' => 'Thread 2, Message 1', + 'Bid' => array( + 'id' => 4, 'message_id' => 2, 'name' => 'Bid 2.1' + ) + ) + ) + ) + ) + ), + array( + 'Project' => array( + 'id' => 2, 'name' => 'Project 2' + ), + 'Thread' => array( + array( + 'id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1', + 'Message' => array( + array( + 'id' => 3, 'thread' => 3, 'name' => 'Thread 3, Message 1', + 'Bid' => array( + 'id' => 3, 'message_id' => 3, 'name' => 'Bid 3.1' + ) + ) + ) + ) + ) + ), + array( + 'Project' => array( + 'id' => 3, 'name' => 'Project 3' + ), + 'Thread' => array() + ) + ); + + $this->assertEqual($result, $expected); + + unset($this->Project); + } + function testFindAllRecursiveSelfJoin() { $this->model =& new Home(); diff --git a/cake/tests/fixtures/bid_fixture.php b/cake/tests/fixtures/bid_fixture.php new file mode 100644 index 000000000..c961786cc --- /dev/null +++ b/cake/tests/fixtures/bid_fixture.php @@ -0,0 +1,50 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class BidFixture extends CakeTestFixture { + var $name = 'Bid'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'message_id' => array('type' => 'integer', 'null' => false), + 'name' => array('type' => 'string', 'null' => false) + ); + var $records = array( + array ('id' => 1, 'message_id' => 1, 'name' => 'Bid 1.1'), + array ('id' => 2, 'message_id' => 1, 'name' => 'Bid 1.2'), + array ('id' => 3, 'message_id' => 3, 'name' => 'Bid 3.1'), + array ('id' => 4, 'message_id' => 2, 'name' => 'Bid 2.1'), + array ('id' => 5, 'message_id' => 2, 'name' => 'Bid 2.2') + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/message_fixture.php b/cake/tests/fixtures/message_fixture.php new file mode 100644 index 000000000..3960a1e8b --- /dev/null +++ b/cake/tests/fixtures/message_fixture.php @@ -0,0 +1,48 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class MessageFixture extends CakeTestFixture { + var $name = 'Message'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'thread_id' => array('type' => 'integer', 'null' => false), + 'name' => array('type' => 'string', 'null' => false) + ); + var $records = array( + array ('id' => 1, 'thread_id' => 1, 'name' => 'Thread 1, Message 1'), + array ('id' => 2, 'thread_id' => 2, 'name' => 'Thread 2, Message 1'), + array ('id' => 3, 'thread_id' => 3, 'name' => 'Thread 3, Message 1') + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/project_fixture.php b/cake/tests/fixtures/project_fixture.php new file mode 100644 index 000000000..12b5e2961 --- /dev/null +++ b/cake/tests/fixtures/project_fixture.php @@ -0,0 +1,47 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class ProjectFixture extends CakeTestFixture { + var $name = 'Project'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'null' => false) + ); + var $records = array( + array ('id' => 1, 'name' => 'Project 1'), + array ('id' => 2, 'name' => 'Project 2'), + array ('id' => 3, 'name' => 'Project 3') + ); +} +?> \ No newline at end of file diff --git a/cake/tests/fixtures/thread_fixture.php b/cake/tests/fixtures/thread_fixture.php new file mode 100644 index 000000000..77a7e536c --- /dev/null +++ b/cake/tests/fixtures/thread_fixture.php @@ -0,0 +1,48 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class ThreadFixture extends CakeTestFixture { + var $name = 'Thread'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'project_id' => array('type' => 'integer', 'null' => false), + 'name' => array('type' => 'string', 'null' => false) + ); + var $records = array( + array ('id' => 1, 'project_id' => 1, 'name' => 'Project 1, Thread 1'), + array ('id' => 2, 'project_id' => 1, 'name' => 'Project 1, Thread 2'), + array ('id' => 3, 'project_id' => 2, 'name' => 'Project 2, Thread 1') + ); +} +?> \ No newline at end of file