Test doubles: Difference between revisions

No edit summary
move and separate footnotes
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:


A '''[[wp:Test double]]''' is any object that stands in for a real dependency in automated testing. Usually in [[PHPUnit]], we make test doubles for other classes, but you can also double PHP native functions or <abbr title="Closures are small use-once functions which are useful as parameters for callback functions like array_walk, array_map, etc. Using closure functions in PHP can help to make code more succinct, expressive, and safer. Closures can access private data of an object.">closures</abbr><ref>aka anonymous functions<br>https://www.php.net/manual/en/functions.anonymous.php<br>https://www.php.net/manual/en/class.closure.php</ref><ref>These 'other things' being doubled are sometimes referred to as the "Depended On Component" - see [https://docs.phpunit.de/en/9.6/test-doubles.html Test Doubles in PHPUnit (v9.6)] or ([https://docs.phpunit.de/en/10.5/test-doubles.html v10.5])</ref>.
A '''[[wp:Test double]]''' is any object that stands in for a real dependency in automated testing<ref>
'''Important Material'''
 
see [https://docs.phpunit.de/en/9.6/test-doubles.html Test Doubles in PHPUnit (v9.6)] or ([https://docs.phpunit.de/en/10.5/test-doubles.html v10.5])
 
see the 'original' '''book on XUnit''' - [http://xunitpatterns.com/Using%20Test%20Doubles.html  chapter on Test Doubles], including diagrams to help illustrate the various types of doubles</ref>. Usually in [[PHPUnit]], we make test doubles for other classes, but you can also double PHP native functions or <abbr title="Closures are small use-once functions which are useful as parameters for callback functions like array_walk, array_map, etc. Using closure functions in PHP can help to make code more succinct, expressive, and safer. Closures can access private data of an object.">closures</abbr><ref>aka anonymous functions<br>https://www.php.net/manual/en/functions.anonymous.php<br>https://www.php.net/manual/en/class.closure.php</ref><ref>These 'other things' being doubled are sometimes referred to as the "Depended On Component"</ref>.


There are 5 types of Test doubles
There are 5 types of Test doubles
Line 214: Line 219:
=== getMockFromWsdl() ===
=== getMockFromWsdl() ===
Although PHPUnit 9.x allows you to build a mock for a web service from a [[wp:WSDL]] file, the <code>getMockFromWsdl()</code> method is deprecated in current versions and removed in PHPUnit 12. This is because people don't use SOAP and WSDL as commonly as they used to, and even if they do, they don't rely on PHP's SOAP extension <ref>[https://github.com/sebastianbergmann/phpunit/issues/5242 Deprecate TestCase::getMockFromWsdl()]</ref>.
Although PHPUnit 9.x allows you to build a mock for a web service from a [[wp:WSDL]] file, the <code>getMockFromWsdl()</code> method is deprecated in current versions and removed in PHPUnit 12. This is because people don't use SOAP and WSDL as commonly as they used to, and even if they do, they don't rely on PHP's SOAP extension <ref>[https://github.com/sebastianbergmann/phpunit/issues/5242 Deprecate TestCase::getMockFromWsdl()]</ref>.
A bit sad, but understandable. PHPUnit can't keep 20 years of legacy implementations along with new technologies.
In PHPUnit 9.6, you can easily mock a Google Web Search<ref>https://docs.phpunit.de/en/9.6/test-doubles.html#stubbing-and-mocking-web-services</ref>


{{References}}
{{References}}