Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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_Mocking_Phpunit - Fatal编程技术网

PhpUnit模拟一个内置函数

PhpUnit模拟一个内置函数,php,mocking,phpunit,Php,Mocking,Phpunit,是否有办法模拟/覆盖PHPUnit中的内置函数shell\u exec。我知道mockry,除了PHPUnit之外,我不能使用其他库。我已经尝试了3个多小时了,有些地方卡住了。如有任何提示/链接,将不胜感激。 我正在使用Zend-Framework 2有几个选项。例如,您可以在测试范围的名称空间中重新声明php函数shell\u exec 查看此伟大博客文章的参考: 您可以尝试使用包来模拟任何内置函数,包括自定义对象,如mocky。 比如说 \Badoo\SoftMocks::redefineF

是否有办法模拟/覆盖
PHPUnit
中的内置函数
shell\u exec
。我知道
mockry
,除了
PHPUnit
之外,我不能使用其他库。我已经尝试了3个多小时了,有些地方卡住了。如有任何提示/链接,将不胜感激。
我正在使用Zend-Framework 2

有几个选项。例如,您可以在测试范围的名称空间中重新声明php函数
shell\u exec

查看此伟大博客文章的参考:

您可以尝试使用包来模拟任何内置函数,包括自定义对象,如
mocky
。 比如说

\Badoo\SoftMocks::redefineFunction('strlen', '', 'return 5;');
这非常有用,特别是对于依赖于外部资源的内置函数。例如:

  • curl_exec
  • 获取\u dns\u记录

    • 非同构名称空间的答案

      正如@notundefined指出的,这里的解决方案取决于与被测试代码位于同一命名空间中的测试。下面是如何使用竞争名称空间完成相同的测试

      <?php
      
      namespace My\Namespace {
          /**
           * Override shell_exec() in the My\Namespace namespace when testing
           *
           * @return int
           */
          function shell_exec()
          {
              return // return your mock or whatever value you want to use for testing
          }
      }
      
      namespace My\Namespace\Tests {
          class SomeClassTest extends \PHPUnit_Framework_TestCase
          {
              public function testSomething()
              {
                  // The above override will be used when calling shell_exec 
                  // from My\Namespace\SomeClass::something() because the 
                  // current namespace is searched before the global.
                  // https://www.php.net/manual/en/language.namespaces.fallback.php
                  (new SomeClass())->something();
              }
          }
      }
      

      只有当测试脚本和实际脚本位于同一命名空间中时,您的方法才有效。。我的名称空间在单独的名称空间中。为此,测试的名称空间应该与SUT(测试中的系统)的名称空间匹配。这是一种常见的方法。也可阅读或参考。好方法;)您可以轻松地将模拟函数放入另一个文件中(与SUT具有相同的命名空间),然后该文件将包含在测试中。。。即使测试本身位于不同的命名空间下,也要使用此方法。也许你可以在回答“Wilt…”时加上这个。。。?
      
      <?php
      
      namespace My\Namespace {
          /**
           * Override shell_exec() in the My\Namespace namespace when testing
           *
           * @return int
           */
          function shell_exec()
          {
              return // return your mock or whatever value you want to use for testing
          }
      }
      
      namespace My\Namespace\Tests {
          class SomeClassTest extends \PHPUnit_Framework_TestCase
          {
              public function testSomething()
              {
                  // The above override will be used when calling shell_exec 
                  // from My\Namespace\SomeClass::something() because the 
                  // current namespace is searched before the global.
                  // https://www.php.net/manual/en/language.namespaces.fallback.php
                  (new SomeClass())->something();
              }
          }
      }