<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class SubjectTest extends TestCase
{
public function testIdenticalObjectPassed(): void
{
$expectedObject = new stdClass;
$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['foo'])
->enableArgumentCloning()
->getMock();
$mock->expects($this->once())
->method('foo')
->with($this->identicalTo($expectedObject));
$mock->foo($expectedObject);
// 现在仿件会克隆参数,因此 identicalTo 约束会失败。
}
}
这样断言肯定就是失败的:
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.005, Memory: 20.00 MB
There was 1 failure:
1) SubjectTest::testIdenticalObjectPassed
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.0148 1571792 1. {main}() D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:0
0.1357 18893800 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:2249
0.1357 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.1406 19070008 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.1702 19145848 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.1704 19145872 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.1704 19145872 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.1708 19145872 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.1713 19145872 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.1713 19145872 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.1713 19146128 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.1713 19147264 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.1713 19147408 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.1713 19147408 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.1713 19147408 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.1713 19147408 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.1714 19147472 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.1714 19147536 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.0148 1571792 1. {main}() D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:0
0.1357 18893800 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:2249
0.1357 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.1406 19070008 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.1702 19145848 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.1704 19145872 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.1704 19145872 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.1708 19145872 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.1713 19145872 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.1713 19145872 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.1713 19146128 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.1713 19147264 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.1713 19147408 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.1713 19147408 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.1713 19147408 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.1713 19147408 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.1714 19147472 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.1714 19147536 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 "foo" when invoked 1 time(s)
Parameter 0 for invocation stdClass::foo(stdClass Object ()) does not match expected value.
Failed asserting that two variables reference the same object.
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\SubjectTest.php:18
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
但是同样的,上面最开始的时候我们用的是直接的字面量1,那么我们用一个栈地址的变量也是同样有效的,不会断言失败的。
我想应该是说参数克隆只是对于对象这种引用的来说的。
SubjectTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class SubjectTest extends TestCase
{
public function testIdenticalObjectPassed(): void
{
$a=1;
$expectedObject = new stdClass;
$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['foo'])
->enableArgumentCloning()
->getMock();
$mock->expects($this->once())
->method('foo')
->with($this->identicalTo($a));
$mock->foo($a);
}
}
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.005, Memory: 20.00 MB
OK (1 test, 1 assertion)
忘了说了,上面字面量1的那种情况我自己测试了和这个变量$a的情况一样,这里就不贴代码了。