<bdo id='1bSwk'></bdo><ul id='1bSwk'></ul>
<i id='1bSwk'><tr id='1bSwk'><dt id='1bSwk'><q id='1bSwk'><span id='1bSwk'><b id='1bSwk'><form id='1bSwk'><ins id='1bSwk'></ins><ul id='1bSwk'></ul><sub id='1bSwk'></sub></form><legend id='1bSwk'></legend><bdo id='1bSwk'><pre id='1bSwk'><center id='1bSwk'></center></pre></bdo></b><th id='1bSwk'></th></span></q></dt></tr></i><div id='1bSwk'><tfoot id='1bSwk'></tfoot><dl id='1bSwk'><fieldset id='1bSwk'></fieldset></dl></div>

      <small id='1bSwk'></small><noframes id='1bSwk'>

      <tfoot id='1bSwk'></tfoot>
    1. <legend id='1bSwk'><style id='1bSwk'><dir id='1bSwk'><q id='1bSwk'></q></dir></style></legend>

      1. PhpUnit 模拟一个内置函数

        时间:2023-10-31

          <i id='rrkYE'><tr id='rrkYE'><dt id='rrkYE'><q id='rrkYE'><span id='rrkYE'><b id='rrkYE'><form id='rrkYE'><ins id='rrkYE'></ins><ul id='rrkYE'></ul><sub id='rrkYE'></sub></form><legend id='rrkYE'></legend><bdo id='rrkYE'><pre id='rrkYE'><center id='rrkYE'></center></pre></bdo></b><th id='rrkYE'></th></span></q></dt></tr></i><div id='rrkYE'><tfoot id='rrkYE'></tfoot><dl id='rrkYE'><fieldset id='rrkYE'></fieldset></dl></div>
            <tbody id='rrkYE'></tbody>
              <tfoot id='rrkYE'></tfoot>

              <small id='rrkYE'></small><noframes id='rrkYE'>

            1. <legend id='rrkYE'><style id='rrkYE'><dir id='rrkYE'><q id='rrkYE'></q></dir></style></legend>

                  <bdo id='rrkYE'></bdo><ul id='rrkYE'></ul>
                  本文介绍了PhpUnit 模拟一个内置函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法在 PHPUnit 中模拟/覆盖内置函数 shell_exec.我知道 Mockery 并且我不能使用除了 PHPUnit 之外的其他库.我已经尝试了超过 3 小时并且卡在了某个地方.任何指针/链接将不胜感激.我正在使用 Zend-framework2

                  Is there a way to mock/override an inbuilt function shell_exec in PHPUnit. I am aware of Mockery and I cannot use other libraries apart from PHPUnit.I have tried for more than 3hrs and somewhere stuck. Any pointers / links would be highly appreciated. I am using Zend-framework2

                  推荐答案

                  有几个选项.例如,您可以在测试范围的命名空间中重新声明 php 函数 shell_exec.

                  There are several options. You could for example redeclare the php function shell_exec in the namespace of your test scope.

                  查看这篇很棒的博客文章以获取参考:PHP:模拟"单元测试中的 time() 等内置函数.

                  Check for reference this great blog post: PHP: "Mocking" built-in functions like time() in Unit Tests.

                  <php
                  namespace MyNamespace;
                  
                  /**
                   * Override shell_exec() in current namespace for testing
                   *
                   * @return int
                   */
                  function shell_exec()
                  {
                      return // return your mock or whatever value you want to use for testing
                  }
                   
                  class SomeClassTest extends PHPUnit_Framework_TestCase
                  { 
                      /*
                       * Test cases
                       */
                      public function testSomething()
                      {
                          shell_exec(); // returns your custom value only in this namespace
                          //...
                      }
                  }
                  

                  现在,如果您在 MyNamespace 的类中使用全局 shell_exec 函数,它将使用您的自定义 shell_exec 函数.

                  Now if you used the global shell_exec function inside a class in the MyNamespace it will use your custom shell_exec function instead.

                  您还可以将模拟函数放在另一个文件中(与 SUT 具有相同的命名空间)并将其包含在测试中.像这样,如果测试具有不同的命名空间,您也可以模拟该函数.

                  You can also put the mocked function in another file (with the same namespace as the SUT) and include it in the test. Like that you can also mock the function if the test has a different namespace.

                  这篇关于PhpUnit 模拟一个内置函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHPUnit:如何使用多个参数模拟多个方法调用? 下一篇:是否可以在 PhpUnit 的测试用例之外创建一个模拟?

                  相关文章

                    <i id='eMVUQ'><tr id='eMVUQ'><dt id='eMVUQ'><q id='eMVUQ'><span id='eMVUQ'><b id='eMVUQ'><form id='eMVUQ'><ins id='eMVUQ'></ins><ul id='eMVUQ'></ul><sub id='eMVUQ'></sub></form><legend id='eMVUQ'></legend><bdo id='eMVUQ'><pre id='eMVUQ'><center id='eMVUQ'></center></pre></bdo></b><th id='eMVUQ'></th></span></q></dt></tr></i><div id='eMVUQ'><tfoot id='eMVUQ'></tfoot><dl id='eMVUQ'><fieldset id='eMVUQ'></fieldset></dl></div>
                  1. <legend id='eMVUQ'><style id='eMVUQ'><dir id='eMVUQ'><q id='eMVUQ'></q></dir></style></legend>
                      <tfoot id='eMVUQ'></tfoot>

                      <small id='eMVUQ'></small><noframes id='eMVUQ'>

                        <bdo id='eMVUQ'></bdo><ul id='eMVUQ'></ul>