[Zend Framework] Zend_Test: PHPUnit を動かしてみる

いろいろと分からないことが多いので、とりあえず動かしてみるところまでやってみることにしました。

Zend_TestコンポーネントはPHPUnitと呼ばれる単体テストフレームワークの上に構築されています。例えば、テストを具体的に記述するためのクラスであるZend_Test_PHPUnit_ControllerTestCaseは、PHPUnitのクラスであるPHPUnit_Framework_TestCaseを継承したものになっています。

PHPUnit をインストールする

システム全体で使うためには、グローバルパスを通す必要があるとのこと。
いままでPEARで用意されていた経緯から、ネット上の情報やPHP, Zendまわりの環境設定もPEARで入れておいた方がつまづかなさそうです。

が、手っ取り早く試したいこと、今後のことを考えて Composer で入れました。

ZFコマンドで作成されているファイルを確認する

今までの経験から、ZFコマンドでプロジェクトを作成すると雛形が作成されているので、今回も確認します。

./tests
├── application
│   └── controllers
│       └── IndexControllerTest.php
├── bootstrap.php
├── library
└── phpunit.xml
3 directories, 3 files

下記を雛形にすることになりそうです。

tests/application/controllers/IndexControllerTest.php
<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        parent::setUp();
    }
}

テスト環境の構築」は、このままで大丈夫そうです。

tests/bootstrap.php
<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

PHPUnitの設定」もそのままで大丈夫そうです。
colors="true" を加えると、いわゆる「グリーン」になりましたの意味が分かります。

tests/phpunit.xml
<phpunit bootstrap="./bootstrap.php">
    <testsuite name="Application Test Suite">
        <directory>./application</directory>
    </testsuite>
    <testsuite name="Library Test Suite">
        <directory>./library</directory>
    </testsuite>
    <filter>
        <!-- If Zend Framework is inside your project's library, uncomment this filter -->
        <!--
        <whitelist>
            <directory suffix=".php">../../library/Zend</directory>
        </whitelist>
        -->
    </filter>
</phpunit>

なお、「出力のための修正」としてエラーコントローラのコード修正に関して触れられていたのですが、ひとまず変更せずにそのままにしました。

テストコードを書いて動かしてみる

まだ書き方が分からないので、Indexコントローラのindexアクションの表示テストになると思われるコードだけ用意して動かしてみます。

tests/application/controllers/IndexControllerTest.php
<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        parent::setUp();
    }
    public function testIndexAction() {
        $this->dispatch('/');
        $this->assertController('index');
        $this->assertAction('index');
    }
}

プロジェクトディレクトリの tests 以下で、下記を実行します。

% phpunit application/controllers/IndexControllerTest.php
PHPUnit 3.7.22 by Sebastian Bergmann.
Configuration read from /Users/***/projects/sample/tests/phpunit.xml
.
Time: 0 seconds, Memory: 6.25Mb
OK (1 test, 2 assertions)

成功しました。
存在しないアクションを指定すると下記のようなエラーになりました。

% phpunit application/controllers/IndexControllerTest.php
PHPUnit 3.7.22 by Sebastian Bergmann.
Configuration read from /Users/***/projects/sample/tests/phpunit.xml
F
Time: 0 seconds, Memory: 6.50Mb
There was 1 failure:
1) IndexControllerTest::testIndexAction
Failed asserting last action used <"index"> was "foobar"
/usr/local/zend/share/ZendFramework/library/Zend/Test/PHPUnit/ControllerTestCase.php:1041
/Users/***/projects/sample/tests/application/controllers/IndexControllerTest.php:15
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.