diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index 1c96009..c45d280 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -11,7 +11,6 @@ use Saucebase\Installer\Environments\Environment; use Saucebase\Installer\Environments\HerdEnvironment; use Saucebase\Installer\ModuleRegistry; -use Symfony\Component\Console\Output\OutputInterface; use function Laravel\Prompts\callout; use function Laravel\Prompts\select; @@ -300,81 +299,12 @@ public function promptForModules(): void return; } - if ($this->selectedStack) { - $available = $this->filterModulesByFramework($available, $this->selectedStack); - } - - if (empty($available)) { - return; - } - $this->selectedModules = $this->registry()->promptSelection($available); } - /** - * Refuse to install a module that ships no frontend for the chosen stack. - * - * @param string[] $packages - */ - public function assertModulesSupportStack(array $packages): bool - { - if (! $this->selectedStack || empty($packages)) { - return true; - } - - $incompatible = []; - - foreach ($packages as $package) { - $frameworks = $this->fetchPackageFrameworks($package); - - if (! in_array($this->selectedStack, $frameworks, true)) { - $incompatible[$package] = $frameworks; - } - } - - if (empty($incompatible)) { - return true; - } - - $this->error("These modules do not support the {$this->selectedStack} stack:"); - - foreach ($incompatible as $package => $frameworks) { - $this->line(" {$package} — supports: ".implode(', ', $frameworks)); - } - - $this->line('Drop them from --modules, or install with a stack they support.'); - - return false; - } - - /** - * @param string[] $packages - * @return string[] - */ - public function filterModulesByFramework(array $packages, string $framework): array - { - return array_values(array_filter( - $packages, - fn (string $pkg) => in_array($framework, $this->fetchPackageFrameworks($pkg), true) - )); - } - - /** - * @return string[] - */ - protected function fetchPackageFrameworks(string $package): array - { - return $this->registry()->frameworks($package); - } - protected function registry(): ModuleRegistry { - return $this->registry ??= new ModuleRegistry($this->modulesBasePath()); - } - - protected function modulesBasePath(): string - { - return $this->path('modules'); + return $this->registry ??= new ModuleRegistry; } public function install(): int @@ -672,173 +602,39 @@ protected function setupModules(): bool } /** - * Require, patch, migrate and seed the modules. Every driver runs these same steps; - * `$run` decides where a command runs (Docker passes one that runs it in the app - * container), defaulting to this machine. + * Core's `saucebase:module-install` requires, patches, migrates and seeds the + * modules. Every driver runs it; `$run` decides where (Docker passes one that runs + * it in the app container), defaulting to this machine. * * @param string[] $selected * @param (\Closure(string[] $command, int $timeout): bool)|null $run */ public function doInstallModules(array $selected, ?\Closure $run = null): bool { - $run ??= fn (array $command, int $timeout): bool => $command[0] === 'php' - ? $this->runArtisan(array_slice($command, 2), $timeout) - : $this->runProcess($command, $timeout); - - $this->newLine(); - - // Phase 1: require all selected packages in one Composer run - $ok = false; - $this->components->task('Installing modules', function () use ($selected, $run, &$ok) { - return $ok = $run(['composer', 'require', '--no-interaction', ...$selected], 300); - }); - - if (! $this->composerRequireSucceeded($ok, $selected)) { - $this->components->warn('Module installation failed — skipping patches, sync, and migrations.'); - - return false; - } - - // Phase 2: apply any patches the modules ship for the host app - if (! $this->applyModulePatches($selected)) { - return false; - } - - // Phase 3: sync module configs, then migrate + seed each module individually - $this->components->task('Syncing modules', fn () => $run(['php', 'artisan', 'modules:sync'], 30)); - - $this->components->task('Running module migrations', fn () => $run(['php', 'artisan', 'migrate', '--force'], 300)); - - foreach ($selected as $package) { - $name = Str::after($package, '/'); - - if (! $this->moduleHasSeeder($name)) { - continue; - } - - $this->components->task("Seeding {$name}", fn () => $run(['php', 'artisan', 'db:seed', "--module={$name}", '--force'], 60)); - } + $run ??= fn (array $command, int $timeout): bool => $this->runArtisan(array_slice($command, 2), $timeout); if ($this->option('demo')) { $this->demoPassword(); - $seeded = false; - $this->components->task('Loading demo content', function () use ($run, &$seeded) { - return $seeded = $run(['php', 'artisan', 'modules:seed', '--demo'], 300); - }); - - if (! $seeded) { - $this->components->warn('Loading demo content failed.'); - - return false; - } - } - - return true; - } - - /** - * Whether every requested package is actually installed, per composer.lock. - * - * `composer require` exits non-zero when any post-update-cmd script fails, even - * though the packages resolved and installed perfectly — the skeleton's documented - * `boost:update` hook does exactly that until Boost is set up. Judging module - * installation by the exit code alone turns that into a failed install, so ask - * what actually landed instead. - * - * @param string[] $packages - */ - public function modulesAreInstalled(array $packages): bool - { - $lock = json_decode((string) @file_get_contents($this->path('composer.lock')), true); - - if (! is_array($lock)) { - return false; - } - - $installed = array_column( - array_merge($lock['packages'] ?? [], $lock['packages-dev'] ?? []), - 'name', - ); - - return array_diff(array_map('strtolower', $packages), array_map('strtolower', $installed)) === []; - } - - /** - * Decide whether a non-zero `composer require` really failed, reporting either way. - * - * @param string[] $packages - */ - public function composerRequireSucceeded(bool $exitedCleanly, array $packages): bool - { - if ($exitedCleanly) { - return true; - } - - if (! $this->modulesAreInstalled($packages)) { - return false; } - $this->warn('Composer reported an error, but every module is installed — a post-install script failed.'); - $this->line(' If that was `boost:update`, run `php artisan boost:install` to configure Boost.'); - - return true; - } - - public function moduleHasSeeder(string $name): bool - { - $seederFile = 'database/seeders/DatabaseSeeder.php'; - - return file_exists($this->path('modules/'.strtolower($name).'/'.$seederFile)) - || file_exists($this->path('vendor/saucebase/'.strtolower($name).'/'.$seederFile)); - } - - /** - * Applies the patches each module ships to wire itself into the application. - * - * A patch already in place is skipped. One that does not apply stops the install: - * the module would be installed but not connected, and nothing else would say so. - * - * @param string[] $modules - */ - public function applyModulePatches(array $modules): bool - { - $conflicts = []; - - foreach ($modules as $package) { - $name = Str::after($package, '/'); - - foreach (["vendor/saucebase/{$name}/patches", "modules/{$name}/patches"] as $dir) { - foreach (glob($this->path($dir).'/*.patch') ?: [] as $patch) { - // Applies in reverse: the change is already there. - if ($this->gitApply($patch, '--reverse', '--check')) { - continue; - } - - if ($this->gitApply($patch)) { - // Routine wiring, so only with -v. Named by module: several ship a user.patch. - $this->info("Applied patch: {$name}/".basename($patch), OutputInterface::VERBOSITY_VERBOSE); - - continue; - } + $this->newLine(); - $conflicts[] = $dir.'/'.basename($patch); - } - } - } + $ok = false; + $this->components->task('Installing modules', function () use ($selected, $run, &$ok) { + return $ok = $run([ + 'php', 'artisan', 'saucebase:module-install', + ...array_map(fn (string $package) => Str::after($package, '/'), $selected), + '--no-interaction', + ...($this->option('demo') ? ['--demo'] : []), + ], 900); + }); - foreach ($conflicts as $patch) { - $this->error("The patch {$patch} does not apply to this application. " - .'Apply its changes by hand, then run the install again.'); + if (! $ok) { + $this->components->warn('Installing modules failed. Run it again to see why: php artisan saucebase:module-install ' + .implode(' ', array_map(fn (string $package) => Str::after($package, '/'), $selected))); } - return $conflicts === []; - } - - private function gitApply(string $patch, string ...$flags): bool - { - return Process::path($this->targetPath()) - ->run(['git', 'apply', '--whitespace=nowarn', ...$flags, $patch]) - ->successful(); + return $ok; } /** @@ -855,11 +651,9 @@ public function fetchAvailableModules(): array */ protected function resolveModuleSelection(array $available): array { - // 1. Select all modules (filtered to the chosen stack if one is set) + // 1. Select all modules if ($this->option('all-modules')) { - return $this->selectedStack - ? $this->filterModulesByFramework($available, $this->selectedStack) - : $available; + return $available; } // 2. Modules passed via --modules option diff --git a/src/Console/Commands/NewCommand.php b/src/Console/Commands/NewCommand.php index 36c09b0..c843a88 100644 --- a/src/Console/Commands/NewCommand.php +++ b/src/Console/Commands/NewCommand.php @@ -216,7 +216,7 @@ protected function resolveModulesUpfront(?string $stack): ?array } $registry = $this->registry(); - $available = $registry->filterByFramework($registry->available(), $stack); + $available = $registry->available(); return empty($available) ? [] : $registry->promptSelection($available); } diff --git a/src/Environments/Environment.php b/src/Environments/Environment.php index 75c518e..6bb3ab9 100644 --- a/src/Environments/Environment.php +++ b/src/Environments/Environment.php @@ -38,16 +38,6 @@ public function run(InstallCommand $command): int $command->promptForModules(); - // Only an explicit --modules= list reaches the installer unfiltered. The prompt - // and --all-modules paths already filter by framework, and checking them here - // would cost a needless Packagist round trip. - $explicit = $command->optionOrNull('modules'); - - if (is_string($explicit) && $explicit !== '' && $explicit !== 'none' - && ! $command->assertModulesSupportStack($this->resolveModules($command))) { - return $this->fail('Checking module compatibility'); - } - $result = $this->boot($command); if ($result === InstallCommand::SUCCESS) { @@ -162,11 +152,7 @@ abstract protected function boot(InstallCommand $command): int; protected function resolveModules(InstallCommand $command): array { if ($command->option('all-modules')) { - $available = $command->fetchAvailableModules(); - - return $command->getSelectedStack() - ? $command->filterModulesByFramework($available, $command->getSelectedStack()) - : $available; + return $command->fetchAvailableModules(); } if ($raw = $command->option('modules')) { diff --git a/src/ModuleRegistry.php b/src/ModuleRegistry.php index 280e907..182f5f1 100644 --- a/src/ModuleRegistry.php +++ b/src/ModuleRegistry.php @@ -8,19 +8,13 @@ use function Laravel\Prompts\multiselect; /** - * Discovers installable Saucebase modules on Packagist and the frontend - * frameworks each one supports. + * Discovers installable Saucebase modules on Packagist. */ class ModuleRegistry { /** @var string[] */ protected array $available = []; - /** @var array */ - protected array $frameworks = []; - - public function __construct(protected ?string $modulesPath = null) {} - /** @return string[] Fully-qualified package names, abandoned ones excluded. */ public function available(): array { @@ -43,53 +37,6 @@ public function available(): array )); } - /** @return string[] Frameworks the module supports; defaults to ['vue']. */ - public function frameworks(string $package): array - { - if (isset($this->frameworks[$package])) { - return $this->frameworks[$package]; - } - - $name = Str::after($package, '/'); - - if ($this->modulesPath !== null) { - $localManifest = $this->modulesPath."/{$name}/composer.json"; - - if (file_exists($localManifest)) { - $local = json_decode((string) file_get_contents($localManifest), true); - $frameworks = data_get($local, 'extra.saucebase.frameworks'); - - if (is_array($frameworks) && ! empty($frameworks)) { - return $this->frameworks[$package] = $frameworks; - } - } - } - - $response = Http::timeout(5)->get("https://raw.githubusercontent.com/saucebase-dev/{$name}/main/composer.json"); - - if ($response->ok()) { - $frameworks = data_get($response->json(), 'extra.saucebase.frameworks'); - - if (is_array($frameworks) && ! empty($frameworks)) { - return $this->frameworks[$package] = $frameworks; - } - } - - return $this->frameworks[$package] = ['vue']; - } - - /** - * @param string[] $packages - * @return string[] - */ - public function filterByFramework(array $packages, string $framework): array - { - return array_values(array_filter( - $packages, - fn (string $pkg) => in_array($framework, $this->frameworks($pkg), true) - )); - } - /** `saucebase/coming-soon` reads as "Coming Soon". */ public function label(string $package): string { diff --git a/tests/Feature/InstallCommandCoreTest.php b/tests/Feature/InstallCommandCoreTest.php index 485186c..b35ca57 100644 --- a/tests/Feature/InstallCommandCoreTest.php +++ b/tests/Feature/InstallCommandCoreTest.php @@ -134,10 +134,10 @@ public function test_the_framework_of_the_application_wins_over_the_one_asked_fo $this->assertSame('vue', $command->exposedCaptureStack('react')); } - public function test_a_module_that_composer_cannot_install_fails_the_install(): void + public function test_a_module_that_cannot_be_installed_fails_the_install(): void { $command = $this->command(); - $command->failing = 'composer require'; + $command->failing = 'php artisan saucebase:module-install'; $this->assertFalse($command->exposedDoInstallModules(['saucebase/auth'])); } @@ -167,6 +167,11 @@ public function runProcess(array $command, int $timeout = 300): bool return $this->failing === null || ! str_starts_with(implode(' ', $command), $this->failing); } + public function runArtisan(array $args, int $timeout = 120): bool + { + return $this->runProcess(['php', 'artisan', ...$args], $timeout); + } + public function argument($key = null): ?string { return $this->asked; diff --git a/tests/Feature/InstallCommandIdentityTest.php b/tests/Feature/InstallCommandIdentityTest.php index bb7de93..1e0c055 100644 --- a/tests/Feature/InstallCommandIdentityTest.php +++ b/tests/Feature/InstallCommandIdentityTest.php @@ -137,62 +137,4 @@ public function test_rejects_a_value_that_is_not_a_hostname(): void // Module / stack compatibility // ------------------------------------------------------------------------- - private function gate(?string $stack, array $fixtures): TestableInstallCommand - { - // Captures output instead of writing it: the gate reports through error()/line(), - // and no console output is bound to a directly-instantiated command. - $command = new class extends TestableInstallCommand - { - /** @var string[] */ - public array $messages = []; - - public function error($string, $verbosity = null): void - { - $this->messages[] = $string; - } - - public function line($string, $style = null, $verbosity = null): void - { - $this->messages[] = $string; - } - }; - - $command->setSelectedStack($stack); - $command->frameworkFixtures = $fixtures; - - return $command; - } - - public function test_accepts_modules_that_support_the_chosen_stack(): void - { - $command = $this->gate('react', [ - 'saucebase/auth' => ['vue', 'react'], - 'saucebase/settings' => ['react'], - ]); - - $this->assertTrue($command->assertModulesSupportStack(['saucebase/auth', 'saucebase/settings'])); - } - - public function test_rejects_a_module_that_does_not_support_the_chosen_stack(): void - { - $command = $this->gate('react', [ - 'saucebase/auth' => ['vue', 'react'], - 'saucebase/billing' => ['vue'], - ]); - - $this->assertFalse($command->assertModulesSupportStack(['saucebase/auth', 'saucebase/billing'])); - - // Must name the offender, not just fail. - $output = implode("\n", $command->messages); - $this->assertStringContainsString('saucebase/billing', $output); - $this->assertStringNotContainsString('saucebase/auth', $output); - } - - public function test_compatibility_gate_is_a_no_op_without_a_stack_or_modules(): void - { - $this->assertTrue($this->gate(null, ['saucebase/billing' => ['vue']]) - ->assertModulesSupportStack(['saucebase/billing'])); - - $this->assertTrue($this->gate('react', [])->assertModulesSupportStack([])); - } } diff --git a/tests/Feature/InstallCommandModulesTest.php b/tests/Feature/InstallCommandModulesTest.php deleted file mode 100644 index 35ba664..0000000 --- a/tests/Feature/InstallCommandModulesTest.php +++ /dev/null @@ -1,97 +0,0 @@ -dir = sys_get_temp_dir().'/sb-modules-'.uniqid(); - mkdir($this->dir, 0755, true); - } - - protected function tearDown(): void - { - @unlink($this->dir.'/composer.lock'); - @rmdir($this->dir); - parent::tearDown(); - } - - /** @param string[] $locked */ - private function command(array $locked, bool $writeLock = true): TestableInstallCommand - { - if ($writeLock) { - file_put_contents($this->dir.'/composer.lock', json_encode([ - 'packages' => array_map(fn (string $name) => ['name' => $name], $locked), - 'packages-dev' => [['name' => 'laravel/boost']], - ])); - } - - $command = new class extends TestableInstallCommand - { - /** @var string[] */ - public array $messages = []; - - public function warn($string, $verbosity = null): void - { - $this->messages[] = $string; - } - - public function line($string, $style = null, $verbosity = null): void - { - $this->messages[] = $string; - } - }; - $command->fakeOptions = ['path' => $this->dir]; - - return $command; - } - - public function test_a_failed_post_install_script_does_not_fail_the_module_install(): void - { - $command = $this->command(['saucebase/auth', 'saucebase/settings']); - - $this->assertTrue($command->composerRequireSucceeded(false, ['saucebase/auth', 'saucebase/settings'])); - $this->assertStringContainsString('boost:install', implode("\n", $command->messages)); - } - - public function test_a_genuinely_missing_package_still_fails(): void - { - $command = $this->command(['saucebase/auth']); - - $this->assertFalse($command->composerRequireSucceeded(false, ['saucebase/auth', 'saucebase/never-resolved'])); - } - - public function test_a_clean_exit_is_taken_at_face_value_without_reading_the_lock(): void - { - $command = $this->command([], writeLock: false); - - $this->assertTrue($command->composerRequireSucceeded(true, ['saucebase/auth'])); - $this->assertSame([], $command->messages, 'a clean run must stay quiet'); - } - - public function test_a_missing_or_unreadable_lock_file_is_treated_as_failure(): void - { - $command = $this->command([], writeLock: false); - - $this->assertFalse($command->composerRequireSucceeded(false, ['saucebase/auth'])); - } - - public function test_package_names_are_matched_case_insensitively(): void - { - $command = $this->command(['saucebase/Auth']); - - $this->assertTrue($command->composerRequireSucceeded(false, ['Saucebase/auth'])); - } -} diff --git a/tests/Feature/InstallCommandPatchesTest.php b/tests/Feature/InstallCommandPatchesTest.php deleted file mode 100644 index 9667c5d..0000000 --- a/tests/Feature/InstallCommandPatchesTest.php +++ /dev/null @@ -1,133 +0,0 @@ -allowRealProcess("'git' *"); - - $this->files = new Filesystem; - $this->dir = sys_get_temp_dir().'/sb-patches-'.uniqid(); - $this->files->ensureDirectoryExists($this->dir.'/app'); - $this->files->ensureDirectoryExists($this->dir.'/modules/auth/patches'); - - $this->files->put($this->dir.'/modules/auth/patches/user.patch', <<<'PATCH' - --- a/app/User.php - +++ b/app/User.php - @@ -1,3 +1,4 @@ - class User - { - + use Sociable; - } - - PATCH); - } - - protected function tearDown(): void - { - $this->files->deleteDirectory($this->dir); - - parent::tearDown(); - } - - public function test_a_patch_is_applied(): void - { - $this->files->put($this->dir.'/app/User.php', "class User\n{\n}\n"); - $command = $this->command(); - - $this->assertTrue($command->applyModulePatches(['saucebase/auth'])); - $this->assertStringContainsString('use Sociable;', $this->files->get($this->dir.'/app/User.php')); - } - - public function test_an_applied_patch_is_only_reported_when_verbose(): void - { - $this->files->put($this->dir.'/app/User.php', "class User\n{\n}\n"); - $command = $this->command(); - - $command->applyModulePatches(['saucebase/auth']); - - // Several modules ship a `user.patch`, so the line names the module. - $this->assertSame([['Applied patch: auth/user.patch', OutputInterface::VERBOSITY_VERBOSE]], $command->notes); - } - - public function test_a_patch_already_applied_is_skipped_without_a_warning(): void - { - $this->files->put($this->dir.'/app/User.php', "class User\n{\n use Sociable;\n}\n"); - $command = $this->command(); - - $this->assertTrue($command->applyModulePatches(['saucebase/auth'])); - $this->assertSame([], $command->problems); - $this->assertSame("class User\n{\n use Sociable;\n}\n", $this->files->get($this->dir.'/app/User.php')); - } - - public function test_a_patch_that_does_not_apply_fails_and_is_named(): void - { - $this->files->put($this->dir.'/app/User.php', "final class Account\n{\n // rewritten by the application\n}\n"); - $command = $this->command(); - - $this->assertFalse($command->applyModulePatches(['saucebase/auth'])); - $this->assertCount(1, $command->problems); - $this->assertStringContainsString('modules/auth/patches/user.patch', $command->problems[0]); - } - - public function test_modules_whose_patches_do_not_apply_fail_the_module_step(): void - { - $command = new class extends TestableInstallCommand - { - public function doInstallModules(array $selected, ?\Closure $run = null): bool - { - return false; - } - }; - $command->fakeOptions = ['path' => $this->dir, 'modules' => 'saucebase/auth']; - - $this->assertFalse($command->exposedSetupModules()); - } - - private function command(): TestableInstallCommand - { - $command = new class extends TestableInstallCommand - { - /** @var string[] */ - public array $problems = []; - - public function error($string, $verbosity = null): void - { - $this->problems[] = $string; - } - - public function warn($string, $verbosity = null): void - { - $this->problems[] = $string; - } - - /** @var array */ - public array $notes = []; - - public function info($string, $verbosity = null): void - { - $this->notes[] = [$string, $verbosity]; - } - }; - $command->fakeOptions = ['path' => $this->dir]; - - return $command; - } -} diff --git a/tests/Feature/InstallCommandTest.php b/tests/Feature/InstallCommandTest.php index 0d42740..d522476 100644 --- a/tests/Feature/InstallCommandTest.php +++ b/tests/Feature/InstallCommandTest.php @@ -19,130 +19,6 @@ protected function setUp(): void Http::preventStrayRequests(); } - // ------------------------------------------------------------------------- - // fetchPackageFrameworks - // ------------------------------------------------------------------------- - - public function test_fetch_package_frameworks_reads_saucebase_extra_field(): void - { - Http::fake([ - 'raw.githubusercontent.com/saucebase-dev/auth/main/composer.json' => Http::response([ - 'extra' => ['saucebase' => ['frameworks' => ['vue', 'react']]], - ]), - ]); - - $cmd = new TestableInstallCommand; - $this->assertSame(['vue', 'react'], $cmd->exposedFetchPackageFrameworks('saucebase/auth')); - } - - public function test_fetch_package_frameworks_defaults_to_vue_when_field_missing(): void - { - Http::fake([ - 'raw.githubusercontent.com/saucebase-dev/billing/main/composer.json' => Http::response([ - 'extra' => ['laravel' => ['providers' => []]], - ]), - ]); - - $cmd = new TestableInstallCommand; - $this->assertSame(['vue'], $cmd->exposedFetchPackageFrameworks('saucebase/billing')); - } - - public function test_fetch_package_frameworks_defaults_to_vue_on_api_failure(): void - { - Http::fake([ - 'raw.githubusercontent.com/saucebase-dev/no-such-module/main/composer.json' => Http::response([], 500), - ]); - - $cmd = new TestableInstallCommand; - $this->assertSame(['vue'], $cmd->exposedFetchPackageFrameworks('saucebase/no-such-module')); - } - - public function test_fetch_package_frameworks_reads_local_composer_json(): void - { - // No Http::fake() — Http::preventStrayRequests() will throw if HTTP is called - $tmpDir = sys_get_temp_dir().'/sb-install-test-'.uniqid(); - mkdir($tmpDir.'/test-fixture', 0755, true); - file_put_contents($tmpDir.'/test-fixture/composer.json', json_encode([ - 'extra' => ['saucebase' => ['frameworks' => ['vue', 'react']]], - ])); - - try { - $cmd = new TestableInstallCommand($tmpDir); - $this->assertSame(['vue', 'react'], $cmd->exposedFetchPackageFrameworks('saucebase/test-fixture')); - } finally { - unlink($tmpDir.'/test-fixture/composer.json'); - rmdir($tmpDir.'/test-fixture'); - rmdir($tmpDir); - } - } - - public function test_fetch_package_frameworks_falls_back_to_github_when_no_local_file(): void - { - Http::fake([ - 'raw.githubusercontent.com/saucebase-dev/billing/main/composer.json' => Http::response([ - 'extra' => ['saucebase' => ['frameworks' => ['vue', 'react']]], - ]), - ]); - - // No modules/billing/composer.json on disk → must fall back to GitHub - $cmd = new TestableInstallCommand; - $this->assertSame(['vue', 'react'], $cmd->exposedFetchPackageFrameworks('saucebase/billing')); - Http::assertSent(fn ($req) => str_contains($req->url(), 'saucebase-dev/billing')); - } - - // ------------------------------------------------------------------------- - // filterModulesByFramework - // ------------------------------------------------------------------------- - - public function test_filter_keeps_all_packages_for_vue(): void - { - $cmd = new TestableInstallCommand; - $cmd->frameworkFixtures = [ - 'saucebase/auth' => ['vue', 'react'], - 'saucebase/billing' => ['vue'], - 'saucebase/themes' => ['vue'], - ]; - - $result = $cmd->exposedFilterModulesByFramework( - ['saucebase/auth', 'saucebase/billing', 'saucebase/themes'], - 'vue' - ); - - $this->assertSame(['saucebase/auth', 'saucebase/billing', 'saucebase/themes'], $result); - } - - public function test_filter_removes_vue_only_packages_for_react(): void - { - $cmd = new TestableInstallCommand; - $cmd->frameworkFixtures = [ - 'saucebase/auth' => ['vue', 'react'], - 'saucebase/billing' => ['vue'], - 'saucebase/themes' => ['vue'], - ]; - - $result = $cmd->exposedFilterModulesByFramework( - ['saucebase/auth', 'saucebase/billing', 'saucebase/themes'], - 'react' - ); - - $this->assertSame(['saucebase/auth'], $result); - } - - public function test_filter_defaults_missing_field_to_vue_only(): void - { - $cmd = new TestableInstallCommand; - - Http::fake([ - 'raw.githubusercontent.com/saucebase-dev/billing/main/composer.json' => Http::response([ - 'extra' => [], - ]), - ]); - - $result = $cmd->exposedFilterModulesByFramework(['saucebase/billing'], 'react'); - - $this->assertSame([], $result, 'Module without frameworks field should not appear for react'); - } - // ------------------------------------------------------------------------- // Deferred stack — saucebase:stack must NOT run during captureStack() // ------------------------------------------------------------------------- @@ -536,24 +412,16 @@ public function call($command, array $arguments = [], $outputBuffer = null): int // resolveModuleSelection — --all-modules framework filtering // ------------------------------------------------------------------------- - public function test_all_modules_filters_by_selected_stack(): void + public function test_all_modules_takes_every_module_whatever_the_stack(): void { + // A private module's stacks can't be read before it is installed, so none is refused. $cmd = new TestableInstallCommand; - $cmd->setSelectedStack('vue'); + $cmd->setSelectedStack('react'); $cmd->fakeOptions = ['all-modules' => true]; - $cmd->frameworkFixtures = [ - 'saucebase/auth' => ['vue', 'react'], - 'saucebase/billing' => ['vue'], - 'saucebase/react-only' => ['react'], - ]; - $result = $cmd->exposedResolveModuleSelection( - ['saucebase/auth', 'saucebase/billing', 'saucebase/react-only'] - ); + $result = $cmd->exposedResolveModuleSelection(['saucebase/auth', 'saucebase/tenancy']); - $this->assertContains('saucebase/auth', $result); - $this->assertContains('saucebase/billing', $result); - $this->assertNotContains('saucebase/react-only', $result, '--all-modules with vue stack must exclude react-only modules'); + $this->assertSame(['saucebase/auth', 'saucebase/tenancy'], $result); } public function test_all_modules_without_stack_returns_all(): void @@ -561,10 +429,6 @@ public function test_all_modules_without_stack_returns_all(): void $cmd = new TestableInstallCommand; $cmd->setSelectedStack(null); $cmd->fakeOptions = ['all-modules' => true]; - $cmd->frameworkFixtures = [ - 'saucebase/auth' => ['vue'], - 'saucebase/react-only' => ['react'], - ]; $result = $cmd->exposedResolveModuleSelection( ['saucebase/auth', 'saucebase/react-only'] @@ -617,37 +481,6 @@ public function test_resolve_matches_multiple_full_package_names(): void $this->assertSame(['saucebase/auth', 'saucebase/billing'], $result); } - // ------------------------------------------------------------------------- - // moduleHasSeeder — vendor/ path - // ------------------------------------------------------------------------- - - public function test_module_has_seeder_checks_vendor_path(): void - { - $name = 'sb-test-seeder-'.uniqid(); - $appDir = sys_get_temp_dir().'/sb-install-test-'.uniqid(); - $vendorSeederDir = $appDir."/vendor/saucebase/{$name}/database/seeders"; - @mkdir($vendorSeederDir, 0755, true); - file_put_contents($vendorSeederDir.'/DatabaseSeeder.php', 'fakeOptions = ['path' => $appDir]; - $this->assertFalse( - file_exists($appDir."/modules/{$name}/database/seeders/DatabaseSeeder.php"), - 'modules/ path must not exist for this test to be valid' - ); - $this->assertTrue($cmd->moduleHasSeeder($name), 'moduleHasSeeder() must detect seeder in vendor/saucebase/'); - } finally { - @unlink($vendorSeederDir.'/DatabaseSeeder.php'); - @rmdir($vendorSeederDir); - @rmdir($appDir."/vendor/saucebase/{$name}/database"); - @rmdir($appDir."/vendor/saucebase/{$name}"); - @rmdir($appDir.'/vendor/saucebase'); - @rmdir($appDir.'/vendor'); - @rmdir($appDir); - } - } - // ------------------------------------------------------------------------- // displaySuccess — sequential step numbering // ------------------------------------------------------------------------- diff --git a/tests/Feature/InstallModulesRunnerTest.php b/tests/Feature/InstallModulesRunnerTest.php index 0430d73..9ead205 100644 --- a/tests/Feature/InstallModulesRunnerTest.php +++ b/tests/Feature/InstallModulesRunnerTest.php @@ -59,24 +59,21 @@ public function test_every_step_runs_through_the_given_runner(): void $command = $this->command(); $ran = []; - $ok = $command->installWith(['saucebase/blog'], function (array $args) use (&$ran): bool { + $ok = $command->installWith(['saucebase/blog', 'saucebase/roadmap'], function (array $args) use (&$ran): bool { $ran[] = implode(' ', $args); return true; }); + // Core's saucebase:module-install does the rest: require, patches, migrate, seed. $this->assertTrue($ok); - $this->assertSame([ - 'composer require --no-interaction saucebase/blog', - 'php artisan modules:sync', - 'php artisan migrate --force', - 'php artisan modules:seed --demo', - ], $ran); + $this->assertSame(['php artisan saucebase:module-install blog roadmap --no-interaction --demo'], $ran); + $this->assertStringContainsString('DEMO_PASSWORD=', (string) file_get_contents($this->tmp.'/.env')); } - public function test_a_failed_demo_seed_fails_the_module_install(): void + public function test_a_failed_module_install_fails_the_step(): void { - $ok = $this->command()->installWith(['saucebase/blog'], fn (array $args): bool => ! in_array('--demo', $args, true)); + $ok = $this->command()->installWith(['saucebase/blog'], fn (array $args): bool => false); $this->assertFalse($ok); } diff --git a/tests/Feature/TestableInstallCommand.php b/tests/Feature/TestableInstallCommand.php index 4956c15..60981a7 100644 --- a/tests/Feature/TestableInstallCommand.php +++ b/tests/Feature/TestableInstallCommand.php @@ -6,11 +6,6 @@ class TestableInstallCommand extends InstallCommand { - /** @var array Pre-built framework map (overrides HTTP for filtering tests). */ - public array $frameworkFixtures = []; - - private ?string $customModulesBasePath; - public function setSelectedStack(?string $stack): void { $this->selectedStack = $stack; @@ -22,23 +17,6 @@ public function setSelectedModules(array $modules): void $this->selectedModules = $modules; } - public function __construct(?string $modulesBasePath = null) - { - parent::__construct(); - $this->customModulesBasePath = $modulesBasePath; - } - - public function exposedFetchPackageFrameworks(string $package): array - { - return $this->fetchPackageFrameworks($package); - } - - /** @param string[] $packages */ - public function exposedFilterModulesByFramework(array $packages, string $framework): array - { - return $this->filterModulesByFramework($packages, $framework); - } - public function exposedSuccessCalloutContent(array $steps): array { return $this->successCalloutContent($steps); @@ -82,18 +60,4 @@ public function doInstallModules(array $selected, ?\Closure $run = null): bool // no-op — prevents composer require from running in unit tests return true; } - - protected function fetchPackageFrameworks(string $package): array - { - if (isset($this->frameworkFixtures[$package])) { - return $this->frameworkFixtures[$package]; - } - - return parent::fetchPackageFrameworks($package); - } - - protected function modulesBasePath(): string - { - return $this->customModulesBasePath ?? parent::modulesBasePath(); - } } diff --git a/tests/Unit/ModuleRegistryTest.php b/tests/Unit/ModuleRegistryTest.php index 0b52950..ee09e6a 100644 --- a/tests/Unit/ModuleRegistryTest.php +++ b/tests/Unit/ModuleRegistryTest.php @@ -2,7 +2,6 @@ namespace Saucebase\Installer\Tests\Unit; -use Illuminate\Support\Facades\Http; use PHPUnit\Framework\Attributes\DataProvider; use Saucebase\Installer\ModuleRegistry; use Saucebase\Installer\Tests\TestCase; @@ -23,40 +22,4 @@ public static function labels(): array ['saucebase/auth-pro', 'Auth Pro'], ]; } - - public function test_a_local_module_names_its_frameworks_without_asking_the_network(): void - { - Http::fake(['*' => Http::response(['extra' => ['saucebase' => ['frameworks' => ['vue']]]])]); - $modules = $this->localModule('auth', ['react']); - - $this->assertSame(['react'], (new ModuleRegistry($modules))->frameworks('saucebase/auth')); - Http::assertNothingSent(); - } - - /** An empty list says nothing, so the published manifest decides. */ - public function test_a_local_module_with_an_empty_framework_list_falls_back_to_the_published_one(): void - { - Http::fake(['*' => Http::response(['extra' => ['saucebase' => ['frameworks' => ['react']]]])]); - $modules = $this->localModule('auth', []); - - $this->assertSame(['react'], (new ModuleRegistry($modules))->frameworks('saucebase/auth')); - } - - public function test_a_published_empty_framework_list_means_vue(): void - { - Http::fake(['*' => Http::response(['extra' => ['saucebase' => ['frameworks' => []]]])]); - - $this->assertSame(['vue'], (new ModuleRegistry)->frameworks('saucebase/auth')); - } - - /** @param string[] $frameworks */ - private function localModule(string $name, array $frameworks): string - { - $dir = sys_get_temp_dir().'/sb-registry-'.uniqid(); - mkdir("{$dir}/{$name}", 0755, true); - file_put_contents("{$dir}/{$name}/composer.json", json_encode(['extra' => ['saucebase' => ['frameworks' => $frameworks]]])); - register_shutdown_function(fn () => exec('rm -rf '.escapeshellarg($dir))); - - return $dir; - } }