在zend单元测试中需要帮助 [英] need help for the zend unit test

查看:74
本文介绍了在zend单元测试中需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用单元测试来测试zend项目, 这是application.ini

I am using unit test to test zend project, this is application.ini

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
resources.frontController.params.displayExceptions = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"


; modules
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "layout"
resources.view[] =
resources.view.doctype = "XHTML1_STRICT"

; database
resources.db.adapter = "PDO_MYSQL"
resources.db.params.dbname = "demodev"
resources.db.params.password = "demo_core_pass"
resources.db.params.username = "demo_core_user"
resources.db.params.host = "localhost"
resources.db.isDefaultTableAdapter = true

; session
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.use_only_cookies = true
;resources.session.remember_me_seconds = 10

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

[amsterdam : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

这是phpunit.xml:

this is the phpunit.xml:

<phpunit bootstrap="./application/bootstrap.php"  colors="true">         
<testsuite>
    <directory>./</directory>       
</testsuite>
<filter>
    <whitelist>
        <directory suffix="MyApp.php">../application/</directory>
        <exclude>
            <file>../application/Bootstrap.php</file>
            <file>../application/controllers/ErrorController.php</file>
            <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>         

这是bootstrap.php

this is the bootstrap.php

<?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') : 'testing'));

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

require_once 'Zend/Application.php';
require_once 'ControllerTestCase.php';
//require_once 'MyApp.php';

这是ControllerTestCase.php

this is the ControllerTestCase.php

<?php
require_once 'Zend/Application.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');

        $this->application->bootstrap();

    }
}

这是IndexControllerTest.php

this is the IndexControllerTest.php

<?php

require_once'Zend/Test/PHPUnit/ControllerTestCase.php'; IndexControllerTest类扩展了ControllerTestCase {

require_once 'Zend/Test/PHPUnit/ControllerTestCase.php'; class IndexControllerTest extends ControllerTestCase {

public function testHomePage() {
    $this->dispatch('/index');
    $this->assertController('index');
    $this->assertAction('menu');
}

}

当我进入测试文件夹时,运行命令phpunit,它给我这个错误:

when I go the the tests folder, run command phpunit, it gives me this error:

D:\PHP\apache2\htdocs\demo_src\tests>phpunit
PHPUnit 3.5.13 by Sebastian Bergmann.

PHP Fatal error:  Call to a member function hasResource() on a non-object in D:\
PHP\apache2\htdocs\demo_src\application\controllers\ErrorController.php on line
46

Fatal error: Call to a member function hasResource() on a non-object in D:\PHP\a
pache2\htdocs\demo_src\application\controllers\ErrorController.php on line 46

如何解决此问题?

当我将IndexControllerTest更改为一项测试时,例如:

When I change the IndexControllerTest as one test, such as:

<?php


class IndexControllerTest extends ControllerTestCase
{

    public function testHomePage() {
        $this->assertTrue(true);

    }


}

它可以工作,但是当我将其更改为

it works, but when I change it to

<?php

class IndexControllerTest extends ControllerTestCase
{

    public function testHomePage() {
        $this->dispatch('/');

    }
}

ErrorController.php中的46行是:

The 46 line in ErrorController.php is:

public function getLog() {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {// this is line 46
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }

它仍然给我同样的错误,有什么建议吗?

it still give me the same error, any suggestion?

推荐答案

尝试使用此IndexControllerTest类

Try with this IndexControllerTest class

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 testHomePage()
    {
        $params = array(
            'action' => 'index',
            'controller' => 'index',
            'module' => 'default'
        );
        $url = $this->url($this->urlizeOptions($params));
        $this->dispatch($url);

        // assertions
        $this->assertModule($params['module']);
        $this->assertController($params['index']);
        $this->assertAction($params['action']);
    }

}

这篇关于在zend单元测试中需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆