CakePHPでコンポーネントのテスト

  • このエントリーをはてなブックマークに追加

モデルを使用しないコンポーネント用のテストする方法を書きたいと思います。
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('display_errors', 1);
/**
 * Use the DS to separate the directories in other defines
 */
	if (!defined('DS')) {
		define('DS', 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 == "WIN32" || PHP_OS == "WINNT") {
        define('C', 'C:');
    } else {
        define('C', '');
    }
/**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 *
 */
	if (!defined('ROOT')) {
		//define('ROOT', dirname(dirname(dirname(__FILE__))));
		define('ROOT', C.DS.'home'.DS.'example'.DS.'cake_app');
	}
/**
 * The actual directory name for the "app".
 *
 */
	if (!defined('APP_DIR')) {
		//define('APP_DIR', basename(dirname(dirname(__FILE__))));
		define ('APP_DIR', 'user');
	}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 *
 */
	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		//define('CAKE_CORE_INCLUDE_PATH', ROOT);
		define('CAKE_CORE_INCLUDE_PATH', C.DS.'home'.DS.'example'.DS.'cake_core');
	}

/**
 * Editing below this line should not be necessary.
 * Change at your own risk.
 *
 */
if (!defined('WEBROOT_DIR')) {
	define('WEBROOT_DIR', basename(dirname(__FILE__)));
}
if (!defined('WWW_ROOT')) {
	define('WWW_ROOT', dirname(__FILE__) . DS);
}
if (!defined('CORE_PATH')) {
	if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'))) {
		define('APP_PATH', null);
		define('CORE_PATH', null);
	} else {
		define('APP_PATH', ROOT . DS . APP_DIR . DS);
		define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
	}
}
if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) {
	trigger_error("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 " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
}

$corePath = App::core('cake');
if (isset($corePath[0])) {
	define('TEST_CAKE_CORE_INCLUDE_PATH', rtrim($corePath[0], DS) . DS);
} else {
	define('TEST_CAKE_CORE_INCLUDE_PATH', CAKE_CORE_INCLUDE_PATH);
}

if (Configure::read('debug') < 1) {
	die(__('Debug setting does not allow access to this url.', true));
}

require_once CAKE_TESTS_LIB . 'cake_test_suite_dispatcher.php';

$Dispatcher = new CakeTestSuiteDispatcher();
$Dispatcher->dispatch();

コンポーネントのテスト

coupon.phpという外部APIを使用してクーポン情報を取得するコンポーネントを例にします。
appのルートディレクトリはtest.phpにある通り/home/example/cake_app/user/になります。

ディレクトリ構成は下記のような形になります。

user
│
├─controllers
│ │
│ └─components
│         coupon.php(テスト対象コンポーネント)
└─tests
  ├─cases
     │
     ├─components
          coupon.test.php(テストクラス)

クーポンコードをパラメーターにクーポン情報を取得するCouponComponent->get()が有る場合に正しく取得できたかどうかのテストケースは以下のようになります。

<?php
App::import('Component', 'Coupon');

class CouponComponentTestCase extends CakeTestCase {
    function setUp () {
        $this->component = new CouponComponent();
    }

    function test_get () {
        $result = $this->component->get(array('coupon_code' => 'xxxx'));

        $xml = new Xml($result);
        $data = Set::reverse($xml);

        $this->assertEqual('success', $data['Result']['mstatus']);
    }

}

test.phpから実行確認も可能ですし、コンソールからも実行確認可能です。

  • このエントリーをはてなブックマークに追加

SNSでもご購読できます。

ads

コメントを残す

*