|
| 1 | +<?php |
| 2 | + |
| 3 | +use EvolutionCMS\Console\Packages\PackageCommand; |
| 4 | +use Illuminate\Console\OutputStyle; |
| 5 | +use Symfony\Component\Console\Input\ArrayInput; |
| 6 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 7 | + |
| 8 | +if (!defined('EVO_CORE_PATH')) { |
| 9 | + define('EVO_CORE_PATH', dirname(__DIR__, 3) . '/'); |
| 10 | +} |
| 11 | + |
| 12 | +function buildStaleProviderCommand(string $configDir): array |
| 13 | +{ |
| 14 | + $command = (new ReflectionClass(PackageCommand::class))->newInstanceWithoutConstructor(); |
| 15 | + |
| 16 | + $configProperty = new ReflectionProperty(PackageCommand::class, 'configDir'); |
| 17 | + $configProperty->setAccessible(true); |
| 18 | + $configProperty->setValue($command, $configDir); |
| 19 | + |
| 20 | + $buffer = new BufferedOutput(); |
| 21 | + $outputProperty = new ReflectionProperty(PackageCommand::class, 'output'); |
| 22 | + $outputProperty->setAccessible(true); |
| 23 | + $outputProperty->setValue($command, new OutputStyle(new ArrayInput([]), $buffer)); |
| 24 | + |
| 25 | + return [$command, $buffer]; |
| 26 | +} |
| 27 | + |
| 28 | +function removeStaleProviderDir(string $dir): void |
| 29 | +{ |
| 30 | + foreach (glob($dir . '*.php') ?: [] as $file) { |
| 31 | + unlink($file); |
| 32 | + } |
| 33 | + if (is_dir($dir)) { |
| 34 | + rmdir($dir); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +beforeEach(function () { |
| 39 | + $this->providersDir = sys_get_temp_dir() . '/evo-stale-providers-' . uniqid() . '/'; |
| 40 | + mkdir($this->providersDir, 0775, true); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(function () { |
| 44 | + removeStaleProviderDir($this->providersDir); |
| 45 | +}); |
| 46 | + |
| 47 | +test('package discovery removes provider config files whose class no longer exists', function () { |
| 48 | + file_put_contents($this->providersDir . 'sCommerceServiceProvider.php', "<?php \nreturn Seiger\sCommerce\sCommerceServiceProvider::class;"); |
| 49 | + file_put_contents($this->providersDir . '001_MissingServiceProvider.php', "<?php\n// Priority 1\nreturn Vendor\Missing\MissingServiceProvider::class;"); |
| 50 | + file_put_contents($this->providersDir . 'Evolution_Auth.php', "<?php \nreturn EvolutionCMS\Providers\AuthServiceProvider::class;"); |
| 51 | + file_put_contents($this->providersDir . 'Broken.php', "<?php\nreturn ['not', 'a', 'class'];"); |
| 52 | + |
| 53 | + [$command, $buffer] = buildStaleProviderCommand($this->providersDir); |
| 54 | + |
| 55 | + $method = new ReflectionMethod(PackageCommand::class, 'cleanupProviders'); |
| 56 | + $method->setAccessible(true); |
| 57 | + $method->invoke($command); |
| 58 | + |
| 59 | + expect(file_exists($this->providersDir . 'sCommerceServiceProvider.php'))->toBeFalse() |
| 60 | + ->and(file_exists($this->providersDir . '001_MissingServiceProvider.php'))->toBeFalse() |
| 61 | + ->and(file_exists($this->providersDir . 'Evolution_Auth.php'))->toBeTrue() |
| 62 | + ->and(file_exists($this->providersDir . 'Broken.php'))->toBeTrue() |
| 63 | + ->and($buffer->fetch())->toContain('Removed stale provider config sCommerceServiceProvider.php'); |
| 64 | +}); |
| 65 | + |
| 66 | +test('package discovery keeps provider files generated during the current run', function () { |
| 67 | + file_put_contents($this->providersDir . 'FreshServiceProvider.php', "<?php \nreturn Vendor\Fresh\FreshServiceProvider::class;"); |
| 68 | + |
| 69 | + [$command] = buildStaleProviderCommand($this->providersDir); |
| 70 | + |
| 71 | + $discovered = new ReflectionProperty(PackageCommand::class, 'discoveredProviderFiles'); |
| 72 | + $discovered->setAccessible(true); |
| 73 | + $discovered->setValue($command, ['FreshServiceProvider.php' => true]); |
| 74 | + |
| 75 | + $method = new ReflectionMethod(PackageCommand::class, 'cleanupProviders'); |
| 76 | + $method->setAccessible(true); |
| 77 | + $method->invoke($command); |
| 78 | + |
| 79 | + expect(file_exists($this->providersDir . 'FreshServiceProvider.php'))->toBeTrue(); |
| 80 | +}); |
| 81 | + |
| 82 | +test('configured provider registration skips classes that do not exist', function () { |
| 83 | + $source = (string) file_get_contents(dirname(__DIR__, 3) . '/src/AbstractLaravel.php'); |
| 84 | + |
| 85 | + expect($source) |
| 86 | + ->toContain('public function registerConfiguredProviders()') |
| 87 | + ->toContain('class_exists($provider)') |
| 88 | + ->toContain('Skipped missing service provider'); |
| 89 | +}); |
0 commit comments