---
title: Experimenting with Architectural Testing
date: 2024-02-08
permalink: daily/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]:

```language-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 %}