In less than 12 months, Drupal 7 will be end-of-life and no longer supported. Plan your upgrade to Drupal 10 now.

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

}

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.

- Oliver

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!

Was this useful?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.