【问题标题】:Raw PHP in PHPUnit test responsePHPUnit 测试响应中的原始 PHP
【发布时间】:2011-10-29 04:54:25
【问题描述】:

我已经设法通过我的 Zend Framework 项目设置 PHPUnit,使用 Phing 在构建时通过 Eclipse 外部工具执行测试。我的测试是在构建时找到并执行的,但由于某种原因,页面中的 PHP 没有被解释。

为了弄清楚我的断言失败的原因,我在我的测试中添加了:echo $this->response->outputBody();,并意识到它与原始 PHP 相呼应。如果我添加 echo get_class($this->response); 我会得到 Zend_Controller_Response_HttpTestCase 作为类名,从我收集的信息来看是正确的。

我在 Apache 上遇到过这个问题,其中文件处理程序未设置为通过 PHP 可执行文件发送 PHP 代码,但据我所知,这应该不是问题,因为 Phing/PHPUnit 应该处理执行所有内容直接通过可执行文件。我对 PHPUnit 或 Phing 的工作原理还不够了解,无法知道我可能做错了什么。有什么建议吗?

这是我的测试类:

<?php

class SearchControllerTest extends ControllerTestCase
{

    public function setUp()
    {
        parent::setUp();
    }

    public function testSearchPizzaChicago()
    {
        $this->_search('restaurants', 'chicago, il');
    }


    private function _search($what, $where)
    {
        $this->request->setMethod('POST');

        $this->request->setPost(
                        array(
                            'search_what' => $what,
                            'search_where' => $where,
                        )
        );

        $this->dispatch(Zend_Registry::get('base_url') . '/search/results');

        echo $this->response->outputBody();

        //echo get_class($this->response);

        $this->assertQuery('#results');
    }

    public function tearDown()
    {
        /* Tear Down Routine */
    }


}

这是我运行其中一项测试时的控制台输出:

Buildfile: C:\workspace\myproject\build.xml

myproject > test:

... raw PHP code here ...

  [phpunit] Testsuite: SearchControllerTest
  [phpunit] Tests run: 1, Failures: 1, Errors: 0, Incomplete: 0, Skipped: 0, Time elapsed: 0.25874 s
  [phpunit] testSearchPizzaChicago FAILED
  [phpunit] Failed asserting node DENOTED BY #results EXISTS
  [phpunit] C:\workspace\frameworks\ZendFramework-1.11\library\Zend\Test\PHPUnit\Constraint\DomQuery.php:263
  [phpunit] C:\workspace\frameworks\ZendFramework-1.11\library\Zend\Test\PHPUnit\ControllerTestCase.php:300
  [phpunit] C:\workspace\myproject\tests\application\controllers\SearchControllerTest.php:34
  [phpunit] C:\workspace\myproject\tests\application\controllers\SearchControllerTest.php:13
  [phpunit] C:\wamp\bin\php\php5.3.5\PEAR\phing.php:37

BUILD FINISHED

Total time: 2.0895 seconds

【问题讨论】:

  • 您是否在输出的原始代码中使用短标签( ?> 或 = ?>)?如果您的服务器未设置 php 短标签,则不会将它们作为 PHP 处理,您将遇到类似于此的问题

标签: php eclipse zend-framework phpunit phing


【解决方案1】:

您正在执行一个 POST 请求 - 如果您的网络服务器运行正常,它将永远不会提供原始 PHP 代码。检查/search/results 脚本以查看它是否正在实际执行。由于您正在获取 php 代码,因此很可能不会将其视为/视为 php 脚本,而是将其内容作为纯文本提供。

【讨论】:

  • 感谢您的回复。部分问题是它没有通过网络服务器,所以它没有出现在我的日志中。原来我关闭了错误,原始 PHP 是代码中的错误导致的,直到我打开错误才发现。测试正在进行中。
猜你喜欢
  • 2013-11-10
  • 2023-03-15
  • 2020-03-10
  • 1970-01-01
  • 2021-03-20
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
相关资源
最近更新 更多