【发布时间】:2017-11-27 13:33:16
【问题描述】:
我正在尝试使用这样的命名空间测试覆盖内置的 php 函数:
原始类:
<?php
namespace My\Namespace;
class OverrideCommand
{
public function myFileExists($path)
{
return file_exists($path);
}
}
单元测试
<?php
namespace My\Namespace\Test\Unit\Console\Command;
function file_exists($path)
{
return true;
}
class OverrideCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var OverrideCommand
*/
protected $command;
protected function setUp()
{
$this->command = new \My\Namespace\OverrideCommand();
}
public function testMyFileExists()
{
$result = $this->command->myFileExists('some/path/file.txt');
$this->assertTrue($result);
}
}
在这种情况下,我的测试中的 file_exists 函数应该总是返回 true,但是当我运行 PHPUnit 时,我得到:
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
There was 1 failure:
1) My\Namespace\Test\Unit\Console\Command\OverrideCommandTest::testMyFileExists
Failed asserting that false is true.
就好像命名空间函数被忽略了,它只是调用内置函数,我错过了什么吗?
【问题讨论】:
-
你试过猴子补丁吗? stackoverflow.com/a/12128017/674033
标签: php unit-testing namespaces mocking phpunit