|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017, 2018 Alexey Kopytko <[email protected]> |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Tests\Pipeline; |
| 21 | + |
| 22 | +use PHPUnit\Framework\ExpectationFailedException; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Pipeline\Standard; |
| 25 | +use ReflectionClass; |
| 26 | +use ReflectionMethod; |
| 27 | +use function file_get_contents; |
| 28 | +use function implode; |
| 29 | +use function Pipeline\take; |
| 30 | +use function preg_match_all; |
| 31 | +use function sprintf; |
| 32 | +use function strpos; |
| 33 | +use function substr; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test documentation has sections for all public methods. |
| 37 | + * |
| 38 | + * @coversNothing |
| 39 | + * |
| 40 | + * @internal |
| 41 | + */ |
| 42 | +final class DocumentationTest extends TestCase |
| 43 | +{ |
| 44 | + private static string $readme; |
| 45 | + private static string $headers; |
| 46 | + |
| 47 | + public static function setUpBeforeClass(): void |
| 48 | + { |
| 49 | + self::$readme = file_get_contents(__DIR__.'/../README.md'); |
| 50 | + |
| 51 | + preg_match_all('/^#.*/m', self::$readme, $matches); |
| 52 | + self::$headers = implode("\n", $matches[0]); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param array<ReflectionClass> $interfaces |
| 57 | + */ |
| 58 | + private static function interfaceFilter(array $interfaces, ReflectionMethod $method): bool |
| 59 | + { |
| 60 | + foreach ($interfaces as $interface) { |
| 61 | + if ($interface->hasMethod($method->getName())) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + public static function provideMethods(): iterable |
| 70 | + { |
| 71 | + $reflection = new ReflectionClass(new Standard()); |
| 72 | + $interfaces = $reflection->getInterfaces(); |
| 73 | + |
| 74 | + return take($reflection->getMethods(ReflectionMethod::IS_PUBLIC)) |
| 75 | + ->filter(fn (ReflectionMethod $method) => self::interfaceFilter($interfaces, $method)) |
| 76 | + ->cast(fn (ReflectionMethod $method) => [$method->getName()]); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @dataProvider provideMethods |
| 81 | + */ |
| 82 | + public function testMethodHasMention(string $methodName): void |
| 83 | + { |
| 84 | + try { |
| 85 | + $this->assertMatchesRegularExpression( |
| 86 | + sprintf('/`%s\(\)`/', $methodName), |
| 87 | + self::$readme, |
| 88 | + "There's no mention of {$methodName}." |
| 89 | + ); |
| 90 | + } catch (ExpectationFailedException $e) { |
| 91 | + $message = $e->getMessage(); |
| 92 | + $this->fail(substr($message, 0, strpos($message, "\n"))); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @dataProvider provideMethods |
| 98 | + */ |
| 99 | + public function testMethodHasHeader(string $methodName): void |
| 100 | + { |
| 101 | + try { |
| 102 | + $this->assertMatchesRegularExpression( |
| 103 | + sprintf('/^##.*(->|`)%s\(\)`/m', $methodName), |
| 104 | + self::$headers, |
| 105 | + "There's no header dedicated to {$methodName}." |
| 106 | + ); |
| 107 | + } catch (ExpectationFailedException $e) { |
| 108 | + $message = $e->getMessage(); |
| 109 | + $this->markTestIncomplete(substr($message, 0, strpos($message, "\n"))); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments