モデルを使用しないコンポーネント用のテストする方法を書きたいと思います。 CakePHPは1.3.5を使用しています。 テスト(Testing) :: CakePHPによる作業の定石 :: マニュアル :: 1.3コレクション.

simpletestインストール

マニュアルにも書いてある通りsimpletestはCakePHPに含まれないのでSimpleTest - Unit Testing for PHP.からダウンロードしてきてvendorsにフォルダをコピーしておきます。 ※1.3.5ではsimpletest1.1系は動かないようだったので、1.0系をインストールしました。 モデルを使う場合はapp/config/database.phpにテスト用のデータベースの設定を書いておいた方が良いですが今回は使用しないので特に書きません。 http://text.example.com/test.phpでCakePHPに組み込まれているテストケースを実行できます。 私の場合は、1つCakePHPに複数appをディレクトリ毎に分けているので以下のようにtest.phpを変更しました。

set_time_limit(0); ini_set(&apos;display_errors&apos;, 1); /** * Use the DS to separate the directories in other defines */ if (!defined(&apos;DS&apos;)) { define(&apos;DS&apos;, DIRECTORY_SEPARATOR); } /** * These defines should only be edited if you have cake installed in * a directory layout other than the way it is distributed. * When using custom settings be sure to use the DS and do not add a trailing DS. */ if (PHP_OS == &quot;WIN32&quot; || PHP_OS == &quot;WINNT&quot;) { define(&apos;C&apos;, &apos;C:&apos;); } else { define(&apos;C&apos;, &apos;&apos;); } /** * The full path to the directory which holds &quot;app&quot;, WITHOUT a trailing DS. * */ if (!defined(&apos;ROOT&apos;)) { //define(&apos;ROOT&apos;, dirname(dirname(dirname(__FILE__)))); define(&apos;ROOT&apos;, C.DS.&apos;home&apos;.DS.&apos;example&apos;.DS.&apos;cake_app&apos;); } /** * The actual directory name for the &quot;app&quot;. * */ if (!defined(&apos;APP_DIR&apos;)) { //define(&apos;APP_DIR&apos;, basename(dirname(dirname(__FILE__)))); define (&apos;APP_DIR&apos;, &apos;user&apos;); } /** * The absolute path to the &quot;cake&quot; directory, WITHOUT a trailing DS. * */ if (!defined(&apos;CAKE_CORE_INCLUDE_PATH&apos;)) { //define(&apos;CAKE_CORE_INCLUDE_PATH&apos;, ROOT); define(&apos;CAKE_CORE_INCLUDE_PATH&apos;, C.DS.&apos;home&apos;.DS.&apos;example&apos;.DS.&apos;cake_core&apos;); } /** * Editing below this line should not be necessary. * Change at your own risk. * */ if (!defined(&apos;WEBROOT_DIR&apos;)) { define(&apos;WEBROOT_DIR&apos;, basename(dirname(__FILE__))); } if (!defined(&apos;WWW_ROOT&apos;)) { define(&apos;WWW_ROOT&apos;, dirname(__FILE__) . DS); } if (!defined(&apos;CORE_PATH&apos;)) { if (function_exists(&apos;ini_set&apos;) &amp;&amp; ini_set(&apos;include_path&apos;, CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get(&apos;include_path&apos;))) { define(&apos;APP_PATH&apos;, null); define(&apos;CORE_PATH&apos;, null); } else { define(&apos;APP_PATH&apos;, ROOT . DS . APP_DIR . DS); define(&apos;CORE_PATH&apos;, CAKE_CORE_INCLUDE_PATH . DS); } } if (!include(CORE_PATH . &apos;cake&apos; . DS . &apos;bootstrap.php&apos;)) { trigger_error(&quot;CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your &quot; . DS . &quot;cake core directory and your &quot; . DS . &quot;vendors root directory.&quot;, E_USER_ERROR); } $corePath = App::core(&apos;cake&apos;); if (isset($corePath[0])) { define(&apos;TEST_CAKE_CORE_INCLUDE_PATH&apos;, rtrim($corePath[0], DS) . DS); } else { define(&apos;TEST_CAKE_CORE_INCLUDE_PATH&apos;, CAKE_CORE_INCLUDE_PATH); } if (Configure::read(&apos;debug&apos;)</p><h2 id="h0db5610283">コンポーネントのテスト</h2><p><strong>coupon.php</strong>という外部APIを使用してクーポン情報を取得するコンポーネントを例にします。 appのルートディレクトリは<strong>test.php</strong>にある通り<strong>/home/example/cake_app/user/</strong>になります。 ディレクトリ構成は下記のような形になります。 <pre><code class="language-bash">user │ ├─controllers │ │ │ └─components │ coupon.php(テスト対象コンポーネント) └─tests ├─cases │ ├─components coupon.test.php(テストクラス)</code></pre> クーポンコードをパラメーターにクーポン情報を取得するCouponComponent-&gt;get()が有る場合に正しく取得できたかどうかのテストケースは以下のようになります。 [php] component = new CouponComponent(); } function test_get () { $result = $this-&gt;component-&gt;get(array(&apos;coupon_code&apos; =&gt; &apos;xxxx&apos;)); $xml = new Xml($result); $data = Set::reverse($xml); $this-&gt;assertEqual(&apos;success&apos;, $data[&apos;Result&apos;][&apos;mstatus&apos;]); } }
test.phpから実行確認も可能ですし、コンソールからも実行確認可能です。