Skip to content

Commit c698f53

Browse files
committed
ReadmeTest: Add support for multiple examples
By moving the mock data to comments in the readme.
1 parent f041a3a commit c698f53

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ $ composer require fossar/guzzle-transcoder
1111

1212
## Basic usage
1313

14+
<!-- Headers: {"content-type": "text/html; charset=iso-8859-1; someOtherRandom=\"header in here\""} -->
15+
<!-- Mock response: iso-8859-1.html -->
16+
<!-- Expected: utf-8.html -->
1417
```php
1518
use Fossar\GuzzleTranscoder\GuzzleTranscoder;
1619
use GuzzleHttp\Client;

tests/ReadmeTest.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,45 @@
88
use GuzzleHttp\Psr7\Response;
99

1010
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 {
1817
$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),
2019
]);
2120

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);
2522

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+
}
3526

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.
3833

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+
];
4250
}
4351
}
4452
}

0 commit comments

Comments
 (0)