5

Zend Framework / Doctrine 2.0アプリケーションの単体テストを作成したかったのですが、ZFで単体テストを設定する方法がよくわかりません。また、これらの単体テストにDoctrine2.0を含めたいと思います。これを設定するにはどうすればよいですか?例を挙げていただけますか?

ありがとうございました

4

1 に答える 1

2

単体テストをセットアップするために、phpunit (phpunit.xml) の構成ファイルとテスト ディレクトリに TestHelper.php を作成しました。構成は基本的に、どの単体テストを実行する必要があり、どのフォルダーとファイルをカバレッジでスキップする必要があるかを phpunit に伝えます。私の設定では、実行されるのはアプリケーションとライブラリフォルダー内のすべての単体テストファイルです。

Testhelper は、すべての単体テストで拡張する必要があります。

phpunit.xml

<phpunit bootstrap="./TestHelper.php" colors="true">
    <testsuite name="Your Application">
        <directory>./application</directory>
        <directory>./library</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../application/</directory>
            <directory suffix=".php">../library/App/</directory>
            <exclude>
                <directory suffix=".phtml">../application/</directory>
                <directory suffix=".php">../application/database</directory>
                <directory suffix=".php">../application/models/Entities</directory>
                <directory suffix=".php">../application/models/mapping</directory>
                <directory suffix=".php">../application/models/proxy</directory>
                <directory suffix=".php">../application/views</directory>
                <file>../application/Bootstrap.php</file>
                <file>../application/modules/admin/controllers/ErrorController.php</file>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/report" title="PrintConcept" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
        <log type="testdox" target="./log/testdox.html" />
    </logging>
</phpunit>

TestHelper.php

<?php
error_reporting(E_ALL | E_STRICT);

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define testing application environment
define('APPLICATION_ENV', 'testing');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/**
 * Zend_Application
 */
require_once 'Zend/Application.php';

/**
 * Base Controller Test Class
 *
 * All controller test should extend this
 */
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class BaseControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{   
    public function setUp()
    {
        $application = new Zend_Application(APPLICATION_ENV,
                             APPLICATION_PATH . '/configs/application.ini');
        $this->bootstrap = array($application->getBootstrap(), 'bootstrap');

        Zend_Session::$_unitTestEnabled = true;

        parent::setUp();
    }

    public function tearDown()
    {
        /* Tear Down Routine */
    }
}

これは、ZF と PHPunit の初期設定のみをカバーしています。

于 2010-08-26T07:28:36.303 に答える