testing

sample

code


target file(s):


models/user.php

1 <?php
2 class User extends AppModel {
3 var $name = 'User';
4 var $validate = array(
5 'name' => array(
6 'alphanumeric' => array(
7 'rule' => array('alphanumeric'),
8 'message' => 'アルファベットか数字で入力してください。',
9 ),
10 'maxlength' => array(
11 'rule' => array('maxlength', 10),
12 'message' => '10文字以内で入力してください',
13 ),
14 ),
15 'birthday' => array(
16 'rule' => 'date',
17 'message' => '正しい日付で入力してください。',
18 'allowEmpty' => true,
19 ),
20 'is_invalid' => array(
21 'rule' => array('boolean'),
22 'message' => 'is_invalidの値が無効です。',
23 ),
24 );
25 var $dummyId = 1;
26
27 function getDummyId() {
28 return $this->dummyId;
29 }
30
31 function readDummy() {
32 return $this->read(null, $this->dummyId);
33 }
34
35 function saveDummy($data) {
36 $data['User']['id'] = $this->dummyId;
37 return $this->save($data);
38 }
39 }
40 ?>

tests/cases/models/user.test.php

1 <?php
2
3 App::import('Model', 'User');
4
5 class UserTestCase extends CakeTestCase {
6 var $fixtures = array( 'app.user' );
7
8 function testGetDummyId() {
9 $this->User =& ClassRegistry::init('User');
10
11 $result = $this->User->getDummyId();
12
13 $this->assertEqual($result, 1);
14 }
15
16 function testReadDummy() {
17 $this->User =& ClassRegistry::init('User');
18
19 $result = $this->User->readDummy();
20 $expected = array(
21 'User' => array(
22 'id' => 1,
23 'name' => 'Test用の名前',
24 'is_invalid' => 1,
25 'birthday' => '1971-07-31',
26 'created' => '2010-03-04 12:34:56',
27 'modified' => '2010-03-04 12:34:56',
28 ),
29 );
30 $this->assertEqual($result, $expected);
31 }
32 }

tests/fixtures/user_fixture.php

1 <?php
2
3 class UserFixture extends CakeTestFixture {
4 var $name = 'User';
5 // var $import = array('model' => 'User', 'records' => true);
6
7 var $fields = array(
8 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary'),
9 'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 24),
10 'is_invalid' => array('type' => 'boolean', 'null' => true, 'default' => NULL),
11 'birthday' => array('type' => 'date', 'null' => true, 'default' => NULL),
12 'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
13 'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
14 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
15 'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
16 );
17
18 var $records = array(
19 array (
20 'id' => 1,
21 'name' => 'Test用の名前',
22 'is_invalid' => 1,
23 'birthday' => '1971-07-31',
24 'created' => '2010-03-04 12:34:56',
25 'modified' => '2010-03-04 12:34:56'
26 ),
27 );
28
29 }
30 ?>