テストの登録を自動化
http://blog.as-roma.com/BoBlog/index.php?itemid=1338
のUnitTestCaseコマンドを使わせてもらってテストを書きまくったおかげでようやくTDDが身についてきたような気配。
でもappid_UnitTestManagerに手動でテストを登録していくのがめんどくさいです。
なのでappid_UnitTestManagerがテストを検索して勝手に登録して実行するようにしてみた。
app/appid_Controller.php
class appid_Controller extends Ethna_Controller { var $directory = array( 'test' => 'app/test', ); function getTestdir($short = false) { if ($short) { return str_replace($this->getBasedir() . '/', '', $this->getDirectory('test')) . '/'; } else { return $this->getDirectory('test') . '/'; } } }
app/appid_UnitTestManager.php
<?php class appid_UnitTestManager extends Ethna_UnitTestManager { function appid_UnitTestManager(&$backend) { parent::Ethna_UnitTestManager($backend); $this->testcase = array_merge($this->testcase, $this->_getTestCaseList()); } function _getTestCaseList($test_dir = null) { $r = array(); if (is_null($test_dir)) { $test_dir = $this->ctl->getTestdir(); } $child_dir_list = array(); $dh = opendir($test_dir); if ($dh == false) { return; } $ext = $this->ctl->getExt('php'); while (($file = readdir($dh)) !== false) { if ($file == "." || $file == "..") { continue; } $file = $test_dir . $file; if (is_dir($file)) { $child_dir_list[] = $file; continue; } if (preg_match("/\.$ext\$/", $file) == 0) { continue; } $file = str_replace($this->ctl->getTestdir(), '', $file); $key = ereg_replace("(.*)_TestCase\.$ext", '\1', $file); $key = str_replace('/', '', $key); $r[$key] = $this->ctl->getTestdir(true) . $file; } closedir($dh); foreach ($child_dir_list as $child_dir) { $tmp = $this->_getTestCaseList($child_dir . "/"); $r = array_merge($r, $tmp); } return $r; } }
これで
ethna add-unit-test-case appid_Hoge
してできたapp/test/appid/Hoge_TestCase.phpとかが勝手に実行されるようになります。らくちん!