3 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__
).'/../../../fixtures/config/config.php');
6 class test_AkActiveRecord_counters
extends AkUnitTest
11 $this->installAndIncludeModels(array('Post','Comment'));
12 $Post =& $this->Post
->create(array('title'=>'A Title','body'=>'and a body'));
13 $this->PostId
= $Post->getId();
17 function test_counter_should_be_default_zero()
19 $Post = $this->Post
->find($this->PostId
);
20 $counter = $Post->comments_count
;
22 $this->assertEqual($counter,0);
23 $this->assertNotNull($counter); // !
26 function test_should_increment_counter_by_one()
28 $this->assertTrue($this->Post
->incrementCounter('comments_count',$this->PostId
));
30 $Post = $this->Post
->find($this->PostId
);
31 $counter = $Post->comments_count
;
33 $this->assertEqual($counter,1);
36 function test_should_increment_counter_by_delta()
38 $this->assertTrue($this->Post
->incrementCounter('comments_count',$this->PostId
,50));
40 $Post = $this->Post
->find($this->PostId
);
41 $counter = $Post->comments_count
;
43 $this->assertEqual($counter,50);
46 function test_should_increment_counter_multiple_times()
48 $this->assertTrue($this->Post
->incrementCounter('comments_count',$this->PostId
));
49 $this->assertTrue($this->Post
->incrementCounter('comments_count',$this->PostId
));
50 $this->assertTrue($this->Post
->incrementCounter('comments_count',$this->PostId
));
52 $Post = $this->Post
->find($this->PostId
);
53 $counter = $Post->comments_count
;
55 $this->assertEqual($counter,3);
58 function test_should_decrement_counter_by_one()
60 $this->assertTrue($this->Post
->decrementCounter('comments_count',$this->PostId
));
62 $Post = $this->Post
->find($this->PostId
);
63 $counter = $Post->comments_count
;
65 $this->assertEqual($counter,-1);
68 function test_should_decrement_counter_by_delta()
70 $this->assertTrue($this->Post
->decrementCounter('comments_count',$this->PostId
,50));
72 $Post = $this->Post
->find($this->PostId
);
73 $counter = $Post->comments_count
;
75 $this->assertEqual($counter,-50);
78 function test_should_decrement_counter_multiple_times()
80 $this->assertTrue($this->Post
->decrementCounter('comments_count',$this->PostId
));
81 $this->assertTrue($this->Post
->decrementCounter('comments_count',$this->PostId
));
82 $this->assertTrue($this->Post
->decrementCounter('comments_count',$this->PostId
));
84 $Post = $this->Post
->find($this->PostId
);
85 $counter = $Post->comments_count
;
87 $this->assertEqual($counter,-3);
95 ak_test('test_AkActiveRecord_counters', true);