From d5900d59bbe3f70bd8a0467ccc125a1e0497b162 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 9 Feb 2024 22:35:23 +0000 Subject: [PATCH] Add daily email for 2024-02-08 Experimenting with Architectural Testing --- source/_daily_emails/2024-02-08.md | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 source/_daily_emails/2024-02-08.md diff --git a/source/_daily_emails/2024-02-08.md b/source/_daily_emails/2024-02-08.md new file mode 100644 index 000000000..09dc6e744 --- /dev/null +++ b/source/_daily_emails/2024-02-08.md @@ -0,0 +1,64 @@ +--- +title: Experimenting with Architectural Testing +date: 2024-02-08 +permalink: archive/2024/02/08/experimenting-with-architectural-testing +snippet: | + Have you tried architectural testing with PHPat, Pest or a similar tool? I have been for the past few days. +tags: + - software-development + - test-driven-development + - automated-testing + - architectural-testing + - php +--- + +{% block content %} +[In yesterday's email][yesterday], I mentioned parallel testing and speeding up your tests by running them in parallel. + +Something else I've been experimenting with recently in architectural testing with PHPat. + +For example, ensuring classes within a namespace are `final` or not, that Controller classes all extend `ControllerBase` and have the Controller suffix in their names. + +Going forward, I'd like to ensure that each Drupal module only uses its own classes and is separated, as I recently had an issue where I deleted a class method in one module only to find it was used in a different module. + +Here's what I have so far for my [testing course codebase][atdc]: + +```php +final class ArchitectureTest { + + public function test_classes_should_be_final(): Rule { + return PHPat::rule() + ->classes(Selector::inNamespace('Drupal\atdc')) + ->shouldBeFinal(); + } + + public function test_controllers_should_extend_ControllerBase(): Rule { + return PHPat::rule() + ->classes(Selector::inNamespace('Drupal\atdc\Controller')) + ->shouldExtend() + ->classes(Selector::classname(ControllerBase::class)); + } + + public function test_controllers_should_have_the_Controller_suffix(): Rule { + return PHPat::rule() + ->classes(Selector::inNamespace('Drupal\atdc\Controller')) + ->shouldBeNamed( + classname: '/Controller$/', + regex: TRUE, + ); + } + +} +``` + +I plan to continue expanding this configuration as I become more familiar with PHPat, and because it's a PHPStan extension, it's already available to run within my projects locally and within the CI pipeline. + +[atdc]: {{site.url}}/atdc +[yesterday]: {{site.url}}/archive/2024/02/07/running-tests-in-parallel-with-paratest +{% endblock %} + +{% block cta %} +P.S. Do you need immediate access to an expert Drupal Developer? [With my Drupal development subscription][subscription], make unlimited requests for a fixed monthly price in less time than posting to a job board! + +[subscription]: {{site.url}}/subscription +{% endblock %}