|
8 | 8 | use GuzzleHttp\Psr7\Response;
|
9 | 9 |
|
10 | 10 | class ReadmeTest extends \PHPUnit\Framework\TestCase {
|
11 |
| - public function testReadme(): void { |
12 |
| - $contents = file_get_contents(__DIR__ . '/../README.md'); |
13 |
| - \assert($contents !== false); // For PHPStan. |
14 |
| - |
15 |
| - $mockedSource = file_get_contents(__DIR__ . '/resources/iso-8859-1.html'); |
16 |
| - \assert($mockedSource !== false); // For PHPStan. |
17 |
| - |
| 11 | + /** |
| 12 | + * @param array<string, string> $mockedHeaders |
| 13 | + * |
| 14 | + * @dataProvider readmeExamples |
| 15 | + */ |
| 16 | + public function testReadme(string $script, string $mockedSourceBody, array $mockedHeaders, string $expectedBody): void { |
18 | 17 | $mock = new MockHandler([
|
19 |
| - new Response(200, ['content-type' => 'text/html; charset=iso-8859-1; someOtherRandom="header in here"'], $mockedSource), |
| 18 | + new Response(200, $mockedHeaders, $mockedSourceBody), |
20 | 19 | ]);
|
21 | 20 |
|
22 |
| - $contents = str_replace('HandlerStack::create();', 'HandlerStack::create($mock);', $contents); |
23 |
| - |
24 |
| - preg_match_all('(```php\n(.*?)\n```)s', $contents, $matches, \PREG_SET_ORDER); |
| 21 | + $script = str_replace('HandlerStack::create();', 'HandlerStack::create($mock);', $script); |
25 | 22 |
|
26 |
| - $codes = array_map(function($match) { |
27 |
| - return $match[1]; |
28 |
| - }, $matches); |
29 |
| - |
30 |
| - $expected1 = file_get_contents(__DIR__ . '/resources/utf-8.html'); |
31 |
| - \assert($expected1 !== false); // For PHPStan. |
32 |
| - $expectations = [ |
33 |
| - $expected1, |
34 |
| - ]; |
| 23 | + $this->expectOutputString($expectedBody); |
| 24 | + eval($script); |
| 25 | + } |
35 | 26 |
|
36 |
| - $cases = array_combine($codes, $expectations); |
37 |
| - \assert($cases !== false); // For PHPStan. |
| 27 | + /** |
| 28 | + * @return iterable<array{string, string, array<string, string>, string}> |
| 29 | + */ |
| 30 | + public function readmeExamples(): iterable { |
| 31 | + $contents = file_get_contents(__DIR__ . '/../README.md'); |
| 32 | + \assert($contents !== false); // For PHPStan. |
38 | 33 |
|
39 |
| - foreach ($cases as $code => $expectation) { |
40 |
| - $this->expectOutputString($expectation); |
41 |
| - eval($code); |
| 34 | + preg_match_all('/<!-- Headers:\s*(?P<headers>.*?)\s*-->\n<!-- Mock response:\s*(?P<response>.*?)\s*-->\n<!-- Expected:\s*(?P<expected>.*?)\s*-->\n```php\n(?P<code>(?s).*?)\n```/', $contents, $matches, \PREG_SET_ORDER); |
| 35 | + |
| 36 | + foreach ($matches as $match) { |
| 37 | + $mockedSourceBody = file_get_contents(__DIR__ . '/resources/' . $match['response']); |
| 38 | + \assert($mockedSourceBody !== false); // For PHPStan. |
| 39 | + $mockedHeaders = json_decode($match['headers'], true); |
| 40 | + \assert(\is_array($mockedHeaders)); // For PHPStan. |
| 41 | + $expectedBody = file_get_contents(__DIR__ . '/resources/' . $match['expected']); |
| 42 | + \assert($expectedBody !== false); // For PHPStan. |
| 43 | + |
| 44 | + yield [ |
| 45 | + $match['code'], |
| 46 | + $mockedSourceBody, |
| 47 | + $mockedHeaders, |
| 48 | + $expectedBody, |
| 49 | + ]; |
42 | 50 | }
|
43 | 51 | }
|
44 | 52 | }
|
0 commit comments