{ "uuid": [ { "value": "999e4bc2-3d7a-4d73-9f5e-62363accde44" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:38+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "Why write custom assertions in your tests?\n" } ], "created": [ { "value": "2023-07-03T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:38+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2023\/07\/03\/why-write-custom-assertions-in-your-tests", "langcode": "en" } ], "body": [ { "value": "\n
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
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
I'm using PHPUnit's native assertions to check it returns a Collection (I regularly include the My initial implementation was to loop over each node and use Whilst this works, it likely won't be clear in the future what it's testing.<\/p>\n\n My initial thought was to add a comment describing it, but then I decided to wrap it in a custom assertion - 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 It can be easily reused within the same test case or elsewhere.<\/p>\n\n Although I only perform one assertion in this case, I can combine multiple assertions and perform any other required steps.<\/p>\n\n 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 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 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 I'm using PHPUnit's native assertions to check it returns a Collection (I regularly include the My initial implementation was to loop over each node and use Whilst this works, it likely won't be clear in the future what it's testing.<\/p>\n\n My initial thought was to add a comment describing it, but then I decided to wrap it in a custom assertion - 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 It can be easily reused within the same test case or elsewhere.<\/p>\n\n Although I only perform one assertion in this case, I can combine multiple assertions and perform any other required steps.<\/p>\n\n 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 ",
"summary": null
}
],
"feeds_item": [
{
"imported": "1970-01-01T00:33:45+00:00",
"guid": null,
"hash": "850f3e0675f0598a361d17b99f568768",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}illuminate\/collections<\/code> library from Laravel in other projects) and that each item is an instance of a
NodeInterface<\/code>, but there isn't an assertion to check each node is of the correct type.<\/p>\n\n
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
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
Why write a custom assertion?<\/h2>\n\n
assertContainsOnlyNodesOfType<\/code> - a private static function within my test class that wraps the native assertions.<\/p>\n\n
Adding a custom assertion<\/h2>\n\n
illuminate\/collections<\/code> library from Laravel in other projects) and that each item is an instance of a
NodeInterface<\/code>, but there isn't an assertion to check each node is of the correct type.<\/p>\n\n
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
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
Why write a custom assertion?<\/h2>\n\n
assertContainsOnlyNodesOfType<\/code> - a private static function within my test class that wraps the native assertions.<\/p>\n\n