|
24 | 24 | | |
25 | 25 | */ |
26 | 26 |
|
| 27 | +use Cerbero\Enum\Enums; |
| 28 | + |
27 | 29 | use function Cerbero\Enum\className; |
28 | 30 | use function Cerbero\Enum\cli; |
29 | 31 | use function Cerbero\Enum\namespaceToPath; |
| 32 | +use function Cerbero\Enum\path; |
30 | 33 |
|
31 | 34 | expect()->extend('toAnnotate', function (array $enums, bool $overwrite = false) { |
32 | 35 | $oldContents = []; |
|
38 | 41 | } |
39 | 42 |
|
40 | 43 | try { |
41 | | - if (is_bool($value = ($this->value)())) { |
42 | | - expect($value)->toBeTrue(); |
43 | | - } else { |
44 | | - expect($value) |
45 | | - ->output->toContain(...$enums) |
46 | | - ->status->toBe(0); |
47 | | - } |
| 44 | + expect($this->value)->toProcessEnums($enums); |
48 | 45 |
|
49 | 46 | foreach ($oldContents as $filename => $oldContent) { |
50 | 47 | $stub = __DIR__ . '/stubs/annotate/' . basename($filename, '.php') . '.stub'; |
|
53 | 50 | $stub = $path; |
54 | 51 | } |
55 | 52 |
|
56 | | - // normalize content to avoid end-of-line incompatibility between OS |
57 | | - $enumContent = str_replace("\r\n", "\n", file_get_contents($filename)); |
58 | | - $stubContent = str_replace("\r\n", "\n", file_get_contents($stub)); |
59 | | - |
60 | | - expect($enumContent)->toBe($stubContent); |
| 53 | + expect($filename)->toContainIgnoreEol($stub); |
61 | 54 | } |
62 | 55 | } finally { |
63 | 56 | foreach ($oldContents as $filename => $oldContent) { |
|
66 | 59 | } |
67 | 60 | }); |
68 | 61 |
|
69 | | -expect()->extend('toGenerate', function (string $enum) { |
70 | | - expect(class_exists($enum))->toBeFalse(); |
| 62 | +expect()->extend('toProcessEnums', function (array $enums) { |
| 63 | + $value = ($this->value)(); |
| 64 | + |
| 65 | + if (is_bool($value)) { |
| 66 | + expect($value)->toBeTrue(); |
| 67 | + } else { |
| 68 | + expect($value) |
| 69 | + ->output->toContain(...$enums) |
| 70 | + ->status->toBe(0); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +expect()->extend('toContainIgnoreEol', function (string $path) { |
| 75 | + // normalize content to avoid end-of-line incompatibility between OS |
| 76 | + $actualContent = str_replace("\r\n", "\n", file_get_contents(path($this->value))); |
| 77 | + $expectedContent = str_replace("\r\n", "\n", file_get_contents(path($path))); |
| 78 | + |
| 79 | + expect($actualContent)->toBe($expectedContent); |
| 80 | +}); |
71 | 81 |
|
72 | | - $directory = 'make'; |
| 82 | +expect()->extend('toGenerate', function (string $enum, bool $cli = false) { |
| 83 | + expect(class_exists($enum))->toBeFalse(); |
73 | 84 |
|
74 | 85 | try { |
75 | | - if (is_bool($value = ($this->value)())) { |
76 | | - expect($value)->toBeTrue(); |
77 | | - |
78 | | - $directory = 'generator'; |
79 | | - } else { |
80 | | - expect($value) |
81 | | - ->output->toContain($enum) |
82 | | - ->status->toBe(0); |
83 | | - } |
| 86 | + expect($this->value)->toProcessEnums([$enum]); |
84 | 87 |
|
85 | 88 | $filename = namespaceToPath($enum); |
| 89 | + $directory = $cli ? 'make' : 'generator'; |
86 | 90 | $stub = sprintf('%s/stubs/%s/%s.stub', __DIR__, $directory, className($enum)); |
87 | 91 |
|
88 | | - // normalize content to avoid end-of-line incompatibility between OS |
89 | | - $enumContent = str_replace("\r\n", "\n", file_get_contents($filename)); |
90 | | - $stubContent = str_replace("\r\n", "\n", file_get_contents($stub)); |
91 | | - |
92 | | - expect($enumContent)->toBe($stubContent); |
| 92 | + expect($filename)->toContainIgnoreEol($stub); |
93 | 93 | } finally { |
94 | 94 | file_exists($filename) && unlink($filename); |
95 | 95 | } |
96 | 96 | }); |
97 | 97 |
|
| 98 | +expect()->extend('toTypeScript', function (array $enums, bool $oneFile = true) { |
| 99 | + expect($this->value)->toProcessEnums($enums); |
| 100 | + |
| 101 | + $paths = []; |
| 102 | + |
| 103 | + try { |
| 104 | + foreach ($enums as $enum) { |
| 105 | + $paths[] = $path = Enums::basePath(Enums::typeScript($enum)); |
| 106 | + $stub = sprintf('%s/stubs/ts/%s.stub', __DIR__, $oneFile ? 'enums' : className($enum)); |
| 107 | + |
| 108 | + expect($path)->toContainIgnoreEol($stub); |
| 109 | + } |
| 110 | + } finally { |
| 111 | + foreach ($paths as $path) { |
| 112 | + file_exists($path) && unlink($path); |
| 113 | + } |
| 114 | + } |
| 115 | +}); |
| 116 | + |
98 | 117 | /* |
99 | 118 | |-------------------------------------------------------------------------- |
100 | 119 | | Functions |
|
113 | 132 | */ |
114 | 133 | function runEnum(string $command): stdClass |
115 | 134 | { |
| 135 | + $paths = sprintf('--base-path="%s" --paths="%s"', Enums::basePath(), implode(',', Enums::paths())); |
| 136 | + |
116 | 137 | ob_start(); |
117 | 138 |
|
118 | | - cli($command, $status); |
| 139 | + cli("{$command} {$paths}", $status); |
119 | 140 |
|
120 | 141 | $output = ob_get_clean(); |
121 | 142 |
|
|
0 commit comments