{"id":1693,"date":"2016-05-13T06:47:04","date_gmt":"2016-05-13T05:47:04","guid":{"rendered":"https:\/\/www.entropywins.wtf\/blog\/?p=1693"},"modified":"2022-01-26T00:32:46","modified_gmt":"2022-01-25T23:32:46","slug":"5-ways-to-write-better-mocks","status":"publish","type":"post","link":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/","title":{"rendered":"5 ways to write better mocks"},"content":{"rendered":"<p>In this post I share 5 easy ways to write better mocks that I picked up over the years. These will help you write tests that break less, are easier to read, are more IDE friendly, and are easier to refactor. The focus is on <a href=\"https:\/\/phpunit.de\/\">PHPUnit<\/a> and PHP, yet most of the techniques used, and principles touched upon, are also applicable when using different languages and testing frameworks.<\/p>\n<p>Before we get down to it, some terminology needs to be agreed upon. This post is about <a href=\"http:\/\/xunitpatterns.com\/Test%20Double.html\">Test Doubles<\/a>, which are commonly referred to as Mocks. It&#8217;s somewhat unfortunate that Mock has become the common name, as it also is a specific type of Test Double. I will use the following, more precise, terminology for the rest of the post:<\/p>\n<ul>\n<li><strong>Test Double<\/strong>: General term for test code that stands in for production code<\/li>\n<li><strong>Stub<\/strong>: A Test Double that does nothing except for returning hardcoded values<\/li>\n<li><strong>Fake<\/strong>: A Test Double that has real behavior in it, though does not make any assertions<\/li>\n<li><strong>Spy<\/strong>: A Test Double that records calls to its methods, though does not make assertions<\/li>\n<li><strong>Mock<\/strong>: A Test Double that makes assertions<\/li>\n<\/ul>\n<p><strong>1. Reference classes using ::class<\/strong><\/p>\n<p>This one is really simple. Instead of calling <span class=\"lang:php decode:true crayon-inline \">$this-&gt;getMock( &#8216;KittenRepository&#8217; )<\/span> , use the <a href=\"http:\/\/php.net\/manual\/en\/language.oop5.basic.php#language.oop5.basic.class.class\"><code>::class<\/code> keyword<\/a> added in PHP 5.5: <span class=\"lang:php decode:true crayon-inline \">$this-&gt;getMock( KittenRepository::class )<\/span> . This avoids your tests getting broken when renaming or moving your class using decent editors. Typos are immediately apparent, and navigating to the class or interface becomes easier.<\/p>\n<p><strong>2. Don&#8217;t bind to method names when you don&#8217;t have to<\/strong><\/p>\n<p>Imagine you are testing some code that uses a <a href=\"http:\/\/www.php-fig.org\/psr\/psr-3\/\">PSR-3 compliant logger<\/a>. This logger has a general <code>log<\/code> method that takes a log level, and a specific method for each of the log levels, such as <code>warning<\/code> and <code>info<\/code>. Even in case where you want to test the specific log level being used, the code under test can use either log or the more specific method. Which one is used is an internal implementation detail of the production code, and something the test preferably does not know about. Consider this Mock:<\/p>\n<pre class=\"lang:php decode:true\">$logger-&gt;expects( $this-&gt;never() )-&gt;method( 'log' );<\/pre>\n<p>If your production code changes to use a more specific method, the test will no longer be correct. In this case you might not even notice, as the test does not further rely on behavior of the created Test Double. Another cost to consider is that you have a <a href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/01\/30\/missing-in-php7-function-references\/\">string reference to a method name<\/a>, which amongst other things, breaks refactoring.<\/p>\n<p>In a number of cases this is easily avoided using the not so well known <code>anything<\/code> PHPUnit method. Want to verify your logger is never invoked in a given situation?<\/p>\n<pre class=\"lang:php decode:true\">$logger-&gt;expects( $this-&gt;never() )-&gt;method( $this-&gt;anything() );<\/pre>\n<p>Want to test what happens when the <a href=\"http:\/\/martinfowler.com\/eaaCatalog\/repository.html\">repository<\/a> your code uses throws an exception?<\/p>\n<pre class=\"lang:php decode:true\">$repository-&gt;expects( $this-&gt;any() )-&gt;method( $this-&gt;anything() )\n    -&gt;willThrowException( new RuntimeException() );<\/pre>\n<p>This approach only works in some situations. In others you will either need to bear the cost of binding to implementation details, change your test to be state based, or resort to more complicated workarounds.<\/p>\n<p><strong>3. Don&#8217;t bind to call count when you don&#8217;t have to<\/strong><\/p>\n<p>When constructing a Stub or a Fake, it&#8217;s easy to turn it into a Mock as well. This is very similar to binding to method calls: your test becomes aware of implementation details.<\/p>\n<p>The previous code snippet shows you a very simple Stub. It&#8217;s a logger that always throws an exception. Note the use of the <code>any<\/code> method, as opposed to the <code>once<\/code> method in this snippet:<\/p>\n<pre class=\"lang:php decode:true \">$repository-&gt;expects( $this-&gt;once() )-&gt;method( $this-&gt;anything() )\n    -&gt;willThrowException( new RuntimeException() );<\/pre>\n<p>If you are not intentionally creating a Mock, then don&#8217;t make assertions about the call count. It&#8217;s easy to add the assertion in nearly all cases, yet it does not come for free.<\/p>\n<p><strong>4. Encapsulate your Test Double creation<\/strong><\/p>\n<p>When constructing a Test Double via the <a href=\"https:\/\/phpunit.de\/manual\/current\/en\/test-doubles.html\">PHPUnit Test Double API<\/a>, you get back an instance of <code>PHPUnit_Framework_MockObject_MockObject<\/code>. While you know that it is also an instance of the class or interface that you fed into the Mock API, tools need a little help (before they are able to help you in return). One way of doing this is extracting the Test Double creation into its own method, and using a <a href=\"http:\/\/php.net\/manual\/en\/functions.returning-values.php#functions.returning-values.type-declaration\">return type hint<\/a>. If you are still using PHP 5.x, you can add a DocBlock with <code>@return KittenRepository<\/code> to achieve the same effect.<\/p>\n<pre class=\"lang:php decode:true\">private function newThrowingRepository(): KittenRepository {\n\u00a0\u00a0 \u00a0$repository = $this-&gt;getMock( KittenRepository::class );\n\n\u00a0\u00a0 \u00a0$repository-&gt;expects( $this-&gt;once() )-&gt;method( $this-&gt;anything() )\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0-&gt;willThrowException( new RuntimeException() );\n\n\u00a0\u00a0 \u00a0return $repository;\n}\n<\/pre>\n<p>Now tools will stop complaining that you are giving a <code>MockObject<\/code> to code expecting a <code>KittenRepository<\/code>.<\/p>\n<p>This extraction has two additional benefits. Firstly, you hide the details of Test Double construction from your actual test method, which now no longer knows about the mocking framework. Secondly, your test method becomes more readable, as it is no longer polluted by details on the wrong level of abstraction. That brings us to clean functions and test methods in general, which is out of scope for this particular blog post.<\/p>\n<p><strong>5. Create your own Test Doubles<\/strong><\/p>\n<p>While often it&#8217;s great to use the PHPUnit Test Double API, there are plenty of cases where creating your own Test Doubles yields significant advantages.<\/p>\n<p>To create your own Test Doubles, simply implement the interface as you would do in production code. For stubs, there is not much to do, just return the stub value. Some tools even allow you to automatically create these. Spies are also quite simple, just create a field of type array to store the calls to a method, and provide a getter.<\/p>\n<p>Remember how we do not want to bind to our production code&#8217;s choice of logger method? If we want to assert something different than no calls being made, the PHPUnit Test Double API is not of much help. It is however easy to create a simple Spy.<\/p>\n<pre class=\"lang:php decode:true \">class LoggerSpy extends \\Psr\\Log\\AbstractLogger {\n    private $logCalls = [];\n\n    public function log( $level, $message, array $context = [] ) {\n        $this-&gt;logCalls[] = [ $level, $message, $context ];\n    }\n\n    public function getLogCalls(): array {\n        return $this-&gt;logCalls;\n    }\n}\n<\/pre>\n<p>Since <code>AbstractLogger<\/code> provides the specific logging methods such as <code>warning<\/code> and <code>info<\/code> and has them call <code>log<\/code>, all calls end up looking the same to the test using the Spy. This class is now <a href=\"https:\/\/github.com\/JeroenDeDauw\/PsrLogTestDoubles\">available as a mini-library<\/a>.<\/p>\n<p>The test that uses the Spy needs to make its own assertions on the spied upon method calls. If certain assertions are common, you can place them in your Spy. Since the Spy itself does not invoke these assertions, it remains a Spy and does not become a Mock. You can even use PHPUnit to do the actual assertions.<\/p>\n<pre class=\"lang:php decode:true \">class MailerSpy implements Mailer {\n    private $testCase;\n    private $sendMailCalls = [];\n\n    public function __construct( PHPUnit_Framework_TestCase $testCase ) {\n        $this-&gt;testCase = $testCase;\n    }\n\n    public function sendMail( EmailAddress $recipient, array $templateArguments = [] ) {\n        $this-&gt;sendMailCalls[] = func_get_args();\n    }\n\n    public function assertMailerCalledOnceWith( EmailAddress $expectedEmail, array $expectedArguments ) {\n        $this-&gt;testCase-&gt;assertCount( 1, $this-&gt;sendMailCalls, 'Mailer should be called exactly once' );\n\n        $this-&gt;testCase-&gt;assertEquals(\n            [\n                $expectedEmail,\n                $expectedArguments\n            ],\n            $this-&gt;sendMailCalls[0]\n        );\n    }\n}<\/pre>\n<p>Creating your own Test Doubles completely sidesteps the problem of referencing method names using strings. I&#8217;ve yet to see a tool that understands the Test Doubles created by PHPUnit. Your IDE won&#8217;t find them when you search for all implementors of an interface, making refactoring, discovery and navigation harder.<\/p>\n<p>A related advantage is that lack of magic not only makes the code easier to understand to tools, but also to developers. You do not need knowledge of the PHPUnit Test Double API to understand and modify your own Test Doubles.<\/p>\n<p>In my projects I put Test Doubles into <code>tests\/Fixtures<\/code>. Since I have a dedicated class for each Test Double, it&#8217;s easy to reuse them. And the tests in which I use them focus on what they want to test, without being polluted with Test Double creation code.<\/p>\n<p>Frank de Jonge wrote a <a href=\"https:\/\/blog.frankdejonge.nl\/testing-without-mocking-frameworks\/\">longer take on why to not use mocking tools<\/a>.<\/p>\n<p><strong>Wrapping up<\/strong><\/p>\n<p>Treat your tests as first class code. Avoid not needed dependencies, respect encapsulation as much as you can, try not to use magic, and keep things simple. The <code>::class<\/code> keyword, the <code>any<\/code> and <code>anything<\/code> methods, encapsulated Test Double construction, and creating your own Test Doubles, are all things that can help with this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I share 5 easy ways to write better mocks that I picked up over the years. These&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[328,415,416,414,195,196,197,215,413,257],"class_list":["post-1693","post","type-post","status-publish","format-standard","hentry","category-programming","tag-clean-code","tag-encapsulation","tag-maintainability","tag-mocks","tag-php","tag-phpunit","tag-planet-wikimedia","tag-refactoring","tag-test-doubles","tag-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>5 ways to write better mocks - Blog of Jeroen De Dauw<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"5 ways to write better mocks - Blog of Jeroen De Dauw\" \/>\n<meta property=\"og:description\" content=\"In this post I share 5 easy ways to write better mocks that I picked up over the years. These&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog of Jeroen De Dauw\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-13T05:47:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-25T23:32:46+00:00\" \/>\n<meta name=\"author\" content=\"Jeroen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/JeroenDeDauw\" \/>\n<meta name=\"twitter:site\" content=\"@JeroenDeDauw\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeroen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/\"},\"author\":{\"name\":\"Jeroen\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\"},\"headline\":\"5 ways to write better mocks\",\"datePublished\":\"2016-05-13T05:47:04+00:00\",\"dateModified\":\"2022-01-25T23:32:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/\"},\"wordCount\":1224,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\"},\"keywords\":[\"Clean Code\",\"Encapsulation\",\"Maintainability\",\"Mocks\",\"PHP\",\"PHPUnit\",\"Planet Wikimedia\",\"Refactoring\",\"Test Doubles\",\"Testing\"],\"articleSection\":[\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/\",\"url\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/\",\"name\":\"5 ways to write better mocks - Blog of Jeroen De Dauw\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#website\"},\"datePublished\":\"2016-05-13T05:47:04+00:00\",\"dateModified\":\"2022-01-25T23:32:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/05\\\/13\\\/5-ways-to-write-better-mocks\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"5 ways to write better mocks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/\",\"name\":\"Entropy Wins\",\"description\":\"A blog on Software Architecture, Design and Craftsmanship\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\",\"name\":\"Jeroen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\",\"caption\":\"Jeroen\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/jeroendedauw\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/JeroenDeDauw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"5 ways to write better mocks - Blog of Jeroen De Dauw","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/","og_locale":"en_US","og_type":"article","og_title":"5 ways to write better mocks - Blog of Jeroen De Dauw","og_description":"In this post I share 5 easy ways to write better mocks that I picked up over the years. These&hellip;","og_url":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/","og_site_name":"Blog of Jeroen De Dauw","article_published_time":"2016-05-13T05:47:04+00:00","article_modified_time":"2022-01-25T23:32:46+00:00","author":"Jeroen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/JeroenDeDauw","twitter_site":"@JeroenDeDauw","twitter_misc":{"Written by":"Jeroen","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/#article","isPartOf":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/"},"author":{"name":"Jeroen","@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7"},"headline":"5 ways to write better mocks","datePublished":"2016-05-13T05:47:04+00:00","dateModified":"2022-01-25T23:32:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/"},"wordCount":1224,"commentCount":5,"publisher":{"@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7"},"keywords":["Clean Code","Encapsulation","Maintainability","Mocks","PHP","PHPUnit","Planet Wikimedia","Refactoring","Test Doubles","Testing"],"articleSection":["Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/","url":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/","name":"5 ways to write better mocks - Blog of Jeroen De Dauw","isPartOf":{"@id":"https:\/\/www.entropywins.wtf\/blog\/#website"},"datePublished":"2016-05-13T05:47:04+00:00","dateModified":"2022-01-25T23:32:46+00:00","breadcrumb":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/13\/5-ways-to-write-better-mocks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.entropywins.wtf\/blog\/"},{"@type":"ListItem","position":2,"name":"5 ways to write better mocks"}]},{"@type":"WebSite","@id":"https:\/\/www.entropywins.wtf\/blog\/#website","url":"https:\/\/www.entropywins.wtf\/blog\/","name":"Entropy Wins","description":"A blog on Software Architecture, Design and Craftsmanship","publisher":{"@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.entropywins.wtf\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7","name":"Jeroen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g","caption":"Jeroen"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g"},"sameAs":["https:\/\/www.linkedin.com\/in\/jeroendedauw\/","https:\/\/x.com\/https:\/\/twitter.com\/JeroenDeDauw"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p74TBF-rj","jetpack-related-posts":[{"id":2092,"url":"https:\/\/www.entropywins.wtf\/blog\/2017\/10\/09\/yield-in-phpunit-data-providers\/","url_meta":{"origin":1693,"position":0},"title":"Yield in PHPUnit data providers","author":"Jeroen","date":"2017-10-09","format":false,"excerpt":"Initially I started creating a general post about PHP Generators, a feature introduced in PHP 5.5. However since I keep failing to come up with good examples for some cool ways to use Generators, I decided to do this mini post focusing on one such cool usage. PHPUnit data providers\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2271,"url":"https:\/\/www.entropywins.wtf\/blog\/2018\/08\/01\/clean-architecture-usecase-tests\/","url_meta":{"origin":1693,"position":1},"title":"Clean Architecture: UseCase tests","author":"Jeroen","date":"2018-08-01","format":false,"excerpt":"When creating an application that follows The Clean Architecture you end up with a number of UseCases that hold your application logic. In this blog post I outline a testing pattern for effectively testing these UseCases and avoiding common pitfalls. Testing UseCases A UseCase contains the application logic for a\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":904,"url":"https:\/\/www.entropywins.wtf\/blog\/2010\/07\/18\/mediawiki-testing-with-phpunit\/","url_meta":{"origin":1693,"position":2},"title":"MediaWiki testing with PHPUnit","author":"Jeroen","date":"2010-07-18","format":false,"excerpt":"I figured having some unit tests for Maps, the MediaWiki extension to work with geographical data and display it by embedding dynamic maps into your articles, would be beneficial to it's quality. It's pretty hard to try cover all possible use cases with manual tests, and consumes a lot of\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"PEAR logo","src":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2010\/07\/pearsmall.gif?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1577,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/01\/30\/missing-in-php7-function-references\/","url_meta":{"origin":1693,"position":3},"title":"Missing in PHP7: function references","author":"Jeroen","date":"2016-01-30","format":false,"excerpt":"This is the first post in my Missing in PHP7 series. Over time, PHP has improved its capabilities with regards to functions.\u00a0As of PHP 5.3 you can create anonymous functions and as of 5.4 you can use the callable type hint. However referencing a function still requires using a string.\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2101,"url":"https:\/\/www.entropywins.wtf\/blog\/2018\/01\/20\/php-project-template\/","url_meta":{"origin":1693,"position":4},"title":"PHP project template","author":"Jeroen","date":"2018-01-20","format":false,"excerpt":"Want to start a new PHP project? Perhaps yet another library you are creating? Tired of doing the same lame groundwork for the 5th time this month? Want to start a code kata and not lose time on generic setup work? I got just the project for you! Edit: there\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2018\/01\/php.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2018\/01\/php.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2018\/01\/php.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2018\/01\/php.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2018\/01\/php.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":1703,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/05\/24\/i-t-a-k-e-2016\/","url_meta":{"origin":1693,"position":5},"title":"I T.A.K.E. 2016","author":"Jeroen","date":"2016-05-24","format":false,"excerpt":"Last week I attended the I T.A.K.E. unconference in Bucharest. This unconference is about software development, and has tracks such as code quality, DevOps, craftsmanship, microservices and leadership. In this post I share my overall impressions as well as the notes I took during the uncoference. Conference impression This was\u2026","rel":"","context":"In &quot;Events&quot;","block_context":{"text":"Events","link":"https:\/\/www.entropywins.wtf\/blog\/category\/events\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/05\/you_down_wit_OPC-yeah_you_know_me.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/05\/you_down_wit_OPC-yeah_you_know_me.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/05\/you_down_wit_OPC-yeah_you_know_me.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/05\/you_down_wit_OPC-yeah_you_know_me.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts\/1693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/comments?post=1693"}],"version-history":[{"count":13,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts\/1693\/revisions"}],"predecessor-version":[{"id":2844,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts\/1693\/revisions\/2844"}],"wp:attachment":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/media?parent=1693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/categories?post=1693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/tags?post=1693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}