前面有了解到phpunit的with可以使得断言一个方法被调用时候所接收的每个参数,但是那种断言是只能断言一次的,就是断言某次执行的。
那么withConsecutive则能断言多次,其接收的是数组为参数,每个数组中的所有元素代表本次调用的方法的参数的断言。那么多次调用的断言就是多个数组了。
其具体实例如:
SubjectTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class SubjectTest extends TestCase
{
public function testFunctionCalledTwoTimesWithSpecificArguments(): void
{
//获取模仿对象构造并为其模仿一个set方法
$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['set'])
->getMock();
//断言这里set方法会被调用两次
//并且断言第一次执行的传入参数是foo和一个大于0的数
//并且断言第二次执行的传入参数是bar和一个大于0的数
$mock->expects($this->exactly(2))
->method('set')
->withConsecutive(
[$this->equalTo('foo'), $this->greaterThan(0)],
[$this->equalTo('bar'), $this->greaterThan(0)]
);
$mock->set('foo', 0);
$mock->set('bar', 48);
}
}
由于这里执行第一个set方法时候其第二个参数是0,并不满足断言中大于0的设想。所以运行结果:
C:\Users\Administrator\PhpstormProjects\untitled\organizing>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\SubjectTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 00:00.004, Memory: 20.00 MB
There was 1 failure:
1) SubjectTest::testFunctionCalledTwoTimesWithSpecificArguments
Warning: include(PHPUnit\Composer\Autoload\ClassLoader.php): failed to open stream: No such file or directory in C:\Users\Administrator\PhpstormProjects\untitled\organizing\src\autoload.php on line 3
Call Stack:
0.0149 1571792 1. {main}() D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:0
0.1327 18893800 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:2249
0.1327 18893912 3. PHPUnit\TextUI\Command->run(array(2), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:93
0.1375 19071600 4. PHPUnit\TextUI\TestRunner->run(class PHPUnit\Framework\TestSuite, array(15), array(0), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:124
0.1661 19126464 5. PHPUnit\TextUI\DefaultResultPrinter->printResult(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/TestRunner.php:496
0.1664 19126488 6. PHPUnit\TextUI\DefaultResultPrinter->printFailures(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:154
0.1664 19126488 7. PHPUnit\TextUI\DefaultResultPrinter->printDefects(array(1), string(7)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:299
0.1701 19126488 8. PHPUnit\TextUI\DefaultResultPrinter->printDefect(class PHPUnit\Framework\TestFailure, long) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:272
0.1706 19126488 9. PHPUnit\TextUI\DefaultResultPrinter->printDefectTrace(class PHPUnit\Framework\TestFailure) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:279
0.1706 19126488 10. PHPUnit\Framework\ExpectationFailedException->__toString() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:288
0.1706 19126712 11. PHPUnit\Util\Filter::getFilteredStacktrace(class PHPUnit\Framework\ExpectationFailedException) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Framework/Exception/Exception.php:58
0.1706 19127848 12. PHPUnit\Util\Filter::shouldPrintFrame(array(2), string(25), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:57
0.1706 19127992 13. PHPUnit\Util\Filter::fileIsExcluded(string(109), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:76
0.1706 19127992 14. PHPUnit\Util\ExcludeList->isExcluded(string(109)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:80
0.1706 19127992 15. PHPUnit\Util\ExcludeList->initialize() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:166
0.1707 19127992 16. class_exists(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1707 19128056 17. spl_autoload_call(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1707 19128120 18. autoload(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
Warning: include(): Failed opening 'PHPUnit\Composer\Autoload\ClassLoader.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Administrator\PhpstormProjects\untitled\organizing\src\autoload.php on line 3
Call Stack:
0.0149 1571792 1. {main}() D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:0
0.1327 18893800 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:2249
0.1327 18893912 3. PHPUnit\TextUI\Command->run(array(2), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:93
0.1375 19071600 4. PHPUnit\TextUI\TestRunner->run(class PHPUnit\Framework\TestSuite, array(15), array(0), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:124
0.1661 19126464 5. PHPUnit\TextUI\DefaultResultPrinter->printResult(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/TestRunner.php:496
0.1664 19126488 6. PHPUnit\TextUI\DefaultResultPrinter->printFailures(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:154
0.1664 19126488 7. PHPUnit\TextUI\DefaultResultPrinter->printDefects(array(1), string(7)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:299
0.1701 19126488 8. PHPUnit\TextUI\DefaultResultPrinter->printDefect(class PHPUnit\Framework\TestFailure, long) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:272
0.1706 19126488 9. PHPUnit\TextUI\DefaultResultPrinter->printDefectTrace(class PHPUnit\Framework\TestFailure) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:279
0.1706 19126488 10. PHPUnit\Framework\ExpectationFailedException->__toString() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:288
0.1706 19126712 11. PHPUnit\Util\Filter::getFilteredStacktrace(class PHPUnit\Framework\ExpectationFailedException) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Framework/Exception/Exception.php:58
0.1706 19127848 12. PHPUnit\Util\Filter::shouldPrintFrame(array(2), string(25), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:57
0.1706 19127992 13. PHPUnit\Util\Filter::fileIsExcluded(string(109), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:76
0.1706 19127992 14. PHPUnit\Util\ExcludeList->isExcluded(string(109)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:80
0.1706 19127992 15. PHPUnit\Util\ExcludeList->initialize() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:166
0.1707 19127992 16. class_exists(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1707 19128056 17. spl_autoload_call(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1707 19128120 18. autoload(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
Expectation failed for method name is "set" when invoked 2 time(s)
Parameter 1 for invocation #0 stdClass::set('foo', 0) does not match expected value.
Failed asserting that 0 is greater than 0.
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\SubjectTest.php:22
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
自然是断言失败。
那么SubjectTest.php修改如:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class SubjectTest extends TestCase
{
public function testFunctionCalledTwoTimesWithSpecificArguments(): void
{
//获取模仿对象构造并为其模仿一个set方法
$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['set'])
->getMock();
//断言这里set方法会被调用两次
//并且断言第一次执行的传入参数是foo和一个大于0的数
//并且断言第二次执行的传入参数是bar和一个大于0的数
$mock->expects($this->exactly(2))
->method('set')
->withConsecutive(
[$this->equalTo('foo'), $this->greaterThan(0)],
[$this->equalTo('bar'), $this->greaterThan(0)]
);
$mock->set('foo', 21);
$mock->set('bar', 48);
}
}
这样自然就断言成功:
C:\Users\Administrator\PhpstormProjects\untitled\organizing>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\SubjectTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.004, Memory: 20.00 MB
OK (1 test, 1 assertion)