testing-knowledge

Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "testing-knowledge" with this command: npx skills add dykyi-roman/awesome-claude-code/dykyi-roman-awesome-claude-code-testing-knowledge

Testing Knowledge Base

Quick reference for PHP testing patterns, principles, and best practices.

Testing Pyramid

        /\
       /  \     Functional (10%)
      /────\    - E2E, browser tests
     /      \   - Slow, fragile
    /────────\  Integration (20%)
   /          \ - DB, HTTP, queues
  /────────────\Unit (70%)
 /              \- Fast, isolated
/________________\- Business logic

Rule: 70% unit, 20% integration, 10% functional. Invert the pyramid = slow, brittle test suite.

AAA Pattern (Arrange-Act-Assert)

public function test_order_calculates_total_with_discount(): void
{
    // Arrange — set up test data
    $order = new Order(OrderId::generate());
    $order->addItem(new Product('Book', Money::EUR(100)));
    $discount = new PercentageDiscount(10);

    // Act — execute the behavior
    $total = $order->calculateTotal($discount);

    // Assert — verify the outcome
    self::assertEquals(Money::EUR(90), $total);
}

Rules:

  • One blank line between sections
  • Single Act per test
  • Assert behavior, not implementation

Naming Conventions

PHPUnit Style

test_{method}_{scenario}_{expected}
ExampleMethodScenarioExpected
test_calculate_total_with_discount_returns_reduced_amountcalculateTotalwith discountreturns reduced amount
test_confirm_when_already_shipped_throws_exceptionconfirmwhen already shippedthrows exception
test_email_with_invalid_format_fails_validationEmail (VO)with invalid formatfails validation

Pest Style

it('calculates total with discount applied')
it('throws exception when confirming shipped order')
it('fails validation for invalid email format')

Test Isolation Principles

DO

  • Fresh fixtures per test
  • Independent test execution (any order)
  • Teardown cleans all state
  • Use in-memory implementations

DON'T

  • Shared mutable state between tests
  • Tests depending on execution order
  • Global variables or singletons
  • Real external services in unit tests

Quick Quality Checklist

RuleCheck
One test = one behaviorSingle assertion group
Test is documentationName reads as specification
No logic in testsNo if/for/while
Fast execution<100ms per unit test
Mock interfaces onlyNever mock VO, Entity, final
≤3 mocks per testMore = design smell
Behavior over implementationTest WHAT, not HOW

DDD Component Testing

ComponentTest FocusMocks Allowed
Value ObjectValidation, equality, immutabilityNone
EntityState transitions, business rulesNone
AggregateInvariants, consistency, eventsNone
Domain ServiceBusiness logic spanning aggregatesRepository (Fake)
Application ServiceOrchestration, transactionsRepository, EventDispatcher
RepositoryCRUD operationsDatabase (SQLite)

PHP 8.4 Test Patterns

Unit Test Template

<?php

declare(strict_types=1);

namespace Tests\Unit\Domain;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('unit')]
#[CoversClass(Email::class)]
final class EmailTest extends TestCase
{
    public function test_creates_valid_email(): void
    {
        $email = new Email('user@example.com');

        self::assertSame('user@example.com', $email->value);
    }

    public function test_throws_for_invalid_format(): void
    {
        $this->expectException(InvalidArgumentException::class);

        new Email('invalid');
    }
}

Integration Test Template

<?php

declare(strict_types=1);

namespace Tests\Integration\Infrastructure;

use PHPUnit\Framework\Attributes\Group;
use Tests\DatabaseTestCase;

#[Group('integration')]
final class DoctrineOrderRepositoryTest extends DatabaseTestCase
{
    private OrderRepositoryInterface $repository;

    protected function setUp(): void
    {
        parent::setUp();
        $this->repository = $this->getContainer()->get(OrderRepositoryInterface::class);
    }

    public function test_saves_and_retrieves_order(): void
    {
        // Arrange
        $order = OrderMother::pending();

        // Act
        $this->repository->save($order);
        $found = $this->repository->findById($order->id());

        // Assert
        self::assertNotNull($found);
        self::assertTrue($order->id()->equals($found->id()));
    }
}

Test Doubles Quick Reference

TypePurposeWhen to Use
StubReturns canned answersExternal API responses
MockVerifies interactionsEvent publishing
FakeWorking implementationInMemory repository
SpyRecords callsLogging, notifications

Decision Matrix

Need to verify a call was made?
├── Yes → Mock or Spy
└── No → Need real behavior?
    ├── Yes → Fake
    └── No → Stub

Common Test Smells

SmellDetectionFix
Logic in Testif, for, while in testExtract to helper or parameterize
Mock Overuse>3 mocksRefactor design, use Fakes
Mystery GuestExternal files, hidden dataInline test data or use Builder
Eager TestTests multiple behaviorsSplit into separate tests
Fragile TestBreaks on refactorTest behavior, not implementation

Advanced Testing Patterns

Contract Testing (Pact)

AspectUnit TestIntegration TestContract Test
SpeedFastSlowMedium
ScopeSingle classService + depsAPI boundary
IsolationFullPartialConsumer/Provider
Use caseBusiness logicDB, queuesService-to-service

When to use: Microservices REST APIs, message-based systems, event schema verification.

Load Testing Patterns

PatternDurationLoad ProfileGoal
Smoke1-2 minMinimalVerify script works
Load10-30 minExpected trafficPerformance baseline
Stress10-30 min1.5-2x expectedFind breaking point
Spike5-10 minSudden burstTest auto-scaling
Soak2-8 hoursSustainedFind memory leaks

Chaos Testing (Failure Injection)

FailureHow to InjectWhat It Tests
Network latencySleep in middlewareTimeout handling
Service errorReturn 500 randomlyCircuit breaker
Connection refusedClose portFallback behavior
Slow databaseQuery delayQuery timeout handling

E2E Distributed Testing

StrategyHowTrade-off
Test containersDocker Compose per testIsolated but slow
Shared stagingDedicated environmentFast but interference
Data seedingAPI/DB setup per testControlled but complex
Snapshot restoreDB snapshot before testsFast reset

References

For detailed information, load these reference files:

  • references/unit-testing.md — Unit test patterns and examples
  • references/integration-testing.md — Integration test setup and patterns
  • references/ddd-testing.md — Testing DDD components (VO, Entity, Aggregate, Service)
  • references/advanced-testing.md — Contract testing (Pact), chaos testing, load testing patterns (ramp-up, spike, soak), E2E distributed testing

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

detect-code-smells

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

clean-arch-knowledge

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

ddd-knowledge

No summary provided by upstream source.

Repository SourceNeeds Review