めも。
■PHPUnitのインストール
以下のコマンドでXAMPP for MacでPHPUnitが使えるようになる。
sudo /Applications/XAMPP/xamppfiles/bin/pear channel-update pear.php.net sudo /Applications/XAMPP/xamppfiles/bin/pear upgrade pear sudo /Applications/XAMPP/xamppfiles/bin/pear channel-discover pear.phpunit.de sudo /Applications/XAMPP/xamppfiles/bin/pear channel-discover pear.symfony-project.com sudo /Applications/XAMPP/xamppfiles/bin/pear channel-discover components.ez.no sudo /Applications/XAMPP/xamppfiles/bin/pear install phpunit/PHPUnit
詳しくは以前の記事で。
■Bake
PHPのエラー
お決まりのtimezoneを設定してくれ的なのが出るので以下のようにする。
vim ./lib/Cake/Console/cake.php
require_onceの前に以下の文を一応記述しておく。
ini_set('date.timezone', 'Asia/Tokyo');
データベースの初期設定
以下のコマンドを実行してDBの設定をする。
./lib/Cake/Console/cake.php bake
対話式ですすめる。以下の順序で設定する。
Welcome to CakePHP v2.0.0 Console --------------------------------------------------------------- App : app Path: /Applications/XAMPP/xamppfiles/htdocs/cake.sample.justoneplanet.info/app/ --------------------------------------------------------------- Your database configuration was not found. Take a moment to create one. --------------------------------------------------------------- Database Configuration: --------------------------------------------------------------- Name: [default] > test Driver: (Mysql/Postgres/Sqlite/Sqlserver) [Mysql] > Persistent Connection? (y/n) [n] > Database Host: [localhost] > Port? [n] > User: [root] > Password: > hogehoge Database Name: [cake] > dbname Table Prefix? [n] > Table encoding? [n] >
モデル(クラス)の作成
以下のコマンドを再度実行する。
./lib/Cake/Console/cake.php bake
以下のように進めていく。テーブル設計などは適当なので適宜読み替える。
--------------------------------------------------------------- App : app Path: /Applications/XAMPP/xamppfiles/htdocs/cake.sample.justoneplanet.info/app/ --------------------------------------------------------------- Interactive Bake Shell --------------------------------------------------------------- [D]atabase Configuration [M]odel [V]iew [C]ontroller [P]roject [F]ixture [T]est case [Q]uit What would you like to Bake? (D/M/V/C/P/F/T/Q) > Model What would you like to Bake? (D/M/V/C/P/F/T/Q) > M --------------------------------------------------------------- Bake Model Path: /Applications/XAMPP/xamppfiles/htdocs/cake.sample.justoneplanet.info/app/Model/ --------------------------------------------------------------- Possible Models based on your current database: 1. Move Enter a number from the list above, type in the name of another model, or 'q' to exit [q] > 1 A displayField could not be automatically detected would you like to choose one? (y/n) > y 1. id 2. game 3. x 4. y 5. color Choose a field from the options above: > 1 Would you like to supply validation criteria for the fields in your model? (y/n) [y] > y
バリデーションの設定をしていくと、そのうち以下のようにユニットテスト用ファイルを生成するか聞いてくるので[y]を選択する。
Do you want to bake unit test files anyway? (y/n) [y] > y Creating file /Applications/XAMPP/xamppfiles/htdocs/cake.sample.justoneplanet.info/app/Test/Case/Model/MoveTest.php Wrote `/Applications/XAMPP/xamppfiles/htdocs/cake.sample.justoneplanet.info/app/Test/Case/Model/MoveTest.php`
■生成されたファイル
app/Test/Case/Model/MoveTestCase.php
<?php /* Move Test cases generated on: 2011-10-21 00:40:13 : 1319125213*/ App::uses('Move', 'Model'); /** * Move Test Case * */ class MoveTestCase extends CakeTestCase { /** * Fixtures * * @var array */ public $fixtures = array('app.move'); /** * setUp method * * @return void */ public function setUp() { parent::setUp(); $this->Move = ClassRegistry::init('Move'); } /** * tearDown method * * @return void */ public function tearDown() { unset($this->Move); parent::tearDown(); } /** * こんな感じで書いていけばいいよ的なmethod */ public function testGetById() { $this->assertEqual($this->Move->getById(1), 1); } }
app/Model/MoveTestCase.php
<?php App::uses('AppModel', 'Model'); /** * Move Model * */ class Move extends AppModel { /** * Display field * * @var string */ public $displayField = 'id'; /** * つじつま合わせのメソッド */ public function getById($id) { return 1; } }
最後にちゃちゃっとhttp://cake.sample.justoneplanet.info/test.phpにアクセスすればテストが書けているのがわかる。