Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPUnit测试上未运行命名空间函数_Php_Unit Testing_Namespaces_Mocking_Phpunit - Fatal编程技术网

PHPUnit测试上未运行命名空间函数

PHPUnit测试上未运行命名空间函数,php,unit-testing,namespaces,mocking,phpunit,Php,Unit Testing,Namespaces,Mocking,Phpunit,我正在尝试使用以下名称空间测试覆盖内置php函数: 原始类别: 就好像忽略了带名称空间的函数,而它只是调用内置函数,我是否遗漏了什么?根据您的代码示例,您定义了名称空间My\namespace\Test\Unit\Console\Command中存在的函数文件: 当然,您实际上从未重写根命名空间中存在的函数文件_ 据我所知,你不能那样做。无论何时尝试定义已存在的函数,都会触发致命错误,请参阅 但是,如果您想要实现的是断言OverrideCommand::myFileExists在文件存在时返回t

我正在尝试使用以下名称空间测试覆盖内置php函数:

原始类别:


就好像忽略了带名称空间的函数,而它只是调用内置函数,我是否遗漏了什么?

根据您的代码示例,您定义了名称空间My\namespace\Test\Unit\Console\Command中存在的函数文件:

当然,您实际上从未重写根命名空间中存在的函数文件_

据我所知,你不能那样做。无论何时尝试定义已存在的函数,都会触发致命错误,请参阅

但是,如果您想要实现的是断言OverrideCommand::myFileExists在文件存在时返回true,在文件不存在时返回false,则可以执行以下操作之一

参考测试中存在和不存在的文件 模拟文件系统 用于模拟文件系统


注意:对于你的例子,我推荐前者。

你试过猴子贴片吗?感谢我监督了这一细节,我最终能够在测试中轻松更改内置函数的名称空间。这只是一个使用file_exists的孤立示例,但我在整个代码中也使用了其他函数,因此vfstream对于我的案例来说可能是一个过度使用。
<?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);
    }
}
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.
namespace My\Namespace\Test\Unit\Console\Command;

function file_exists($path)
{
    return true;
}
public function testMyFileExistsReturnsFalseIfFileDoesNotExist()
{
     $command = new OverrideCommand();

     $this->assertTrue($command->myFileExists(__DIR__ . '/NonExistentFile.php');
}

public function testMyFileExistsReturnsTrueIfFileExists()
{
     $command = new OverrideCommand();

     $this->assertTrue($command->myFileExists(__FILE__);
}