PHPUnit的安装与Zend测试(Setup PHPUnit with Zend Test)

2019-07-31 03:02发布

我想开始使用PHPUnit的与Zend测试我的Zend框架应用程序。 我能运行在命令行PHPUnit命令phpunit --configuration phpunit.xml 。 我试过下面这个教程是基于关闭马修纬二路O'Phinney的的博客文章 。 当PHPUnit的尝试写入日志文件,我得到一个错误。 这里是我的phpunit.xml

<phpunit bootstrap="./Bootstrap.php" colors="true">
    <testsuite name="Zend Framework Tests">
        <directory>./</directory>
    </testsuite>
    <!-- Optional filtering and logging settings -->
    <filter>
        <whitelist>
            <directory suffix=".php">../library/</directory>
            <directory suffix=".php">../application/</directory>
            <exclude>
                <directory suffix=".phtml">../application/</directory>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html"/>
    </logging>
</phpunit>

我的测试引导:

<?php
//Set app paths and environment
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
define('TEST_PATH', BASE_PATH . '/tests');
define('APPLICATION_ENV', 'testing');
//Set include path
set_include_path('.' . PATH_SEPARATOR . BASE_PATH . '/library' . PATH_SEPARATOR . get_include_path());
//Set the default timezone
date_default_timezone_set('America/Chicago');
?>

而我ControllerTestCase,我想我的测试控制器扩展:

<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $_application;

    public function setUp()
    {
        //Override the parent to solve an issue with not finding the correct module
        $this->bootstrap = new Zend_Application(
            APPLICATION_ENV, 
            APPLICATION_PATH . '/configs/application.ini'
        );
        parent::setUp();
    }
}
?>

我得到的时候PHPUnit的尝试写入日志文件的错误是: Fatal error: Class 'Symfony\Component\Console\Command\Command' not found in C:\repositories\myfirstzend.com\includes\library\Doctrine\DBAL\Tools\Console\Command\ImportCommand.php on line 38

在我做错任何线索? 我在PHP 5.4,Windows 7的XAMPP 8.0,梨是最新的,和我有最新的PHPUnit。

更新如果我改变我的bootstrap.php从马修纬二路O'Phinney的博客如下:

<?php
/*
 * Start output buffering
 */
ob_start();

/*
 * Set error reporting to the level to which code must comply.
 */
error_reporting( E_ALL | E_STRICT );

/*
 * Set default timezone
 */
date_default_timezone_set('GMT');

/*
 * Testing environment
 */
define('APPLICATION_ENV', 'testing');

/*
 * Determine the root, library, tests, and models directories
 */
$root        = realpath(dirname(__FILE__) . '/../');
$library     = $root . '/library';
$tests       = $root . '/tests';
$models      = $root . '/application/models';
$controllers = $root . '/application/controllers';

/*
 * Prepend the library/, tests/, and models/ directories to the
 * include_path. This allows the tests to run out of the box.
 */
$path = array(
    $models,
    $library,
    $tests,
    get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $path));

/**
 * Register autoloader
 */
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

/**
 * Store application root in registry
 */
Zend_Registry::set('testRoot', $root);
Zend_Registry::set('testBootstrap', $root . '/application/bootstrap.php');

/*
 * Unset global variables that are no longer needed.
 */
unset($root, $library, $models, $controllers, $tests, $path);

我继续获取有关的Symfony从学说的错误。 我保证我安装pear.symfony.com/Yaml为好。 所以还是坏。

如果我删除从我app.ini学说为参考,我测试的应用程序(这意味着它不会被加载),我仍然得到错误。 它几乎感觉像装载机为三个部分(PHPUnit的,ZF,教义)是各自为战。 有没有解决的办法?

第二次更新:我降级PHPUnit来3.4.15,我仍然有这个问题。 我的下一步是去从PHP 5.4至5.3.x.

第三次更新:我现在是在PHP 5.3.10和我看到了同样的错误。

如果您需要了解更多信息,请让我知道。

Answer 1:

我失去了在引导文件中的应用程序的加载。

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

为了让你对我有什么在我的测试/ bootstrap.php中的想法(这是自动生成的,因为释放1.11.4 Zend的工具)

<?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();

正如Zend框架手册上提到,最好是用PHPUnit的3.4.15虽然我可以运行使用PHPUnit的3.6.12因为我隔离我的测试集中于我的业务逻辑,而不是Zend框架的逻辑我的测试。

我还修改了phpunit.xml以及到以下几点:

<phpunit bootstrap="./bootstrap.php" colors="true">
    <testsuite name="Application Test Suite">
        <directory>./application</directory>
    </testsuite>
    <testsuite name="Library Test Suite">
        <directory>./library</directory>
    </testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">../../library</directory>
            <directory suffix=".php">../../application</directory>
            <exclude>
                <directory suffix=".php">../../library/Zend</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

我希望这可以解决许多你现在面临的问题的。

最好的祝福,

米开朗基罗



文章来源: Setup PHPUnit with Zend Test