agentleFS
edutruledutrul/drupal-ai/.claude/skills/drupal-unit/SKILL.md

drupal-unit

Skill71 starsChanged 7 months agoMITView on GitHub

Drupal Unit tests with UnitTestCase — testing isolated PHP logic with no Drupal bootstrap, mocking dependencies.

The file

Files
SKILL.md

Drupal Unit Tests

When to Use

  • Pure PHP logic with no Drupal dependencies
  • Utility functions, data transformations, value objects
  • Classes where all dependencies can be mocked

Basic Structure

// tests/src/Unit/MyServiceTest.php
namespace Drupal\Tests\my_module\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\my_module\Service\MyService;
use Psr\Log\LoggerInterface;

/**
 * @coversDefaultClass \Drupal\my_module\Service\MyService
 * @group my_module
 */
final class MyServiceTest extends UnitTestCase {

  private MyService $service;

  protected function setUp(): void {
    parent::setUp();

    $logger = $this->createMock(LoggerInterface::class);
    $this->service = new MyService($logger);
  }

  /**
   * @covers ::processData
   */
  public function testProcessData(): void {
    $input = ['foo' => 'bar'];
    $result = $this->service->processData($input);

    $this->assertArrayHasKey('processed', $result);
    $this->assertTrue($result['processed']);
  }

  /**
   * @covers ::processData
   */
  public function testProcessDataWithEmptyInput(): void {
    $this->expectException(\InvalidArgumentException::class);
    $this->service->processData([]);
  }

}

Mocking with PHPUnit

// Mock a simple interface
$mock = $this->createMock(SomeInterface::class);
$mock->method('someMethod')->willReturn('value');

// Mock with argument matching
$mock->expects($this->once())
  ->method('someMethod')
  ->with($this->equalTo('expected-arg'))
  ->willReturn('result');

// Mock throwing exception
$mock->method('someMethod')->willThrowException(new \RuntimeException('Error'));

// Stub multiple calls
$mock->method('someMethod')
  ->willReturnMap([
    ['arg1', 'result1'],
    ['arg2', 'result2'],
  ]);

Mocking Drupal Translation (t())

// UnitTestCase provides getStringTranslationStub()
$this->service = new MyService(
  $this->getStringTranslationStub()
);

Mocking Logger

// Expect a specific log message
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
  ->method('error')
  ->with($this->stringContains('Failed to process'));

Running Tests

Examples assume a docroot/-based Drupal project. If your project uses web/ or another document root, adjust paths accordingly.

# Run specific test file
ddev exec vendor/bin/phpunit docroot/modules/custom/my_module/tests/src/Unit/MyServiceTest.php

# Run all unit tests for a module
ddev exec vendor/bin/phpunit docroot/modules/custom/my_module/tests/src/Unit/

# Run with verbose output
ddev exec vendor/bin/phpunit --verbose docroot/modules/custom/my_module/tests/

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.