2011-07-21 226 views
0

我想测试我的控制器。 我遵循这个指南:用phpunit测试

http://www.zendcasts.com/unit-testing-a ... S/2010/11/

一切顺利,直到我想在我的测试功能,使用此电话: $这个 - >讯( '/') ; 我的问题是引导,但我不知道如何解决这个问题。 我贴参与测试我的文件:

的application.ini = http://pastie.org/2248669

phpunit.xml =

<phpunit bootstrap="./application/bootstrap.php" colors="true"> 
<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">../application</directory> 
    <directory suffix=".php">../library/Kolcms</directory> 
    <exclude> 
     <directory suffix=".php">../library/Zend</directory> 
     <directory suffix=".php">../library/Kolcms/Model/DB</directory> 
     <directory suffix=".phtml">../library/</directory> 
     <directory suffix=".phtml">../application/</directory> 
     <file suffix=".php">../application/Bootstrap.php</file> 
    </exclude> 
    </whitelist> 

</filter> 

<logging> 
    <log type="coverage-html" target="./log/report" charset="UTF-8" 
    yui="true" highlight = "true" lowUpperBound="50" highLowerBound="80" /> 
</logging> 

的bootstrap.php =

<?php 

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

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

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


/** Zend_Application */ 
require_once 'Zend/Application.php'; 
require_once 'ControllerTestCase.php'; 
// Create application, bootstrap, and run 
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini' 
); 
$application->bootstrap(); 
clearstatcache(); 

ControllerTestCase .php =

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

class ControllerTestCase 
extends Zend_Test_PHPUnit_ControllerTestCase 
{ 

/** 
* 
* @var Zend_Application 
*/ 

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

} 

public function tearDown() 
{ 
    $this->resetRequest(); 
    $this->resetResponse(); 

    parent::tearDown(); 
} 

} 

indexControllerTest.php =

<?php 
include_once APPLICATION_PATH . '/modules/core/controllers/IndexController.php'; 
class Core_IndexControllerTest extends ControllerTestCase 
{ 
     public function testDefaultShouldInvokeAction() 
     { 
      $this->dispatch('/core/index/index'); 
      $this->assertController('index'); 
      $this->assertAction('index'); 
      $this->assertModule('core'); 
     } 
} 

这里是我的PHPUnit的输出:

$ phpunit 
PHPUnit 3.5.5 by Sebastian Bergmann. 

PHP Fatal error: Call to a member function hasResource() on a non-object in 
/home/aykut/dev/kolonicms/application/modules/core/controllers/ErrorController.php on 
line 49 

Fatal error: Call to a member function hasResource() on a non-object in 
/home/aykut/dev/kolonicms/application/modules/core/controllers/ErrorController.php 
on line 49 

有人可以帮我请。 谢谢

回答

1

我的猜测是,你没有一个路由网址/core/index/index,所以ZF重定向到你的错误控制器。接下来,ErrorController.php有一个错误,导致它在不保存对象的变量或null上调用hasResource()方法。如果您先修复错误控制器,则诊断问题可能会更容易。

这正是我为什么为控制器和视图构建单独的抽象测试用例类的原因。我不喜欢Zend的ControllerTestCase除控制器外还测试路由,调度程序和视图。

0

如果我们可以看到dispatch()方法中的代码,它会更容易,但我可以假设有可能是在setUp()(可能是在引导程序)中的某处触发的错误,吞下该异常。之后,它试图打电话tearDown并导致PHP错误的位置在设置从未初始化在resetRequest()(也许$this->application$this->bootstrap使用的属性。

我发布了一个answer说明在这种或错误可能发生。我知道它的旧帖子,但是如果它可以帮助别人。