/symfony-skills:testing-pyramidfrom symfony-skills
Guides writing tests for Symfony apps following the testing pyramid: unit (PHPUnit), integration (KernelTestCase), and functional (WebTestCase) tests with fixtures and Foundry factories.
Ships in symfony-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
╱╲ Functional (few) — WebTestCase: real HTTP kernel, real DB, full stack
╱──╲ Integration (some) — KernelTestCase: container + DB, no HTTP
╱────╲ Unit (many) — plain PHPUnit: pure PHP, no container, no DB
╱──────╲
Most tests are fast unit tests of domain logic. A smaller layer of integration tests covers wiring + persistence. A thin top layer of functional tests covers the critical user-facing flows end-to-end. Don't invert it — a suite that's all WebTestCase is slow and brittle.
Test value objects, aggregates, and services with their collaborators mocked. No container, no DB, milliseconds each.
final class OrderTest extends TestCase
{
public function test_confirming_an_empty_order_is_rejected(): void
{
$order = Order::place();
$this->expectException(\DomainException::class);
$order->confirm(); // invariant: can't confirm empty
}
public function test_adding_a_line_to_a_confirmed_order_is_rejected(): void
{
$order = Order::place();
$order->addItem(Uuid::v7(), 2);
$order->confirm();
$this->expectException(\DomainException::class);
$order->addItem(Uuid::v7(), 1);
}
}
Mock only what you own and inject (ports/repositories). Don't mock value objects or the framework.
// ✅ mock the repository PORT
$orders = $this->createMock(OrderRepository::class);
$orders->method('ofId')->willReturn($existingOrder);
$service = new CancelOrderHandler($orders);
KernelTestCase boots the container; pull real services and hit a real (test) database. Use it to verify repository queries, Doctrine mapping, and service wiring.
final class OrderRepositoryTest extends KernelTestCase
{
private EntityManagerInterface $em;
private OrderRepository $repo;
protected function setUp(): void
{
self::bootKernel();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
$this->repo = self::getContainer()->get(OrderRepository::class);
}
public function test_find_active_by_customer_returns_only_active(): void
{
OrderFactory::createOne(['customerEmail' => '[email protected]', 'status' => OrderStatus::Active]);
OrderFactory::createOne(['customerEmail' => '[email protected]', 'status' => OrderStatus::Cancelled]);
$result = $this->repo->findActiveByCustomer('[email protected]');
self::assertCount(1, $result);
}
}
WebTestCase sends a real request through the kernel. Reserve it for critical flows and contract checks (status codes, problem+json shape, auth).
final class CreateOrderTest extends WebTestCase
{
public function test_post_orders_returns_201_with_location(): void
{
$client = static::createClient();
$client->request('POST', '/api/orders', server: [
'CONTENT_TYPE' => 'application/json',
], content: json_encode([
'customerEmail' => '[email protected]',
'items' => [['productId' => Uuid::v7()->toRfc4122(), 'quantity' => 2]],
]));
self::assertResponseStatusCodeSame(201);
self::assertResponseHasHeader('Location');
}
public function test_invalid_payload_returns_422_problem_json(): void
{
$client = static::createClient();
$client->request('POST', '/api/orders', content: json_encode(['customerEmail' => 'not-an-email']));
self::assertResponseStatusCodeSame(422);
self::assertResponseHeaderSame('Content-Type', 'application/problem+json');
}
}
test (.env.test / DATABASE_URL); never run tests against dev/prod data.php bin/console --env=test doctrine:database:create
php bin/console --env=test doctrine:migrations:migrate --no-interaction
Prefer zenstruck/foundry factories over static fixture files — readable, overridable per test, no shared mutable state.
final class OrderFactory extends PersistentProxyObjectFactory
{
public static function class(): string { return Order::class; }
protected function defaults(): array
{
return [
'customerEmail' => self::faker()->email(),
'status' => OrderStatus::Pending,
];
}
}
// In a test
$order = OrderFactory::createOne(['status' => OrderStatus::Active]);
OrderFactory::createMany(5);
test_confirming_an_empty_order_is_rejected, not testConfirm.MockHttpClient).WebTestCase — invert back to a pyramid: most logic in fast unit tests.test DB + DAMA transaction rollback.schema:update — run migrations so tests catch drift.MockHttpClient.application/problem+json content type on error-path tests.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skills17 skills in this plugin
/api-platform-resourcesUse when exposing data through API Platform 4 — defining #[ApiResource], operations, DTO resources, state providers/processors, serialization groups, filters, and pagination. Use when the task mentions API Platform, ApiResource, state processor, or exposing an entity as a REST/GraphQL endpoint.
/bundle-organizationUse when structuring a Symfony project's src/ directory, organizing code into modules, configuring services, or deciding where a class belongs. Covers the modular monolith layout, service configuration, and config/ conventions for Symfony 7 (Flex, no app bundle).
/console-commandsUse when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.
/doctrine-migrationsUse when changing the database schema in Symfony — generating Doctrine migrations, writing reversible up()/down(), data migrations, and deploy-safe (zero-downtime) changes. Use when the task mentions migration, schema change, ALTER TABLE, or doctrine:migrations.
/doctrine-ormUse when creating Doctrine ORM 3 entities, associations, repositories, enums, or UUID identifiers in Symfony. Defines entity conventions, attribute mapping, fetch strategy, and repository structure. Use when the task mentions entity, Doctrine, repository, association, or mapping.
/doctrine-query-optimizationUse when writing Doctrine queries, fixing N+1 problems, paginating large result sets, building read models, or tuning DQL/query builder performance. Use when the task mentions N+1, slow query, pagination, DQL, hydration, or read model.
Other plugins' skills for the same job