php+中文头文件,使用PHPUnit测试PHP头文件

我正在尝试使用PHPunit来测试输出一些自定义标头的类.

问题是在我的机器上这个:

class HeadersTest extends PHPUnit_Framework_TestCase {

public function testHeaders()

{

ob_start();

header('Location: foo');

$headers_list = headers_list();

header_remove();

ob_clean();

$this->assertContains('Location: foo', $headers_list);

}

}

甚至这个:

class HeadersTest extends PHPUnit_Framework_TestCase {

public function testHeaders()

{

ob_start();

header('Location: foo');

header_remove();

ob_clean();

}

}

返回此错误:

name@host [~/test]# phpunit --verbose HeadersTest.php

PHPUnit 3.6.10 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 2.25Mb

There was 1 error:

1) HeadersTest::testHeaders

Cannot modify header information - headers already sent by (output started at /usr/local/lib/php/PHPUnit/Util/Printer.php:173)

/test/HeadersTest.php:9

FAILURES!

Tests: 1, Assertions: 0, Errors: 1.

这看起来好像在测试运行之前还有其他东西输出到终端,即使没有包含其他文件且在PHP标记开头之前没有其他字符.它可能是PHPunit中导致这种情况的东西吗?

问题可能是什么?

解决方法:

问题是PHPUnit会在屏幕上打印标题,此时您无法添加更多标题.

解决方法是在一个独立的过程中运行测试.这是一个例子

class FooTest extends PHPUnit_Framework_TestCase

{

/**

* @runInSeparateProcess

*/

public function testBar()

{

header('Location : http://foo.com');

}

}

这将导致:

$phpunit FooTest.php

PHPUnit 3.6.10 by Sebastian Bergmann.

.

Time: 1 second, Memory: 9.00Mb

OK (1 test, 0 assertions)

关键是@runInSeparateProcess注释.

如果您正在使用PHPUnit~4.1或其他东西并得到错误:

PHP Fatal error: Uncaught Error: Class 'PHPUnit_Util_Configuration' not found in -:378

Stack trace:

#0 {main}

thrown in - on line 378

Fatal error: Uncaught Error: Class 'PHPUnit_Util_Configuration' not found in - on line 378

Error: Class 'PHPUnit_Util_Configuration' not found in - on line 378

Call Stack:

0.0013 582512 1. {main}() -:0

尝试将此添加到您的引导程序文件以修复它:

if (!defined('PHPUNIT_COMPOSER_INSTALL')) {

define('PHPUNIT_COMPOSER_INSTALL', __DIR__ . '/path/to/composer/vendors/dir/autoload.php');

}

标签:output-buffering,php,unit-testing,phpunit

来源: https://codeday.me/bug/20190925/1817292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值