|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MunicipioStyleGuide\Components\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +class UnusedTokensTest extends TestCase |
| 8 | +{ |
| 9 | + private const SHADOW_TOKEN = 'shadow'; |
| 10 | + private const SHADOW_REQUIRED_TOKENS = ['shadow-amount', 'shadow-color']; |
| 11 | + private const TOKEN_EXCEPTIONS = self::SHADOW_REQUIRED_TOKENS; |
| 12 | + |
| 13 | + /** |
| 14 | + * @testdox component utilizes all tokens declared in component.json |
| 15 | + * @dataProvider componentFilesProvider |
| 16 | + */ |
| 17 | + public function testComponentUtilizeAllTokens(string $component, string $tokenFile, string $styleFile): void |
| 18 | + { |
| 19 | + $tokens = self::extractTokensFromTokenFile($tokenFile); |
| 20 | + $styleFileContents = (string) file_get_contents($styleFile); |
| 21 | + |
| 22 | + self::assertShadowDependenciesAreDeclared($tokens, $styleFileContents, $component); |
| 23 | + $unusedTokens = self::findUnusedTokens($tokens, $styleFileContents, self::TOKEN_EXCEPTIONS); |
| 24 | + |
| 25 | + $errorMessage = sprintf("Component '%s' has declared but unused tokens in %s:%s- %s", $component, $styleFile, PHP_EOL, implode(PHP_EOL . '- ', $unusedTokens)); |
| 26 | + $this->assertEmpty($unusedTokens, $errorMessage); |
| 27 | + } |
| 28 | + |
| 29 | + private static function assertShadowDependenciesAreDeclared( |
| 30 | + array $tokens, |
| 31 | + string $styleFileContents, |
| 32 | + string $component, |
| 33 | + ): void { |
| 34 | + if (!self::styleFileUsesToken($styleFileContents, self::SHADOW_TOKEN)) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + $missingTokens = array_values(array_diff(self::SHADOW_REQUIRED_TOKENS, $tokens)); |
| 39 | + |
| 40 | + self::assertEmpty( |
| 41 | + $missingTokens, |
| 42 | + sprintf( |
| 43 | + "Component '%s' is missing required shadow tokens: %s", |
| 44 | + $component, |
| 45 | + implode(', ', $missingTokens), |
| 46 | + ), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + private static function findUnusedTokens(array $tokens, string $styleFileContents, array $excludedTokens): array |
| 51 | + { |
| 52 | + $unusedTokens = []; |
| 53 | + |
| 54 | + foreach ($tokens as $token) { |
| 55 | + if (in_array($token, $excludedTokens, true)) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if (!self::styleFileUsesToken($styleFileContents, $token)) { |
| 60 | + $unusedTokens[] = $token; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return $unusedTokens; |
| 65 | + } |
| 66 | + |
| 67 | + private static function styleFileUsesToken(string $styleFileContents, string $token): bool |
| 68 | + { |
| 69 | + // Match tokens.use/get(<anything>, '<token>'[, <optional args>]) |
| 70 | + $pattern = '/tokens\.(use|get)\s*\([^,]+,\s*[\'\"]' . preg_quote($token, '/') . '[\'\"](?:\s*,[^)]*)?\s*\)/m'; |
| 71 | + |
| 72 | + return preg_match($pattern, $styleFileContents) === 1; |
| 73 | + } |
| 74 | + |
| 75 | + private static function extractTokensFromTokenFile(string $tokenFile): array |
| 76 | + { |
| 77 | + $content = (string) file_get_contents($tokenFile); |
| 78 | + $data = json_decode($content, true); |
| 79 | + |
| 80 | + if (!is_array($data)) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + return $data['tokens'] ?? []; |
| 85 | + } |
| 86 | + |
| 87 | + public static function componentFilesProvider(): \Generator |
| 88 | + { |
| 89 | + $componentsDir = __DIR__ . '/../'; |
| 90 | + $componentExceptions = ['Tests']; // Exclude test directory itself |
| 91 | + $components = array_filter(scandir($componentsDir), function ($item) use ($componentsDir, $componentExceptions) { |
| 92 | + return is_dir($componentsDir . $item) && !in_array($item, ['.', '..', ...$componentExceptions], true); |
| 93 | + }); |
| 94 | + |
| 95 | + foreach ($components as $component) { |
| 96 | + $tokenFile = $componentsDir . $component . '/component.json'; |
| 97 | + $styleFile = $componentsDir . $component . '/style.scss'; |
| 98 | + |
| 99 | + yield $component => [$component, $tokenFile, $styleFile]; |
| 100 | + } |
| 101 | + |
| 102 | + return; |
| 103 | + } |
| 104 | +} |
0 commit comments