"value":"\n <p>Today, I wrote a test whilst fixing a bug in some legacy custom Drupal code.<\/p>\n\n<p>The code is for a custom block, which can be configured using user-defined settings - including a link URL.<\/p>\n\n<p>In this case, if the link was null (one hadn't been provided), the link was generated to the home page instead of the desired destination.<\/p>\n\n<h2 id=\"my-first-attempt\">My first attempt<\/h2>\n\n<p>There was a combination of settings needed to replicate the bug, such as the current site language, the node type the block was placed on and, of course, an empty link URL.<\/p>\n\n<p>Because the block uses the current route to get the current node, my first attempt to test this was to use a browser\/functional test.<\/p>\n\n<p>That failed quickly after having to enable various other custom modules due to dependencies and to add and configure unrelated configuration settings.<\/p>\n\n<h2 id=\"my-second-attempt\">My second attempt<\/h2>\n\n<p>My second attempt used kernel\/integration tests, but as there's no <code>setParameter()<\/code> method on the route matcher I could use in the test, I'd need to rely on mocking.<\/p>\n\n<p>In a unit test, everything needs to be mocked, but a kernel test allows me to be more selective, only mock what I need, and use the real services for everything else - a.k.a. partial mocking.<\/p>\n\n<h2 id=\"the-result\">The result<\/h2>\n\n<p>I replicated the bug by setting the default and current languages, creating a mock language manager, creating a node of the required type and returning it from a mocked route match.<\/p>\n\n<p>Everything else remained the same.<\/p>\n\n<p>Then, I fixed the bug and used a data provider to provide different variables into the test so each use case was covered.<\/p>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>The whole test file is 129 lines and would have been much more if I'd had to replicate all the configuration in a functional test or mock everything in a unit test.<\/p>\n\n<p>An integration test with partial mocking was ideal in this case, as it gave me the most flexibility to test what I needed whilst keeping the code simple.<\/p>\n\n<p>Whilst I'm aware of over-using mocks, this was an ideal situation to use them.<\/p>\n\n ",
"format":"full_html",
"processed":"\n <p>Today, I wrote a test whilst fixing a bug in some legacy custom Drupal code.<\/p>\n\n<p>The code is for a custom block, which can be configured using user-defined settings - including a link URL.<\/p>\n\n<p>In this case, if the link was null (one hadn't been provided), the link was generated to the home page instead of the desired destination.<\/p>\n\n<h2 id=\"my-first-attempt\">My first attempt<\/h2>\n\n<p>There was a combination of settings needed to replicate the bug, such as the current site language, the node type the block was placed on and, of course, an empty link URL.<\/p>\n\n<p>Because the block uses the current route to get the current node, my first attempt to test this was to use a browser\/functional test.<\/p>\n\n<p>That failed quickly after having to enable various other custom modules due to dependencies and to add and configure unrelated configuration settings.<\/p>\n\n<h2 id=\"my-second-attempt\">My second attempt<\/h2>\n\n<p>My second attempt used kernel\/integration tests, but as there's no <code>setParameter()<\/code> method on the route matcher I could use in the test, I'd need to rely on mocking.<\/p>\n\n<p>In a unit test, everything needs to be mocked, but a kernel test allows me to be more selective, only mock what I need, and use the real services for everything else - a.k.a. partial mocking.<\/p>\n\n<h2 id=\"the-result\">The result<\/h2>\n\n<p>I replicated the bug by setting the default and current languages, creating a mock language manager, creating a node of the required type and returning it from a mocked route match.<\/p>\n\n<p>Everything else remained the same.<\/p>\n\n<p>Then, I fixed the bug and used a data provider to provide different variables into the test so each use case was covered.<\/p>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>The whole test file is 129 lines and would have been much more if I'd had to replicate all the configuration in a functional test or mock everything in a unit test.<\/p>\n\n<p>An integration test with partial mocking was ideal in this case, as it gave me the most flexibility to test what I needed whilst keeping the code simple.<\/p>\n\n<p>Whilst I'm aware of over-using mocks, this was an ideal situation to use them.<\/p>\n\n ",