DEV Community

Oliver Davies
Oliver Davies

Posted on • Originally published at oliverdavies.uk on

Experimenting with Architectural Testing

In yesterday's email, 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:

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,
      );
  }

}

Enter fullscreen mode Exit fullscreen mode

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.

P.S. Do you need immediate access to an expert Drupal Developer? With my Drupal development subscription, make unlimited requests for a fixed monthly price in less time than posting to a job board!

Top comments (0)