Fixing #138
[akelos.git] / test / unit / lib / AkActiveRecord / _AkActiveRecord_finders.php
blob26a267eba0e42acffbc0e440d7dfb6101e76b404
1 <?php
3 defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION') ? null : define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false);
4 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
6 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
8 class AkActiveRecord_finders_TestCase extends AkUnitTest
11 function setup()
13 $this->installAndIncludeModels(array('Post', 'Tag', 'Comment'));
14 $Installer = new AkInstaller();
15 @$Installer->dropTable('posts_tags');
16 @Ak::file_delete(AK_MODELS_DIR.DS.'post_tag.php');
19 function test_should_find_using_id_and_options()
21 $Tag =& new Tag();
23 $One =& $Tag->create(array('name' => 'One'));
24 $Two =& $Tag->create(array('name' => 'Two'));
26 //find by id is always 'first'; API-change
27 //$Found =& $Tag->find('first', $Two->getId(), array('order'=>'name'));
28 $Found =& $Tag->find($Two->getId(), array('order'=>'name'));
30 $this->assertEqual($Found->getId(), $Two->getId());
34 function test_should_not_return_duplicated_owners_when_including_multiple_associates()
36 $Post =& new Post(array('title' => 'The best PHP Framework is ...'));
37 $Post->comment->create(array('name'=>'Comment 1'));
38 $Post->comment->create(array('name'=>'Comment 2'));
39 $Post->tag->create(array('name'=>'Tag 1'));
40 $Post->tag->create(array('name'=>'Tag 2'));
42 $this->assertTrue($Post->save());
44 // on PostgreSQL we get an unordered comments-list
45 $this->assertTrue($Post =& $Post->find($Post->getId(), array('include'=>array('comments', 'tags'))));
46 $exptected = array('Comment 1','Comment 2');
47 $this->assertTrue(in_array($Post->comments[0]->get('name'),$exptected));
48 $this->assertTrue(in_array($Post->comments[1]->get('name'),$exptected));
50 // so we could do this
51 $this->assertTrue($Post =& $Post->find($Post->getId(), array('include'=>array('comments', 'tags'),'order'=>'_comments.id ASC, _tags.id ASC')));
52 $this->assertEqual(count($Post->comments), 2);
53 $this->assertEqual($Post->comments[0]->get('name'), 'Comment 1');
54 $this->assertEqual($Post->comments[1]->get('name'), 'Comment 2');
56 $this->assertEqual(count($Post->tags), 2);
57 $this->assertEqual($Post->tags[0]->get('name'), 'Tag 1');
58 $this->assertEqual($Post->tags[1]->get('name'), 'Tag 2');
65 ak_test('AkActiveRecord_finders_TestCase',true);