Added some tests for related models afterFind callback

This commit is contained in:
k-halaburda 2013-08-13 09:29:14 +02:00
parent 3855c0690e
commit 8e40a3a3d3
7 changed files with 14407 additions and 14009 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* ModelReadTest file * ModelReadTest file
* *
@ -17,7 +18,6 @@
* @since CakePHP(tm) v 1.2.0.4206 * @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
*/ */
require_once dirname(__FILE__) . DS . 'ModelTestBase.php'; require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
/** /**
@ -27,7 +27,7 @@ require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
*/ */
class ModelReadTest extends BaseModelTest { class ModelReadTest extends BaseModelTest {
/** /**
* testExists function * testExists function
* @return void * @return void
*/ */
@ -46,7 +46,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse($TestModel->exists(2)); $this->assertFalse($TestModel->exists(2));
} }
/** /**
* testFetchingNonUniqueFKJoinTableRecords() * testFetchingNonUniqueFKJoinTableRecords()
* *
* Tests if the results are properly returned in the case there are non-unique FK's * Tests if the results are properly returned in the case there are non-unique FK's
@ -88,7 +88,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(in_array(false, $doomed)); $this->assertTrue(in_array(false, $doomed));
} }
/** /**
* testGroupBy method * testGroupBy method
* *
* These tests will never pass with Postgres or Oracle as all fields in a select must be * These tests will never pass with Postgres or Oracle as all fields in a select must be
@ -248,7 +248,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testOldQuery method * testOldQuery method
* *
* @return void * @return void
@ -284,7 +284,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(is_array($results)); $this->assertTrue(is_array($results));
} }
/** /**
* testPreparedQuery method * testPreparedQuery method
* *
* @return void * @return void
@ -351,7 +351,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse($result === false); $this->assertFalse($result === false);
} }
/** /**
* testParameterMismatch method * testParameterMismatch method
* *
* @expectedException PDOException * @expectedException PDOException
@ -370,7 +370,7 @@ class ModelReadTest extends BaseModelTest {
$Article->query($query, $params); $Article->query($query, $params);
} }
/** /**
* testVeryStrangeUseCase method * testVeryStrangeUseCase method
* *
* @expectedException PDOException * @expectedException PDOException
@ -390,7 +390,7 @@ class ModelReadTest extends BaseModelTest {
$Article->query($query, $param); $Article->query($query, $param);
} }
/** /**
* testRecursiveUnbind method * testRecursiveUnbind method
* *
* @return void * @return void
@ -2986,7 +2986,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testSelfAssociationAfterFind method * testSelfAssociationAfterFind method
* *
* @return void * @return void
@ -3008,7 +3008,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($afterFindData, $noAfterFindData); $this->assertEquals($afterFindData, $noAfterFindData);
} }
/** /**
* Test that afterFind can completely unset data. * Test that afterFind can completely unset data.
* *
* @return void * @return void
@ -3027,12 +3027,11 @@ class ModelReadTest extends BaseModelTest {
$model->ModifiedComment->remove = true; $model->ModifiedComment->remove = true;
$result = $model->find('all'); $result = $model->find('all');
$this->assertTrue( $this->assertTrue(
empty($result[0]['ModifiedComment']), empty($result[0]['ModifiedComment']), 'Zeroith row should be removed by afterFind'
'Zeroith row should be removed by afterFind'
); );
} }
/** /**
* testFindThreadedNoParent method * testFindThreadedNoParent method
* *
* @return void * @return void
@ -3046,7 +3045,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindThreaded method * testFindThreaded method
* *
* @return void * @return void
@ -3132,7 +3131,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindAllThreaded method * testFindAllThreaded method
* *
* @return void * @return void
@ -3608,7 +3607,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* test find('neighbors') * test find('neighbors')
* *
* @return void * @return void
@ -3762,7 +3761,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* Test find(neighbors) with missing fields so no neighbors are found. * Test find(neighbors) with missing fields so no neighbors are found.
* *
* @return void * @return void
@ -3787,7 +3786,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindCombinedRelations method * testFindCombinedRelations method
* *
* @return void * @return void
@ -4065,7 +4064,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testSaveEmpty method * testSaveEmpty method
* *
* @return void * @return void
@ -4078,7 +4077,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse($expected); $this->assertFalse($expected);
} }
/** /**
* testFindAllWithConditionInChildQuery * testFindAllWithConditionInChildQuery
* *
* @return void * @return void
@ -4121,7 +4120,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindAllWithConditionsHavingMixedDataTypes method * testFindAllWithConditionsHavingMixedDataTypes method
* *
* @return void * @return void
@ -4202,22 +4201,14 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testBindUnbind method * testBindUnbind method
* *
* @return void * @return void
*/ */
public function testBindUnbind() { public function testBindUnbind() {
$this->loadFixtures( $this->loadFixtures(
'User', 'User', 'Comment', 'FeatureSet', 'DeviceType', 'DeviceTypeCategory', 'ExteriorTypeCategory', 'Device', 'Document', 'DocumentDirectory'
'Comment',
'FeatureSet',
'DeviceType',
'DeviceTypeCategory',
'ExteriorTypeCategory',
'Device',
'Document',
'DocumentDirectory'
); );
$TestModel = new User(); $TestModel = new User();
@ -4695,7 +4686,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(is_object($TestModel2->NewFeatureSet)); $this->assertTrue(is_object($TestModel2->NewFeatureSet));
} }
/** /**
* testBindMultipleTimes method * testBindMultipleTimes method
* *
* @return void * @return void
@ -4867,7 +4858,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* test that multiple reset = true calls to bindModel() result in the original associations. * test that multiple reset = true calls to bindModel() result in the original associations.
* *
* @return void * @return void
@ -4883,7 +4874,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse(isset($TestModel->hasMany['Comment']), 'Association left behind'); $this->assertFalse(isset($TestModel->hasMany['Comment']), 'Association left behind');
} }
/** /**
* testBindMultipleTimes method with different reset settings * testBindMultipleTimes method with different reset settings
* *
* @return void * @return void
@ -4901,8 +4892,7 @@ class ModelReadTest extends BaseModelTest {
)); ));
$this->assertTrue($result); $this->assertTrue($result);
$result = $TestModel->bindModel( $result = $TestModel->bindModel(
array('hasMany' => array('Article')), array('hasMany' => array('Article')), false
false
); );
$this->assertTrue($result); $this->assertTrue($result);
@ -4917,7 +4907,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* test that bindModel behaves with Custom primary Key associations * test that bindModel behaves with Custom primary Key associations
* *
* @return void * @return void
@ -4936,7 +4926,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse(empty($result)); $this->assertFalse(empty($result));
} }
/** /**
* test that calling unbindModel() with reset == true multiple times * test that calling unbindModel() with reset == true multiple times
* leaves associations in the correct state. * leaves associations in the correct state.
* *
@ -4953,7 +4943,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(isset($TestModel->hasMany['Comment']), 'Association permanently removed'); $this->assertTrue(isset($TestModel->hasMany['Comment']), 'Association permanently removed');
} }
/** /**
* testBindMultipleTimes method with different reset settings * testBindMultipleTimes method with different reset settings
* *
* @return void * @return void
@ -4971,8 +4961,7 @@ class ModelReadTest extends BaseModelTest {
)); ));
$this->assertTrue($result); $this->assertTrue($result);
$result = $TestModel->unbindModel( $result = $TestModel->unbindModel(
array('belongsTo' => array('Article')), array('belongsTo' => array('Article')), false
false
); );
$this->assertTrue($result); $this->assertTrue($result);
@ -4987,7 +4976,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testAssociationAfterFind method * testAssociationAfterFind method
* *
* @return void * @return void
@ -5082,7 +5071,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]); $this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
} }
/** /**
* testDeeperAssociationAfterFind method * testDeeperAssociationAfterFind method
* *
* @return void * @return void
@ -5115,7 +5104,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals('Fired', $result['Comment'][0]['Attachment']['callback']); $this->assertEquals('Fired', $result['Comment'][0]['Attachment']['callback']);
} }
/** /**
* Tests that callbacks can be properly disabled * Tests that callbacks can be properly disabled
* *
* @return void * @return void
@ -5141,7 +5130,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testAssociationAfterFindCallbacksDisabled method * testAssociationAfterFindCallbacksDisabled method
* *
* @return void * @return void
@ -5234,7 +5223,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]); $this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
} }
/** /**
* Tests that the database configuration assigned to the model can be changed using * Tests that the database configuration assigned to the model can be changed using
* (before|after)Find callbacks * (before|after)Find callbacks
* *
@ -5246,7 +5235,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(3, count($TestModel->find('all'))); $this->assertEquals(3, count($TestModel->find('all')));
} }
/** /**
* testCallbackSourceChangeUnknownDatasource method * testCallbackSourceChangeUnknownDatasource method
* *
* @expectedException MissingDatasourceConfigException * @expectedException MissingDatasourceConfigException
@ -5258,20 +5247,14 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse($TestModel->find('all', array('connection' => 'foo'))); $this->assertFalse($TestModel->find('all', array('connection' => 'foo')));
} }
/** /**
* testMultipleBelongsToWithSameClass method * testMultipleBelongsToWithSameClass method
* *
* @return void * @return void
*/ */
public function testMultipleBelongsToWithSameClass() { public function testMultipleBelongsToWithSameClass() {
$this->loadFixtures( $this->loadFixtures(
'DeviceType', 'DeviceType', 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory', 'Document', 'Device', 'DocumentDirectory'
'DeviceTypeCategory',
'FeatureSet',
'ExteriorTypeCategory',
'Document',
'Device',
'DocumentDirectory'
); );
$DeviceType = new DeviceType(); $DeviceType = new DeviceType();
@ -5356,7 +5339,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testHabtmRecursiveBelongsTo method * testHabtmRecursiveBelongsTo method
* *
* @return void * @return void
@ -5414,7 +5397,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testNonNumericHabtmJoinKey method * testNonNumericHabtmJoinKey method
* *
* @return void * @return void
@ -5516,7 +5499,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testHabtmFinderQuery method * testHabtmFinderQuery method
* *
* @return void * @return void
@ -5545,8 +5528,7 @@ class ModelReadTest extends BaseModelTest {
)), )),
'conditions' => array(), 'conditions' => array(),
'order' => null 'order' => null
), ), $Article
$Article
); );
$Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql; $Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql;
@ -5564,7 +5546,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result['Tag']); $this->assertEquals($expected, $result['Tag']);
} }
/** /**
* testHabtmLimitOptimization method * testHabtmLimitOptimization method
* *
* @return void * @return void
@ -5634,7 +5616,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testHasManyLimitOptimization method * testHasManyLimitOptimization method
* *
* @return void * @return void
@ -5750,7 +5732,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindAllRecursiveSelfJoin method * testFindAllRecursiveSelfJoin method
* *
* @return void * @return void
@ -5858,18 +5840,14 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindAllRecursiveWithHabtm method * testFindAllRecursiveWithHabtm method
* *
* @return void * @return void
*/ */
public function testFindAllRecursiveWithHabtm() { public function testFindAllRecursiveWithHabtm() {
$this->loadFixtures( $this->loadFixtures(
'MyCategoriesMyUsers', 'MyCategoriesMyUsers', 'MyCategoriesMyProducts', 'MyCategory', 'MyUser', 'MyProduct'
'MyCategoriesMyProducts',
'MyCategory',
'MyUser',
'MyProduct'
); );
$MyUser = new MyUser(); $MyUser = new MyUser();
@ -5928,7 +5906,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testReadFakeThread method * testReadFakeThread method
* *
* @return void * @return void
@ -5992,7 +5970,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindFakeThread method * testFindFakeThread method
* *
* @return void * @return void
@ -6056,7 +6034,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindAllFakeThread method * testFindAllFakeThread method
* *
* @return void * @return void
@ -6276,7 +6254,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testConditionalNumerics method * testConditionalNumerics method
* *
* @return void * @return void
@ -6293,7 +6271,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(empty($result)); $this->assertTrue(empty($result));
} }
/** /**
* test buildQuery() * test buildQuery()
* *
* @return void * @return void
@ -6324,7 +6302,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* test find('all') method * test find('all') method
* *
* @return void * @return void
@ -6553,7 +6531,7 @@ class ModelReadTest extends BaseModelTest {
} }
} }
/** /**
* test find('list') method * test find('list') method
* *
* @return void * @return void
@ -6592,8 +6570,7 @@ class ModelReadTest extends BaseModelTest {
$TestModel->find('all', array( $TestModel->find('all', array(
'order' => 'Article.title ASC', 'order' => 'Article.title ASC',
'fields' => array('id', 'title') 'fields' => array('id', 'title')
)), )), '{n}.Article.id', '{n}.Article.title'
'{n}.Article.id', '{n}.Article.title'
); );
$expected = array( $expected = array(
1 => 'First Article', 1 => 'First Article',
@ -6605,8 +6582,7 @@ class ModelReadTest extends BaseModelTest {
$result = Hash::combine( $result = Hash::combine(
$TestModel->find('all', array( $TestModel->find('all', array(
'order' => 'Article.title ASC' 'order' => 'Article.title ASC'
)), )), '{n}.Article.id', '{n}.Article'
'{n}.Article.id', '{n}.Article'
); );
$expected = array( $expected = array(
1 => array( 1 => array(
@ -6642,8 +6618,7 @@ class ModelReadTest extends BaseModelTest {
$result = Hash::combine( $result = Hash::combine(
$TestModel->find('all', array( $TestModel->find('all', array(
'order' => 'Article.title ASC' 'order' => 'Article.title ASC'
)), )), '{n}.Article.id', '{n}.Article', '{n}.Article.user_id'
'{n}.Article.id', '{n}.Article', '{n}.Article.user_id'
); );
$expected = array( $expected = array(
1 => array( 1 => array(
@ -6682,8 +6657,7 @@ class ModelReadTest extends BaseModelTest {
$TestModel->find('all', array( $TestModel->find('all', array(
'order' => 'Article.title ASC', 'order' => 'Article.title ASC',
'fields' => array('id', 'title', 'user_id') 'fields' => array('id', 'title', 'user_id')
)), )), '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id'
'{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id'
); );
$expected = array( $expected = array(
@ -6828,7 +6802,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testFindField method * testFindField method
* *
* @return void * @return void
@ -6859,7 +6833,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(4, $result); $this->assertEquals(4, $result);
} }
/** /**
* testFindUnique method * testFindUnique method
* *
* @return void * @return void
@ -6881,7 +6855,7 @@ class ModelReadTest extends BaseModelTest {
))); )));
} }
/** /**
* test find('count') method * test find('count') method
* *
* @return void * @return void
@ -6929,7 +6903,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* Test that find('first') does not use the id set to the object. * Test that find('first') does not use the id set to the object.
* *
* @return void * @return void
@ -6944,7 +6918,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals('Project 1', $result['Project']['name'], 'Wrong record retrieved'); $this->assertEquals('Project 1', $result['Project']['name'], 'Wrong record retrieved');
} }
/** /**
* test find with COUNT(DISTINCT field) * test find with COUNT(DISTINCT field)
* *
* @return void * @return void
@ -6963,7 +6937,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(4, $result); $this->assertEquals(4, $result);
} }
/** /**
* Test find(count) with Db::expression * Test find(count) with Db::expression
* *
* @return void * @return void
@ -6986,7 +6960,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(1, $result); $this->assertEquals(1, $result);
} }
/** /**
* testFindMagic method * testFindMagic method
* *
* @return void * @return void
@ -7017,7 +6991,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testRead method * testRead method
* *
* @return void * @return void
@ -7097,20 +7071,14 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testRecursiveRead method * testRecursiveRead method
* *
* @return void * @return void
*/ */
public function testRecursiveRead() { public function testRecursiveRead() {
$this->loadFixtures( $this->loadFixtures(
'User', 'User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Featured', 'ArticleFeatured'
'Article',
'Comment',
'Tag',
'ArticlesTag',
'Featured',
'ArticleFeatured'
); );
$TestModel = new User(); $TestModel = new User();
@ -7212,16 +7180,7 @@ class ModelReadTest extends BaseModelTest {
public function testRecursiveFindAll() { public function testRecursiveFindAll() {
$this->loadFixtures( $this->loadFixtures(
'User', 'User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment', 'ArticleFeatured', 'ArticleFeaturedsTags', 'Featured', 'Category'
'Article',
'Comment',
'Tag',
'ArticlesTag',
'Attachment',
'ArticleFeatured',
'ArticleFeaturedsTags',
'Featured',
'Category'
); );
$TestModel = new Article(); $TestModel = new Article();
@ -7522,7 +7481,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testRecursiveFindAllWithLimit method * testRecursiveFindAllWithLimit method
* *
* @return void * @return void
@ -7688,7 +7647,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* Testing availability of $this->findQueryType in Model callbacks * Testing availability of $this->findQueryType in Model callbacks
* *
* @return void * @return void
@ -7702,7 +7661,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals('first', $comments['Comment']['querytype']); $this->assertEquals('first', $comments['Comment']['querytype']);
} }
/** /**
* testVirtualFields() * testVirtualFields()
* *
* Test correct fetching of virtual fields * Test correct fetching of virtual fields
@ -7722,7 +7681,7 @@ class ModelReadTest extends BaseModelTest {
$Post->Author->virtualFields = array('false' => '1 = 2'); $Post->Author->virtualFields = array('false' => '1 = 2');
$result = $Post->find('first'); $result = $Post->find('first');
$this->assertEquals(2, $result['Post']['two']); $this->assertEquals(2, $result['Post']['two']);
$this->assertFalse((bool)$result['Author']['false']); $this->assertFalse((bool) $result['Author']['false']);
} }
$result = $Post->find('first', array('fields' => array('author_id'))); $result = $Post->find('first', array('fields' => array('author_id')));
@ -7786,7 +7745,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(4, $result); $this->assertEquals(4, $result);
} }
/** /**
* testVirtualFieldsOrder() * testVirtualFieldsOrder()
* *
* Test correct order on virtual fields * Test correct order on virtual fields
@ -7832,7 +7791,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* testVirtualFieldsMysql() * testVirtualFieldsMysql()
* *
* Test correct fetching of virtual fields * Test correct fetching of virtual fields
@ -7885,7 +7844,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(isset($result['Author']['full_name'])); $this->assertTrue(isset($result['Author']['full_name']));
} }
/** /**
* test that virtual fields work when they don't contain functions. * test that virtual fields work when they don't contain functions.
* *
* @return void * @return void
@ -7900,7 +7859,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(isset($result['Post']['writer']), 'virtual field not fetched %s'); $this->assertTrue(isset($result['Post']['writer']), 'virtual field not fetched %s');
} }
/** /**
* test that isVirtualField will accept both aliased and non aliased fieldnames * test that isVirtualField will accept both aliased and non aliased fieldnames
* *
* @return void * @return void
@ -7918,7 +7877,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertFalse($Post->isVirtualField(array())); $this->assertFalse($Post->isVirtualField(array()));
} }
/** /**
* test that getting virtual fields works with and without model alias attached * test that getting virtual fields works with and without model alias attached
* *
* @return void * @return void
@ -7932,7 +7891,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']); $this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']);
} }
/** /**
* test that checks for error when NOT condition passed in key and a 1 element array value * test that checks for error when NOT condition passed in key and a 1 element array value
* *
* @return void * @return void
@ -7943,8 +7902,7 @@ class ModelReadTest extends BaseModelTest {
$Article->recursive = -1; $Article->recursive = -1;
$result = $Article->find( $result = $Article->find(
'all', 'all', array(
array(
'conditions' => array( 'conditions' => array(
'Article.id NOT' => array(1) 'Article.id NOT' => array(1)
) )
@ -7953,7 +7911,7 @@ class ModelReadTest extends BaseModelTest {
$this->assertTrue(is_array($result) && !empty($result)); $this->assertTrue(is_array($result) && !empty($result));
} }
/** /**
* test custom find method * test custom find method
* *
* @return void * @return void
@ -7973,4 +7931,212 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
} }
/**
* test after find callback on related model
*
* @return void
*/
public function testRelatedAfterFindCallback() {
$this->loadFixtures('ModelWithRelations', 'ModelRelated', 'ModelHabtmRelation');
$ModelWithRelations = new ModelWithRelations();
// belongsTo Test
$ModelWithRelations->bindModel(array(
'belongsTo' => array(
'BelongsTo' => array(
'className' => 'ModelRelated',
'foreignKey' => 'related_id',
)
)
)
);
$results = $ModelWithRelations->find('all');
$expected = array(
array(
'ModelWithRelations' => array(
'id' => '1',
'name' => 'First record',
'related_id' => '1'
),
'BelongsTo' => array(
'id' => '1',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '1'
)
),
array(
'ModelWithRelations' => array(
'id' => '2',
'name' => 'Second record',
'related_id' => '2'
),
'BelongsTo' => array(
'id' => '2',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '2'
)
)
);
$this->assertEquals($expected, $results, 'Model related with belongsTo afterFind callback fail');
// hasOne test
$ModelWithRelations->bindModel(array(
'hasOne' => array(
'HasOne' => array(
'className' => 'ModelRelated',
'foreignKey' => 'primary_id',
)
)
)
);
$results = $ModelWithRelations->find('all');
$expected = array(
array(
'ModelWithRelations' => array(
'id' => '1',
'name' => 'First record',
'related_id' => '1'
),
'HasOne' => array(
'id' => '1',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '1'
)
),
array(
'ModelWithRelations' => array(
'id' => '2',
'name' => 'Second record',
'related_id' => '2'
),
'HasOne' => array(
'id' => '2',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '2'
)
)
);
$this->assertEquals($expected, $results, 'Model related with hasOne afterFind callback fail');
// hasMany test
$ModelWithRelations->bindModel(array(
'hasMany' => array(
'HasMany' => array(
'className' => 'ModelRelated',
'foreignKey' => 'primary_id',
)
)
)
);
$results = $ModelWithRelations->find('all');
$expected = array(
array(
'ModelWithRelations' => array(
'id' => '1',
'name' => 'First record',
'related_id' => '1'
),
'HasMany' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '1'
)
)
),
array(
'ModelWithRelations' => array(
'id' => '2',
'name' => 'Second record',
'related_id' => '2'
),
'HasMany' => array(
(int) 0 => array(
'id' => '2',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '2'
)
)
)
);
$this->assertEquals($expected, $results, 'Model related with hasMany afterFind callback fail');
// hasAndBelongsToMany test
$ModelWithRelations->bindModel(array(
'hasAndBelongsToMany' => array(
'HasAndBelongsToMany' => array(
'className' => 'ModelRelated',
'with' => 'ModelHabtmRelation',
'foreignKey' => 'primary_id',
'associationForeignKey' => 'related_id',
)
)
)
);
$results = $ModelWithRelations->find('all');
$expected = array(
array(
'ModelWithRelations' => array(
'id' => '1',
'name' => 'First record',
'related_id' => '1'
),
'HasAndBelongsToMany' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '1',
'ModelHabtmRelation' => array(
'id' => '1',
'primary_id' => '1',
'related_id' => '1'
)
),
(int) 1 => array(
'id' => '2',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '2',
'ModelHabtmRelation' => array(
'id' => '2',
'primary_id' => '1',
'related_id' => '2'
)
)
)
),
array(
'ModelWithRelations' => array(
'id' => '2',
'name' => 'Second record',
'related_id' => '2'
),
'HasAndBelongsToMany' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '1',
'ModelHabtmRelation' => array(
'id' => '3',
'primary_id' => '2',
'related_id' => '1'
)
),
(int) 1 => array(
'id' => '2',
'name' => 'Successfuly changed in AfterFind',
'primary_id' => '2',
'ModelHabtmRelation' => array(
'id' => '4',
'primary_id' => '2',
'related_id' => '2'
)
)
)
)
);
$this->assertEquals($expected, $results, 'Model related with hasAndBelongsToMany afterFind callback fail');
}
} }

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* ModelTest file * ModelTest file
* *
@ -17,7 +18,6 @@
* @since CakePHP(tm) v 1.2.0.4206 * @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
*/ */
App::uses('Model', 'Model'); App::uses('Model', 'Model');
App::uses('AppModel', 'Model'); App::uses('AppModel', 'Model');
require_once dirname(__FILE__) . DS . 'models.php'; require_once dirname(__FILE__) . DS . 'models.php';
@ -29,21 +29,21 @@ require_once dirname(__FILE__) . DS . 'models.php';
*/ */
abstract class BaseModelTest extends CakeTestCase { abstract class BaseModelTest extends CakeTestCase {
/** /**
* autoFixtures property * autoFixtures property
* *
* @var bool false * @var bool false
*/ */
public $autoFixtures = false; public $autoFixtures = false;
/** /**
* Whether backup global state for each test method or not * Whether backup global state for each test method or not
* *
* @var bool false * @var bool false
*/ */
public $backupGlobals = false; public $backupGlobals = false;
/** /**
* fixtures property * fixtures property
* *
* @var array * @var array
@ -72,9 +72,10 @@ abstract class BaseModelTest extends CakeTestCase {
'core.fruits_uuid_tag', 'core.uuid_tag', 'core.product_update_all', 'core.group_update_all', 'core.fruits_uuid_tag', 'core.uuid_tag', 'core.product_update_all', 'core.group_update_all',
'core.player', 'core.guild', 'core.guilds_player', 'core.armor', 'core.armors_player', 'core.player', 'core.guild', 'core.guilds_player', 'core.armor', 'core.armors_player',
'core.bidding', 'core.bidding_message', 'core.site', 'core.domain', 'core.domains_site', 'core.bidding', 'core.bidding_message', 'core.site', 'core.domain', 'core.domains_site',
'core.model_with_relations','core.model_related','core.model_habtm_relation',
); );
/** /**
* setUp method * setUp method
* *
* @return void * @return void
@ -84,7 +85,7 @@ abstract class BaseModelTest extends CakeTestCase {
$this->debug = Configure::read('debug'); $this->debug = Configure::read('debug');
} }
/** /**
* tearDown method * tearDown method
* *
* @return void * @return void
@ -94,4 +95,5 @@ abstract class BaseModelTest extends CakeTestCase {
Configure::write('debug', $this->debug); Configure::write('debug', $this->debug);
ClassRegistry::flush(); ClassRegistry::flush();
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,52 @@
<?php
/**
* Short description for file.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Fixture
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Class PrimaryRelatedFixture
*
* @package Cake.Test.Fixture
*/
class ModelHabtmRelationFixture extends CakeTestFixture {
/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'primary_id' => array('type' => 'integer'),
'related_id' => array('type' => 'integer'),
);
/**
* records property
*
* @var array
*/
public $records = array(
array('id' => 1, 'primary_id' => 1, 'related_id' => 1),
array('id' => 2, 'primary_id' => 1, 'related_id' => 2),
array('id' => 3, 'primary_id' => 2, 'related_id' => 1),
array('id' => 4, 'primary_id' => 2, 'related_id' => 2),
);
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Short description for file.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Fixture
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Class RelatedFixture
*
* @package Cake.Test.Fixture
*/
class ModelRelatedFixture extends CakeTestFixture {
/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'name' => array('type' => 'string'),
'primary_id' => array('type' => 'integer'),
);
/**
* records property
*
* @var array
*/
public $records = array(
array('id' => 1, 'name' => 'This should change on afterFind', 'primary_id' => 1),
array('id' => 2, 'name' => 'This also should change on afterFind', 'primary_id' => 2)
);
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Short description for file.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Fixture
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Class PrimaryFixture
*
* @package Cake.Test.Fixture
*/
class ModelWithRelationsFixture extends CakeTestFixture {
/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'name' => array('type' => 'string'),
'related_id' => array('type' => 'integer'),
);
/**
* records property
*
* @var array
*/
public $records = array(
array('id' => 1, 'name' => 'First record', 'related_id' => 1),
array('id' => 2, 'name' => 'Second record', 'related_id' => 2)
);
}