CakePHP1.3でtestを書くにはsimpletestが必要。(デフォルトで付いてない)CakePHP1.3はちょっと古いのでsimpletestの最新では動かないので1.0系を使う。
まずFixtureを書く。Fixtureはクラスで書く。(これyamlとかでできないかなあ)
<?php class PostFixture extends CakeTestFixture { var $name = 'Post'; var $table = 'posts'; var $fields = array( 'id' => array( 'type' => 'integer', 'null' => false, 'key' => 'primary' ), 'name' => array( 'type' => 'integer', 'null' => false ) ); var $records = array( array('id' => 1, 'name' => 'komagata'), array('id' => 2, 'name' => 'machida'), array('id' => 3, 'name' => 'yoshida') ); } ?>
modelのテストケースを書く。
<?php
class TestPost extends Post {
var $cacheSources = false;
var $useDbConfig = 'test';
}
class PostTestCase extends CakeTestCase {
var $Post = null;
var $fixtures = array('app.post');
function startTest() {
$this->Post =& ClassRegistry::init('Post');
}
function endTest() {
unset($this->Post);
ClassRegistry::flush();
}
function testPostInstance() {
$this->assertTrue(is_a($this->Post, 'Post'));
}
function testFetchKomagata() {
$this->Post->recursive = -1;
$result = $this->Post->fetchKomagata();
$expected = array(
'Post' => array('id' => 1, 'name' => 'komagata')
);
$this->assertEqual($result[0], $expected);
}
}
?>
こんな感じ。/test.phpから結果が見れる。