草庐IT

PHPUnit : Assert a parameter when pass it to mock object

coder 2023-06-16 原文

对于下面的代码,

$mockObject->expects($this->at(0))
           ->method('search')
           ->with($searchConfig)
           ->will($this->returnValue([]));

这一行会自动断言确保当它调用方法 search 时它必须包含 $searchConfig 参数。在这种情况下,我们必须提供完全匹配的 $searchConfig 但有时它是数组还是对象就很难了。

是否有任何可能的方法让 PHPUnit 调用某些特定的方法来断言它包含我们想要的传入方法的参数?

例如,我可以创建闭包函数来断言,而不是使用 ->with() 方法

function ($config){
    $this->assertFalse(isset($config['shouldnothere']));
    $this->assertTrue($config['object']->isValidValue());
}

最佳答案

您可以使用 ->with($this->callback()) 并传入闭包以对参数执行更复杂的断言。

来自PHPUnit Docs

The callback() constraint can be used for more complex argument verification. This constraint takes a PHP callback as its only argument. The PHP callback will receive the argument to be verified as its only argument and should return TRUE if the argument passes verification and FALSE otherwise.

Example 10.13: More complex argument verification

getMock('Observer', array('reportError'));

    $observer->expects($this->once())
             ->method('reportError')
             ->with($this->greaterThan(0),
                    $this->stringContains('Something'),
                    $this->callback(function($subject){
                      return is_callable(array($subject, 'getName')) &&
                             $subject->getName() == 'My subject';
                    }));

    $subject = new Subject('My subject');
    $subject->attach($observer);

    // The doSomethingBad() method should report an error to the observer
    // via the reportError() method
    $subject->doSomethingBad();
} } ?>

所以你的测试会变成:

$mockObject->expects($this->at(0))
->method('search')
->with($this->callback(
    function ($config){
        if(!isset($config['shouldnothere']) && $config['object']->isValidValue()) {
            return true;
        }
        return false;
    })
->will($this->returnValue([]));

关于PHPUnit : Assert a parameter when pass it to mock object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21181038/

有关PHPUnit : Assert a parameter when pass it to mock object的更多相关文章

  1. xml - phpunit 测试 xml 输出 - 2

    我编写了一个API,它从另一个API获取数据并将其转换为XML。我如何使用phpunit来测试输出是否为预期的XML且是否有效?我应该创建一个包含所有节点的示例xml,然后根据它检查输出吗? 最佳答案 这是我认为可以批准的搜索结果,适用于遇到测试生成的xml供给器函数/类这个问题的任何人。有很多方法可以测试xml输出是否正确,有些方法比其他方法更容易。我最近完成了一个与OP此时询问的脚本类似的脚本。如果您从一个函数中得到以下部分xml输出(我们称之为$person->output()):bluecurly第一个想法是使用您必须生成x

  2. xml - 如何运行特定的 phpunit xml 测试套件? - 2

    我如何选择要执行的特定测试套件?$phpunit--configurationconfig.xml配置文件:librarylibrary/XXX/FormTest.phplibrary/XXX/Form 最佳答案 这是PHPUnit3.7.13的代码$phpunit--configurationconfig.xml--testsuiteLibrary$phpunit--configurationconfig.xml--testsuiteXXX_Form如果你想运行一组测试套件,那么你可以这样做librarylibrary/XXX/F

  3. phpunit 不能在 Windows 上工作 - 2

    我正在按照这些说明操作:WindowsGloballyinstallingthePHARinvolvesthesameprocedureasmanuallyinstallingComposeronWindows:CreateadirectoryforPHPbinaries;e.g.,C:\binAppend;C:\bintoyourPATHenvironmentvariable(relatedhelp)Downloadhttps://phar.phpunit.de/phpunit-6.2.pharandsavethefileasC:\bin\phpunit.pharOpenacomma

  4. 批处理文件中的 PHPUnit 设置 - 2

    我不喜欢使用PEAR方法,所以我决定使用另一种方法,但是,我仍然无法制作一个完美的.bat文件以在两种情况下运行。我下载了phpunit.phar和我添加到PATH中的路径=>C:\dev例如C:\dev\phpunit.phar我还在C:\dev\phpunit.bat中创建了一个phpunit.bat文件,这允许我在任何环境下运行phpunit地方。这是phpunit.bat的内容@echooffphpc:\dev\phpunit.phar当我使用guard-phpunit时,我的运行结果非常完美。但是,问题是当我想将参数/参数传递给phpunit时它不起作用。例如,我在C:\te

  5. php - 既然 PEAR 方法不再可行,如何为 WAMP 安装 PhpUnit? - 2

    所有说明都是关于PEARmethodwhichisnowdeprecated的.那么我们如何安装它才能使用WAMP从命令行运行phpUnit? 最佳答案 好的,PeterPopelyshko找到了解决方案。是documentedunderthewindowssection.我把它简化了一点:1)复制了phpunit.phar文件到php目录例如。c:\wamp\bin\php\php5.4.12\,因为php目录已经在我的PATH环境中了2)在同一目录中从命令行运行以下命令:echo@php"%~dp0phpunit.phar"%*

  6. windows - 如何在 Windows 上从 git bash 运行 PHPUnit? - 2

    我有Windows7,通过以下说明安装phpunit:https://phpunit.de/manual/current/en/installation.html并且在常规cmd终端上工作正常,但是phpunit在从git-scm安装的windowsgitbash终端中无法识别。我主要是一名Linux开发人员,所以在Windows上设置这些东西对我来说有点陌生。 最佳答案 使用aliasnative会好很多。编辑~/.bashrc,在其中添加这些:aliasphpunit="php/C/Tools/php/phpunit.phar"

  7. phpunit assertRegExp() 测试在 unix 中通过,在 windows 中失败 - 2

    我有一个PHPUnit测试,它使用正则表达式验证日志文件的输出。日志文件看起来像这样(第一行是空的,但我不知道如何在这里显示):lfworker01:-----------------------------------------------------------Lastupdate:2012-06-1411:43:17LastSegmentSent:2009-12-0223:25:00(1259792700)CurrentSegement:2009-12-0300:25:00(1259796300)ClicksprocessedSegment:3OpensessionsSegme

  8. windows - 麻烦在 Windows 7 上安装 phpunit,wamp 服务器 - 2

    我已经尝试在Windows7上安装phpunit,但我遇到了这样的问题:C:\wamp\bin\php\php5.3.13>pearchannel-discovercomponents.ez.noAddingChannel"components.ez.no"succeededDiscoveryofchannel"components.ez.no"succeededC:\wamp\bin\php\php5.3.13>pearchannel-discoverpear.phpunit.deChannel"pear.phpunit.de"isalreadyinitializedC:\wamp\

  9. php - 如何在 windows 上使用 composer 运行 phpunit 测试(cmd) - 2

    这个问题在这里已经有了答案:phpunitcommanddoesn'tworkforlaravel4onwindows7(11个答案)关闭7年前。我想在我的本地Windows机器上运行phpunit测试..(使用wamp)..我已经全局安装了composer..并且phpunit安装在我的本地目录..文件夹结构.._________tests||||______PagesTest.php||________vendor||______bin||_____phpunit在我的localroot(www)上..我创建了一个phpunit.xml文件.../tests/在Cmd上我试过:C:

  10. 可以使用 Netbeans + PHPUnit 在远程服务器上进行 PHP 单元测试吗? - 2

    我已经在本地Windows机器和远程Linux服务器上安装了PHPUnit。我能够从Netbeans在本地运行PHPUnit。我的下一步是使用Netbeans触发PHPUnit.bat脚本来:-SSH到远程服务器-从那里运行PHPUnit我可以手动使用PLink到SSH并从远程服务器的PHPUnit运行中取回输出。关于如何配置PHPUnit.bat脚本来执行此操作的任何想法? 最佳答案 如果这有帮助:用于从netbeans7.4远程运行phpunit的Windows脚本:phpunit.bat@echooff::==========

随机推荐