$npx -y skills add aligundogdu/symfony-hexagonal-skill --skill symfony-testingSymfony testing — PHPUnit test organization, unit tests, integration tests, functional tests, TDD workflow, PHPStan, mocking ports, test patterns. Triggers on: test, PHPUnit, TDD, unit test, integration test, functional test, mock, testing, test driven, code quality, PHPStan
| 1 | # Symfony Testing |
| 2 | |
| 3 | You are an expert in testing within Symfony hexagonal architecture. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | - User needs to write tests |
| 8 | - User asks about test organization or TDD |
| 9 | - User needs mocking strategies for ports |
| 10 | - User asks about PHPStan or code quality |
| 11 | - User mentions test-driven development |
| 12 | |
| 13 | ## Test Directory Structure (Mirrors src/) |
| 14 | |
| 15 | ``` |
| 16 | tests/ |
| 17 | ├── Unit/ |
| 18 | │ └── Domain/ |
| 19 | │ └── {Module}/ |
| 20 | │ ├── Entity/ |
| 21 | │ │ └── {Entity}Test.php |
| 22 | │ └── ValueObject/ |
| 23 | │ └── {ValueObject}Test.php |
| 24 | ├── Integration/ |
| 25 | │ └── Application/ |
| 26 | │ └── {Module}/ |
| 27 | │ ├── Command/ |
| 28 | │ │ └── {Handler}Test.php |
| 29 | │ └── Query/ |
| 30 | │ └── {Handler}Test.php |
| 31 | ├── Functional/ |
| 32 | │ └── Presentation/ |
| 33 | │ └── {Module}/ |
| 34 | │ └── API/ |
| 35 | │ └── {Controller}Test.php |
| 36 | └── Support/ |
| 37 | └── Adapter/ |
| 38 | └── InMemory{Repository}.php |
| 39 | ``` |
| 40 | |
| 41 | ## Test Types by Layer |
| 42 | |
| 43 | ### Unit Tests (Domain) — Pure PHP, no framework |
| 44 | ```php |
| 45 | namespace Tests\Unit\Domain\User\ValueObject; |
| 46 | |
| 47 | use App\Domain\User\ValueObject\Email; |
| 48 | use App\Domain\User\Exception\InvalidEmailException; |
| 49 | use PHPUnit\Framework\TestCase; |
| 50 | |
| 51 | final class EmailTest extends TestCase |
| 52 | { |
| 53 | public function test_valid_email_is_created(): void |
| 54 | { |
| 55 | $email = new Email('user@example.com'); |
| 56 | $this->assertSame('user@example.com', $email->value); |
| 57 | } |
| 58 | |
| 59 | public function test_invalid_email_throws_exception(): void |
| 60 | { |
| 61 | $this->expectException(InvalidEmailException::class); |
| 62 | new Email('invalid'); |
| 63 | } |
| 64 | |
| 65 | public function test_emails_are_equal(): void |
| 66 | { |
| 67 | $email1 = new Email('user@example.com'); |
| 68 | $email2 = new Email('user@example.com'); |
| 69 | $this->assertTrue($email1->equals($email2)); |
| 70 | } |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | ### Integration Tests (Application) — Mock ports |
| 75 | ```php |
| 76 | namespace Tests\Integration\Application\User\Command; |
| 77 | |
| 78 | use App\Application\User\Command\RegisterUser; |
| 79 | use App\Application\User\Command\RegisterUserHandler; |
| 80 | use App\Domain\User\Port\UserRepositoryInterface; |
| 81 | use PHPUnit\Framework\TestCase; |
| 82 | |
| 83 | final class RegisterUserHandlerTest extends TestCase |
| 84 | { |
| 85 | public function test_it_registers_a_user(): void |
| 86 | { |
| 87 | $repository = $this->createMock(UserRepositoryInterface::class); |
| 88 | $repository->expects($this->once())->method('save'); |
| 89 | $repository->method('existsByEmail')->willReturn(false); |
| 90 | |
| 91 | $handler = new RegisterUserHandler($repository); |
| 92 | $result = $handler(new RegisterUser('test@example.com', 'Test', 'password123')); |
| 93 | |
| 94 | $this->assertNotEmpty($result); |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ### Functional Tests (Presentation) — Full HTTP stack |
| 100 | ```php |
| 101 | namespace Tests\Functional\Presentation\User\API; |
| 102 | |
| 103 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 104 | |
| 105 | final class UserControllerTest extends WebTestCase |
| 106 | { |
| 107 | public function test_create_user(): void |
| 108 | { |
| 109 | $client = static::createClient(); |
| 110 | $client->request('POST', '/api/users', [], [], [ |
| 111 | 'CONTENT_TYPE' => 'application/json', |
| 112 | ], json_encode([ |
| 113 | 'email' => 'test@example.com', |
| 114 | 'name' => 'Test User', |
| 115 | 'password' => 'Password123', |
| 116 | ])); |
| 117 | |
| 118 | $this->assertResponseStatusCodeSame(201); |
| 119 | $response = json_decode($client->getResponse()->getContent(), true); |
| 120 | $this->assertNotNull($response['result']['id']); |
| 121 | $this->assertNull($response['error']); |
| 122 | } |
| 123 | } |
| 124 | ``` |
| 125 | |
| 126 | ## TDD Workflow |
| 127 | |
| 128 | 1. Write test first (RED) |
| 129 | 2. Write minimal code to pass (GREEN) |
| 130 | 3. Refactor (REFACTOR) |
| 131 | 4. Run PHPStan |
| 132 | 5. Repeat |
| 133 | |
| 134 | ## References |
| 135 | |
| 136 | See `references/` for detailed guides: |
| 137 | - `test-organization.md` — phpunit.xml config, test suites, fixtures |
| 138 | - `test-patterns.md` — In-memory adapters, test builders, assertion helpers |