$npx -y skills add edutrul/drupal-ai --skill drupal-unitDrupal Unit tests with UnitTestCase — testing isolated PHP logic with no Drupal bootstrap, mocking dependencies.
| 1 | # Drupal Unit Tests |
| 2 | |
| 3 | ## When to Use |
| 4 | |
| 5 | - Pure PHP logic with no Drupal dependencies |
| 6 | - Utility functions, data transformations, value objects |
| 7 | - Classes where all dependencies can be mocked |
| 8 | |
| 9 | ## Basic Structure |
| 10 | |
| 11 | ```php |
| 12 | // tests/src/Unit/MyServiceTest.php |
| 13 | namespace Drupal\Tests\my_module\Unit; |
| 14 | |
| 15 | use Drupal\Tests\UnitTestCase; |
| 16 | use Drupal\my_module\Service\MyService; |
| 17 | use Psr\Log\LoggerInterface; |
| 18 | |
| 19 | /** |
| 20 | * @coversDefaultClass \Drupal\my_module\Service\MyService |
| 21 | * @group my_module |
| 22 | */ |
| 23 | final class MyServiceTest extends UnitTestCase { |
| 24 | |
| 25 | private MyService $service; |
| 26 | |
| 27 | protected function setUp(): void { |
| 28 | parent::setUp(); |
| 29 | |
| 30 | $logger = $this->createMock(LoggerInterface::class); |
| 31 | $this->service = new MyService($logger); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @covers ::processData |
| 36 | */ |
| 37 | public function testProcessData(): void { |
| 38 | $input = ['foo' => 'bar']; |
| 39 | $result = $this->service->processData($input); |
| 40 | |
| 41 | $this->assertArrayHasKey('processed', $result); |
| 42 | $this->assertTrue($result['processed']); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @covers ::processData |
| 47 | */ |
| 48 | public function testProcessDataWithEmptyInput(): void { |
| 49 | $this->expectException(\InvalidArgumentException::class); |
| 50 | $this->service->processData([]); |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | ## Mocking with PHPUnit |
| 57 | |
| 58 | ```php |
| 59 | // Mock a simple interface |
| 60 | $mock = $this->createMock(SomeInterface::class); |
| 61 | $mock->method('someMethod')->willReturn('value'); |
| 62 | |
| 63 | // Mock with argument matching |
| 64 | $mock->expects($this->once()) |
| 65 | ->method('someMethod') |
| 66 | ->with($this->equalTo('expected-arg')) |
| 67 | ->willReturn('result'); |
| 68 | |
| 69 | // Mock throwing exception |
| 70 | $mock->method('someMethod')->willThrowException(new \RuntimeException('Error')); |
| 71 | |
| 72 | // Stub multiple calls |
| 73 | $mock->method('someMethod') |
| 74 | ->willReturnMap([ |
| 75 | ['arg1', 'result1'], |
| 76 | ['arg2', 'result2'], |
| 77 | ]); |
| 78 | ``` |
| 79 | |
| 80 | ## Mocking Drupal Translation (t()) |
| 81 | |
| 82 | ```php |
| 83 | // UnitTestCase provides getStringTranslationStub() |
| 84 | $this->service = new MyService( |
| 85 | $this->getStringTranslationStub() |
| 86 | ); |
| 87 | ``` |
| 88 | |
| 89 | ## Mocking Logger |
| 90 | |
| 91 | ```php |
| 92 | // Expect a specific log message |
| 93 | $logger = $this->createMock(LoggerInterface::class); |
| 94 | $logger->expects($this->once()) |
| 95 | ->method('error') |
| 96 | ->with($this->stringContains('Failed to process')); |
| 97 | ``` |
| 98 | |
| 99 | ## Running Tests |
| 100 | |
| 101 | Examples assume a `docroot/`-based Drupal project. If your project uses `web/` or another document root, adjust paths accordingly. |
| 102 | |
| 103 | ```bash |
| 104 | # Run specific test file |
| 105 | ddev exec vendor/bin/phpunit docroot/modules/custom/my_module/tests/src/Unit/MyServiceTest.php |
| 106 | |
| 107 | # Run all unit tests for a module |
| 108 | ddev exec vendor/bin/phpunit docroot/modules/custom/my_module/tests/src/Unit/ |
| 109 | |
| 110 | # Run with verbose output |
| 111 | ddev exec vendor/bin/phpunit --verbose docroot/modules/custom/my_module/tests/ |
| 112 | ``` |