今日のPHP勉強会でdoctestすごい便利そうでいいなーと思ったので早速CakeのModelをテストしようと思ったのですがそのままでは動きませんでした。なんかクラス名にCakeと相容れない規則があるみたいだったので適当につぶしました。
doctestを利用するには、PHPUnit3とMaple4_DocTestが必要です。インストールは
第7回 DocTestを使ってUnitTestをやってみよう – テスト講座
を参考にしてください。
Maple4_DocTestがそのままだと動かないので修正します。
まず一箇所目。Maple4/Utils/Class.php。
*** ./Maple4_DocTest-0.2.0/Maple4/Utils/Class.php 2008-08-20 22:40:55.000000000 +0900
--- /usr/share/php/Maple4/Utils/Class.php 2008-08-31 21:46:53.000000000 +0900
***************
*** 81,86 ****
--- 81,91 ----
*/
public function toClassname($filename, $options = array())
{
+ $filename = basename($filename);
+ $parts = explode('_', $filename);
+ $parts = array_map('ucfirst', $parts);
+ $filename = implode('', $parts);
+
$doUcfirst = true;
if (isset($options['ucfirst']) &&
!is_null($options['ucfirst'])) {
もういっこ。Maple4/DocTest/Runner.php。
*** ./Maple4_DocTest-0.2.0/Maple4/DocTest/Runner.php 2008-08-20 22:40:55.000000000 +0900
--- /usr/share/php/Maple4/DocTest/Runner.php 2008-08-31 22:16:23.000000000 +0900
***************
*** 118,124 ****
require_once($realpath);
! $classname = $this->classUtils->toClassname($path);
if (!is_null($classname)) {
$suite->addTestSuite($classname);
--- 118,125 ----
require_once($realpath);
! //$classname = $this->classUtils->toClassname($path);
! $classname = current(explode('.', basename($path)));
if (!is_null($classname)) {
$suite->addTestSuite($classname);
たぶんこれで下準備は大丈夫。次はdoctestを実行するCake用のシェルスクリプトを用意します。以下の内容をdoctest.phpとしてapp/vendors/shells/の下に保存します。
/**
* DoctestShell
*
*/
class DoctestShell extends Shell {
/**
* model
*
* <example>
* cd app
* ../cake/console/cake doctest model
* <example>
*/
function model()
{
require_once CONFIGS . 'database.php';
App::Import('Core', array('Model', 'AppModel'));
$this->doctest('models');
}
/**
* doctest
*
*/
function doctest($place)
{
require_once 'Maple4/DocTest.php';
$pathname = dirname(dirname(dirname(__FILE__))) . '/' . $place;
$options = array(
'compileDir' => dirname(dirname(dirname(__FILE__))) . '/tmp/tests/' . $place,
'color' => true,
'report' => null,
'forceCompile' => true,
'notify' => null,
);
Maple4_DocTest::create()->run($pathname, $options);
}
}
で、あとは、CakePHP1.2 バッチ処理 | Sun Limited Mt.を参考に、
cd app
../cake/console/cake doctest model
と実行すると、Modelに対してdoctestが動きます。
Related posts: