|
| 1 | +# Testing Guidelines |
| 2 | + |
| 3 | +This document outlines the testing standards and practices for contributions to StringFormatter. |
| 4 | + |
| 5 | +## Core Testing Principles |
| 6 | + |
| 7 | +1. Use Data-Driven Testing: Create comprehensive data providers with descriptive string keys |
| 8 | +2. Test behaviors, not implementations: Focus on input/output results through public interfaces |
| 9 | +3. Use real instances: Avoid PHPUnit mocks - create custom test implementations when needed |
| 10 | +4. Black box testing: Verify interactions through results, not internal state |
| 11 | + |
| 12 | +## Test Structure Standards |
| 13 | + |
| 14 | +### Class Organization |
| 15 | + |
| 16 | +- All test classes must be `final` |
| 17 | +- Use the `Respect\StringFormatter\Test\Unit` namespace |
| 18 | +- Add `#[CoversClass(ClassName::class)]` attribute for code coverage |
| 19 | +- Extend `PHPUnit\Framework\TestCase` |
| 20 | + |
| 21 | +### Method Organization |
| 22 | + |
| 23 | +- Use `#[Test]` attribute for test methods |
| 24 | +- Use `#[DataProvider('methodName')]` for parameterized tests |
| 25 | +- Follow naming pattern: `itShould[Behavior]When[Condition]` or `itShould[ExpectedBehavior]` |
| 26 | + |
| 27 | +## Data Provider Patterns |
| 28 | + |
| 29 | +```php |
| 30 | +/ @return array<string, array{0: InputType, 1: TemplateType, 2: ExpectedType}> */ |
| 31 | +public static function providerForFeatureName(): array |
| 32 | +{ |
| 33 | + return [ |
| 34 | + 'descriptive test case name' => [ |
| 35 | + $input, |
| 36 | + $template, |
| 37 | + $expected, |
| 38 | + ], |
| 39 | + 'another test case' => [ |
| 40 | + $input2, |
| 41 | + $template2, |
| 42 | + $expected2, |
| 43 | + ], |
| 44 | + ]; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Provider Requirements |
| 49 | + |
| 50 | +- Use descriptive string keys for each test case |
| 51 | +- Document array structure with PHPDoc array shapes |
| 52 | +- Include edge cases, error conditions, and real-world scenarios |
| 53 | +- Test Unicode support and internationalization when applicable |
| 54 | + |
| 55 | +## Test Structure: Arrange-Act-Assert |
| 56 | + |
| 57 | +Follow the Arrange-Act-Assert pattern for clear test organization: |
| 58 | + |
| 59 | +```php |
| 60 | +#[Test] |
| 61 | +public function itShouldFormatTemplateCorrectly(): void |
| 62 | +{ |
| 63 | + // Arrange: Set up test data and objects |
| 64 | + $parameters = ['name' => 'John']; |
| 65 | + $template = 'Hello {{name}}!'; |
| 66 | + $expected = 'Hello John!'; |
| 67 | + |
| 68 | + // Act: Execute the method being tested |
| 69 | + $formatter = new FormatterClass($parameters); |
| 70 | + $actual = $formatter->format($template); |
| 71 | + |
| 72 | + // Assert: Verify the result |
| 73 | + self::assertSame($expected, $actual); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Pattern Benefits |
| 78 | + |
| 79 | +- **Arrange**: Clearly separates test setup |
| 80 | +- **Act**: Highlights the specific behavior being tested |
| 81 | +- **Assert**: Makes verification explicit and easy to read |
| 82 | + |
| 83 | +## Assertion Patterns |
| 84 | + |
| 85 | +### Primary Assertions |
| 86 | + |
| 87 | +```php |
| 88 | +// Following Arrange-Act-Assert pattern: |
| 89 | +// 1. Arrange: Create formatter and setup (done above) |
| 90 | +// 2. Act: Call format method (done above) |
| 91 | +// 3. Assert: Verify result |
| 92 | +self::assertSame($expected, $actual); |
| 93 | +``` |
| 94 | + |
| 95 | +### Exception Testing |
| 96 | + |
| 97 | +```php |
| 98 | +#[Test] |
| 99 | +public function itShouldThrowExceptionForInvalidInput(): void |
| 100 | +{ |
| 101 | + $this->expectException(InvalidFormatterException::class); |
| 102 | + $this->expectExceptionMessage('Specific error message'); |
| 103 | + |
| 104 | + new FormatterClass($invalidInput); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Coverage Requirements |
| 109 | + |
| 110 | +### Test Coverage Areas |
| 111 | + |
| 112 | +- Happy Path: Primary functionality with valid inputs |
| 113 | +- Edge Cases: Empty inputs, boundary conditions, malformed data |
| 114 | +- Error Conditions: Invalid inputs, exception scenarios |
| 115 | +- Type Support: All PHP types when the class under test handler mixed types directly |
| 116 | +- Unicode: International characters, emoji, mixed languages |
| 117 | +- Real-world Usage: Email templates, log messages, URLs, etc. |
| 118 | + |
| 119 | +## Code Quality Standards |
| 120 | + |
| 121 | +### Test Organization |
| 122 | + |
| 123 | +- Each test method validates one specific behavior |
| 124 | +- Group related tests logically by feature |
| 125 | +- Keep test setup minimal and inline |
| 126 | +- Use descriptive test case names in data providers |
| 127 | + |
| 128 | +### Documentation Standards |
| 129 | + |
| 130 | +- Document test methods with PHPDoc when behavior isn't obvious |
| 131 | +- Use type annotations for complex parameters in data providers |
| 132 | +- Include real-world examples in test cases |
| 133 | + |
| 134 | +## Dependencies and Mocks |
| 135 | + |
| 136 | +### Preferred Approach |
| 137 | + |
| 138 | +- Create real instances of any objects needed for testing |
| 139 | +- Use custom test implementations instead of PHPUnit mocks |
| 140 | +- Test through public APIs like `format()` and `formatWith()` |
| 141 | + |
| 142 | +### When Custom Implementations Are Needed |
| 143 | + |
| 144 | +```php |
| 145 | +class DumpStringifier implements Stringifier |
| 146 | +{ |
| 147 | + public function stringify(mixed $value): string |
| 148 | + { |
| 149 | + return var_export($value, true); |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Static Analysis Compatibility |
| 155 | + |
| 156 | +- Write tests that pass PHPStan static analysis |
| 157 | +- Use proper type annotations in PHPDoc |
| 158 | +- Ensure test code follows PSR-12 coding standard |
| 159 | + |
| 160 | +## Examples from Codebase |
| 161 | + |
| 162 | +See these test files for reference patterns: |
| 163 | + |
| 164 | +- `tests/Unit/PlaceholderFormatterTest.php` - Data-driven testing with comprehensive providers |
| 165 | +- `tests/Unit/PatternFormatterTest.php` - Exception testing and edge case coverage |
| 166 | +- `tests/Unit/JavascriptFormatterTest.php` - Unicode and internationalization testing |
| 167 | + |
| 168 | +Remember: Tests should be clear, maintainable, and focused on verifying desired behaviors rather than implementation details. |
0 commit comments