ワンクリックで
drupal-unit
Drupal Unit tests with UnitTestCase — testing isolated PHP logic with no Drupal bootstrap, mocking dependencies.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Drupal Unit tests with UnitTestCase — testing isolated PHP logic with no Drupal bootstrap, mocking dependencies.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
HTMX in Drupal 11.3+ core — the Htmx PHP fluent builder, dynamic forms with swapOob, partial routes with _htmx_route, response headers, and Drupal.behaviors integration. Use when building interactive UI without full-page reloads using Drupal's native HTMX support.
DDEV local development expertise. Use when working with DDEV projects, containers, configuration, or troubleshooting DDEV environments.
Custom Docker Compose local development patterns. Use when working with Docker-based local environments, container configuration, or troubleshooting Docker setups.
Drupal access control — permissions.yml, access callbacks, AccessResult, custom access checkers, and entity access.
Drupal Cache API — cache tags, contexts, max-age, cache bins, invalidation, and adding cache metadata to render arrays.
Drupal Composer management — requiring modules, updates, patches via composer-patches, and version constraints.
| name | drupal-unit |
| description | Drupal Unit tests with UnitTestCase — testing isolated PHP logic with no Drupal bootstrap, mocking dependencies. |
// 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([]);
}
}
// 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'],
]);
// UnitTestCase provides getStringTranslationStub()
$this->service = new MyService(
$this->getStringTranslationStub()
);
// Expect a specific log message
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
->method('error')
->with($this->stringContains('Failed to process'));
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/