使用 phpunit 进行测试

发布于 2024-11-25 12:11:58 字数 4065 浏览 4 评论 0原文

我正在尝试测试我的控制器。 我遵循了这个指南:

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

一切正常,直到我想在我的测试函数中使用此调用: $this->dispatch('/'); 我认为问题出在引导上,但我不知道如何解决这个问题。 我粘贴测试中涉及的文件:

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

请有人帮助我。 谢谢

I'm trying to test my controllers.
I followed this guide:

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

Everything was ok until I wanted to use this call in my test function:
$this->dispatch('/');
I thing the problem is with bootstrapping, but I don't know how to resolve this.
I paste my files involved in the testing:

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');
      }
}

And here is my phpunit output:

$ 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

Can somebody help me, please.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-12-02 12:11:58

我的猜测是,您没有 URL /core/index/index 的路由,因此 ZF 正在重定向到您的错误控制器。接下来,ErrorController.php 有一个错误,导致它对不包含对象或为 null 的变量调用 hasResource() 方法代码>.如果首先修复错误控制器,诊断问题可能会更容易。

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

My guess is that you don't have a route for the URL /core/index/index, and so ZF is redirecting to your error controller. Next, ErrorController.php has a bug that causes it to call the hasResource() method on a variable that doesn't hold an object or is null. It will probably be easier to diagnose the issue if you fix the error controller first.

This is precisely why I built separate abstract test-case classes for controllers and views. I don't like that Zend's ControllerTestCase tests the route, dispatcher, and views in addition to the controller.

奢欲 2024-12-02 12:11:58

如果我们可以看到 dispatch() 方法中的代码,那就更容易了,但我可以假设 setUp() 中的某处可能触发了错误> (也许在引导程序中),吞掉异常。之后,它尝试调用 tearDown 以及 resetRequest() 中使用的属性(可能是 $this->application$this ->bootstrap 在安装过程中从未初始化,导致 PHP 错误,

我发布了一个 答案解释何时会发生这种类型或错误。我知道这是一篇旧文章,但如果它可以帮助其他人。

If we could see the code in the dispatch() method, it would be easier, but I can assume that there is probably an error that was triggered somewhere in the setUp() (maybe in the bootstrap), swallowing the exception. After that it tried to call tearDown and the attributes used in resetRequest() (maybe $this->application or $this->bootstrap where never initialized in setup resulting in a PHP Error.

I posted an answer explaining when this kind or error can occur. I know its an old post, but if it can help someone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文