【问题标题】:Testing modular Zend Framework application with PHPUnit使用 PHPUnit 测试模块化 Zend Framework 应用程序
【发布时间】:2012-05-12 16:33:14
【问题描述】:

我有一个带有模块的 Zend Framework 应用程序,我想设置 PHPUnit 测试。

这是项目文件夹

- project/
   -application/
      - controllers/
          - indexController.php
      - modules/
         - mycore/
            - controllers/
                -  ActionsController.php
            - views/
                - ...
   - ...
   - tests/
      - application/
         - controllers/
            -IndexControllerTest.php
         - modules/
            - mycore/
                - controllers/
                   - ActionsControllerTest.php
      ControllerTestCase.php
      Bootstrap.php
      phpunit.xml

这是测试文件夹中每个安装文件的内容

ControllerTestCase.php

require_once 'Zend/Application.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
    protected $application

    public function setUp() {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }

    public function appBootstrap() {
        $this->application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini');
        $bootstrap = $this->application->getBootstrap()->bootstrap();

        $front = Zend_Controller_Front::getInstance();
        $front->setControllerDirectory(APPLICATION_PATH . '/controllers','default');
        $front->addModuleDirectory(APPLICATION_PATH . '/modules');

        return $bootstrap;
    }

    public function tearDown() {
        Zend_Auth::getInstance()->clearIdentity();
        $this->resetRequest();
        $this->resetResponse();
        parent::tearDown();
    }

    protected  function _doLogin($identity = null) {
        if ( $identity === null ) {
            $identity = $this->_generateFakeIdentity();
        }
        Zend_Auth::getInstance()->getStorage()->write( $identity );
    }

    protected function _generateFakeIdentity() {
        $identity = new stdClass();
        $identity->RecID                     = 3;
        $identity->user_firstname            = '****';
        $identity->user_lastname             = '********';
        $identity->confirmed                 = true;
        $identity->enabled                   = true;

        return $identity;
    }

    protected  function _doLogout() {
        Zend_Auth::getInstance()->clearIdentity();
    }
}

Bootstrap.php

error_reporting(E_ALL);
// 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') : 'development'));

// 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';

phpunit.xml

<phpunit bootstrap="./bootstrap.php" colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true" >
    <testsuite name="Application Test Suite">
        <directory>./</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>

这就是模块测试的内容

ActionsControllerTest.php

class Mycore_ActionsControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

    public $module;

    public function setUp()
    {
        $this->module = 'mycore';
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        $_SERVER['HTTP_HOST'] = 'unittest_host';
        $_SERVER['REQUEST_URI'] = '/';
        parent::setUp();
    }

    public function testIndexAction()
    {
        $this->dispatch("/");
        // assertions
        $this->assertModule('mycore');
        $this->assertController('actions');
        $this->assertAction('index');        
    }


}

结果如下:

Starting test 'IndexControllerTest::testIndexAction'.
.
Starting test 'Mycore_ActionsControllerTest::testIndexAction'.
F

Time: 1 second, Memory: 14.00Mb

There was 1 failure:

1) Mycore_ActionsControllerTest::testIndexAction
Failed asserting last module used <"default"> was "mycore"

/project/library/Zend/Test/PHPUnit/ControllerTestCase.php:929
/project/tests/application/modules/mycore/controllers/ActionsControllerTest.php:21

一个模块中的所有测试都工作正常,但是当我开始测试模块控制器时,我得到了这个错误。 我在整个互联网上搜索,但找不到解决此错误的方法,所以我希望有人能帮助我。

【问题讨论】:

    标签: unit-testing zend-framework phpunit


    【解决方案1】:

    你得到的不是错误,而是失败的测试。

    这可能是因为您正在调度到“/”,它在默认模块的默认控制器中查找默认操作。如果你派发到 '/mycore' 或 '/mycore/actions/index' 你可能会发现测试通过了。

    为了让您的测试在不更改的情况下通过,您需要change your default route 指向“/mycore/actions/index”。

    【讨论】:

    • 这解释了为什么这不能正常工作。但是我对我的测试用例做了一些更改,所以我向 /mycore/actions/index 进行了调度(通常应用程序应该重定向到 /user/login)。但是现在,如果我检查模块“mycore”控制器“操作”和操作“索引”,测试不会失败。我什至没有得到任何回应(没有 . 或 F 或 E )。我如何调试它以获得清晰的错误消息?提前致谢!
    • 表示测试通过。您的 phpunit.xml 中似乎没有设置任何日志记录。看看this as an example of how to set it up
    • 你没有得到一个'。'什么时候通过测试?索引控制器得到一个 .而这个没有任何共鸣。好的,我刚刚看到您的编辑,我会检查的。谢谢!
    • 我添加了登录部分,对于一般索引控制器,它看起来像是通过了(我得到 IndexController --> IndexControllerTest 但对于 Mycore_ActionsController 我什么都没有。所以没有通过测试?但是没有调试信息。我还使用了 phpunit 的可视化界面,这给了我这个错误“断言最后使用的模块失败 是“用户”,所以模块仍然有问题。是否还有一种方法可以获取您在 phpunit 的当前页面?所以我可以检查重定向是否顺利。谢谢!
    • 您可以像在任何 php 文件中一样在测试中回显您想要的任何内容。像echo "I'm here"; 这样的东西会出现在 PHPUnit 输出中。
    【解决方案2】:

    我有同样的问题。 对我来说,这是因为请求被重定向到“ErrorController”。 您可以通过评论您的 assertModule 行来检查这一点。 如果您遇到类似这样的错误“断言使用的最后一个控制器失败 是 "actions"',我们处于相同的情况。 这种错误可能有多种原因。 你必须抓住错误。 调度后,您可以像这样检索异常:

    $response = $this->getResponse();
    $exceptions = $response->getException();
    // do whatever you want, mail, log, print
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 2013-10-02
      • 2012-03-04
      • 2015-07-26
      相关资源
      最近更新 更多