|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WooCommerceSubscriptionsStubs\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * Smoke checks over the generated stubs. |
| 11 | + * |
| 12 | + * A stubs package fails quietly: a regeneration truncates the output, or produces a file that no |
| 13 | + * longer parses, and nothing here looks wrong until a consumer's static analysis reports symbols |
| 14 | + * that should exist. These assert the two properties a generated file must hold whatever it |
| 15 | + * contains -- it parses, and it actually declares something. |
| 16 | + */ |
| 17 | +final class StubsTest extends TestCase |
| 18 | +{ |
| 19 | + private const ROOT = __DIR__ . '/..'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Every generated stub file in the repository root. |
| 23 | + * |
| 24 | + * Discovered rather than listed, so a repository that grows a second stub file gets it covered |
| 25 | + * without anyone remembering to add it here. |
| 26 | + * |
| 27 | + * @return array<string, array{0: string}> |
| 28 | + */ |
| 29 | + public function stubFileProvider(): array |
| 30 | + { |
| 31 | + $cases = []; |
| 32 | + |
| 33 | + foreach ((array) glob(self::ROOT . '/*.stub') as $path) { |
| 34 | + $cases[basename((string) $path)] = [basename((string) $path)]; |
| 35 | + } |
| 36 | + |
| 37 | + return $cases; |
| 38 | + } |
| 39 | + |
| 40 | + public function testTheRepositoryShipsAtLeastOneStubFile(): void |
| 41 | + { |
| 42 | + $this->assertNotEmpty($this->stubFileProvider(), 'no .stub files found -- generation produced nothing'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @dataProvider stubFileProvider |
| 47 | + */ |
| 48 | + public function testTheStubFileIsValidPhp(string $file): void |
| 49 | + { |
| 50 | + $path = self::ROOT . '/' . $file; |
| 51 | + |
| 52 | + $this->assertFileExists($path); |
| 53 | + |
| 54 | + // TOKEN_PARSE makes the tokenizer raise ParseError on invalid source rather than returning a |
| 55 | + // best-effort token list, which is what makes this an actual syntax check. |
| 56 | + $this->assertNotEmpty(token_get_all((string) file_get_contents($path), TOKEN_PARSE)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @dataProvider stubFileProvider |
| 61 | + */ |
| 62 | + public function testTheStubFileDeclaresSomething(string $file): void |
| 63 | + { |
| 64 | + if (strpos($file, 'constants') !== false) { |
| 65 | + $this->markTestSkipped('a constants stub is legitimately empty when the plugin defines none'); |
| 66 | + } |
| 67 | + |
| 68 | + $source = (string) file_get_contents(self::ROOT . '/' . $file); |
| 69 | + |
| 70 | + // A generated file that parses but contains only its header is the shape a truncated or |
| 71 | + // failed generation takes -- valid PHP, and useless. |
| 72 | + foreach (['function ', 'class ', 'interface ', 'trait ', 'define(', 'const '] as $needle) { |
| 73 | + if (stripos($source, $needle) !== false) { |
| 74 | + $this->addToAssertionCount(1); |
| 75 | + |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $this->fail($file . ' parses but declares nothing -- a truncated or empty generation'); |
| 81 | + } |
| 82 | +} |
0 commit comments