"value":"\n <p>I'm refactoring some code on a client project - creating a Repository class to centralise some logic before implementing the next feature.<\/p>\n\n<p>The repository class is responsible for finding and returning any nodes with a specified field value and some base conditions (it must be the correct node type, published, etc.).<\/p>\n\n<h2 id=\"adding-a-custom-assertion\">Adding a custom assertion<\/h2>\n\n<p>I'm using PHPUnit's native assertions to check it returns a Collection (I regularly include the <code>illuminate\/collections<\/code> library from Laravel in other projects) and that each item is an instance of a <code>NodeInterface<\/code>, but there isn't an assertion to check each node is of the correct type.<\/p>\n\n<p>My initial implementation was to loop over each node and use <code>assertSame<\/code> on its bundle before refactoring to create an array of unique bundle names and comparing it to my expected names:<\/p>\n\n<pre><code class=\"language-php\">self::assertSame(\n expected: [$nodeType],\n actual: $haystack\n ->map(fn (NodeInterface $item): string => $item->bundle())\n ->unique()\n ->toArray(),\n);\n<\/code><\/pre>\n\n<h2 id=\"why-write-a-custom-assertion%3F\">Why write a custom assertion?<\/h2>\n\n<p>Whilst this works, it likely won't be clear in the future what it's testing.<\/p>\n\n<p>My initial thought was to add a comment describing it, but then I decided to wrap it in a custom assertion - <code>assertContainsOnlyNodesOfType<\/code> - a private static function within my test class that wraps the native assertions.<\/p>\n\n<p>This approach makes the test more readable now and in the future and more domain-focused by giving it a descriptive name.<\/p>\n\n<p>It can be easily reused within the same test case or elsewhere.<\/p>\n\n<p>Although I only perform one assertion in this case, I can combine multiple assertions and perform any other required steps.<\/p>\n\n<p>Finally, I can contain any implementation details within the custom assertion. Here, I'm matching the result against an array of expected values, not just a single node type which is what I want. This detail can be contained within the assertion, making it easier to read and reuse in the future.<\/p>\n\n ",
"format":"full_html",
"processed":"\n <p>I'm refactoring some code on a client project - creating a Repository class to centralise some logic before implementing the next feature.<\/p>\n\n<p>The repository class is responsible for finding and returning any nodes with a specified field value and some base conditions (it must be the correct node type, published, etc.).<\/p>\n\n<h2 id=\"adding-a-custom-assertion\">Adding a custom assertion<\/h2>\n\n<p>I'm using PHPUnit's native assertions to check it returns a Collection (I regularly include the <code>illuminate\/collections<\/code> library from Laravel in other projects) and that each item is an instance of a <code>NodeInterface<\/code>, but there isn't an assertion to check each node is of the correct type.<\/p>\n\n<p>My initial implementation was to loop over each node and use <code>assertSame<\/code> on its bundle before refactoring to create an array of unique bundle names and comparing it to my expected names:<\/p>\n\n<pre><code class=\"language-php\">self::assertSame(\n expected: [$nodeType],\n actual: $haystack\n ->map(fn (NodeInterface $item): string => $item->bundle())\n ->unique()\n ->toArray(),\n);\n<\/code><\/pre>\n\n<h2 id=\"why-write-a-custom-assertion%3F\">Why write a custom assertion?<\/h2>\n\n<p>Whilst this works, it likely won't be clear in the future what it's testing.<\/p>\n\n<p>My initial thought was to add a comment describing it, but then I decided to wrap it in a custom assertion - <code>assertContainsOnlyNodesOfType<\/code> - a private static function within my test class that wraps the native assertions.<\/p>\n\n<p>This approach makes the test more readable now and in the future and more domain-focused by giving it a descriptive name.<\/p>\n\n<p>It can be easily reused within the same test case or elsewhere.<\/p>\n\n<p>Although I only perform one assertion in this case, I can combine multiple assertions and perform any other required steps.<\/p>\n\n<p>Finally, I can contain any implementation details within the custom assertion. Here, I'm matching the result against an array of expected values, not just a single node type which is what I want. This detail can be contained within the assertion, making it easier to read and reuse in the future.<\/p>\n\n ",