diff --git a/CHANGELOG-8.x.md b/CHANGELOG-8.x.md index 2973309..9dab287 100644 --- a/CHANGELOG-8.x.md +++ b/CHANGELOG-8.x.md @@ -2,6 +2,13 @@ This changelog references the relevant changes (bug and security fixes) done to `orchestra/workbench`. +## 8.15.2 + +Released: 2024-12-19 + +### Fixes + +* Fix generated namespace via `workbench:install`. ## 8.15.1 diff --git a/composer.json b/composer.json index 4729d68..a56f033 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "laravel/tinker": "^2.9", "nunomaduro/collision": "^8.0", "orchestra/canvas": "^9.1", - "orchestra/testbench-core": "^9.8", + "orchestra/testbench-core": "^9.8.2", "symfony/polyfill-php83": "^1.31", "symfony/polyfill-php84": "^1.31", "symfony/process": "^7.0.3", diff --git a/src/Console/DevToolCommand.php b/src/Console/DevToolCommand.php index c94c426..fbd633f 100644 --- a/src/Console/DevToolCommand.php +++ b/src/Console/DevToolCommand.php @@ -43,8 +43,8 @@ public function handle(Filesystem $filesystem) event(new InstallStarted($this->input, $this->output, $this->components)); - $this->prepareWorkbenchDirectories($filesystem, $workingPath); $this->prepareWorkbenchNamespaces($filesystem, $workingPath); + $this->prepareWorkbenchDirectories($filesystem, $workingPath); if ($this->option('install') === true) { $this->call('workbench:install', [ @@ -115,6 +115,8 @@ protected function prepareWorkbenchNamespaces(Filesystem $filesystem, string $wo ->handle(fn (array $content) => $this->appendScriptsToComposer( $this->appendAutoloadDevToComposer($content, $filesystem), $filesystem )); + + Workbench::flushCachedClassAndNamespaces(); } /** @@ -141,11 +143,23 @@ protected function prepareWorkbenchDatabaseSchema(Filesystem $filesystem, string join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php') ); + $workbenchSeederNamespacePrefix = rtrim(Workbench::detectNamespace('database/seeders') ?? 'Workbench\Database\Seeders\\', '\\'); + + $filesystem->replaceInFile([ + '{{WorkbenchSeederNamespace}}', + '{{ WorkbenchSeederNamespace }}', + 'Workbench\Database\Seeders', + ], [ + $workbenchSeederNamespacePrefix, + $workbenchSeederNamespacePrefix, + $workbenchSeederNamespacePrefix, + ], join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php')); + if ($filesystem->exists(join_paths($workingPath, 'database', 'factories', 'UserFactory.php'))) { $filesystem->replaceInFile([ 'use Orchestra\Testbench\Factories\UserFactory;', ], [ - 'use Workbench\Database\Factories\UserFactory;', + \sprintf('use %sUserFactory;', Workbench::detectNamespace('database/factories') ?? 'Workbench\Database\Factories\\'), ], join_paths($workingPath, 'database', 'seeders', 'DatabaseSeeder.php')); } } diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 75a3f0c..7d0a087 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -120,11 +120,11 @@ protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string force: (bool) $this->option('force'), ))->handle($from, $to); - $workbenchAppNamespacePrefix = Workbench::detectNamespace('app', force: true) ?? 'Workbench\App\\'; - $workbenchSeederNamespacePrefix = Workbench::detectNamespace('database/seeders', force: true) ?? 'Workbench\Database\Seeders\\'; + $workbenchAppNamespacePrefix = rtrim(Workbench::detectNamespace('app') ?? 'Workbench\App\\', '\\'); + $workbenchSeederNamespacePrefix = rtrim(Workbench::detectNamespace('database/seeders') ?? 'Workbench\Database\Seeders\\', '\\'); - $serviceProvider = \sprintf('%sProviders\WorkbenchServiceProvider', $workbenchAppNamespacePrefix); - $databaseSeeder = \sprintf('%sDatabaseSeeder', $workbenchSeederNamespacePrefix); + $serviceProvider = \sprintf('%s\Providers\WorkbenchServiceProvider', $workbenchAppNamespacePrefix); + $databaseSeeder = \sprintf('%s\DatabaseSeeder', $workbenchSeederNamespacePrefix); $filesystem->replaceInFile( [ @@ -140,8 +140,6 @@ protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string '{{WorkbenchDatabaseSeeder}}', '{{ WorkbenchDatabaseSeeder }}', 'Workbench\Database\Seeders\DatabaseSeeder', - - ' - migrate-fresh', ], [ $workbenchAppNamespacePrefix, @@ -156,10 +154,6 @@ protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string $databaseSeeder, $databaseSeeder, $databaseSeeder, - - $databaseSeeder === 'Database\Seeders\DatabaseSeeder' - ? ' - migrate-fresh' - : ' - migrate-fresh:'.PHP_EOL.' --seed: true', ], $to ); diff --git a/tests/Console/CommandTestCase.php b/tests/Console/CommandTestCase.php index eb74c83..a086569 100644 --- a/tests/Console/CommandTestCase.php +++ b/tests/Console/CommandTestCase.php @@ -2,13 +2,14 @@ namespace Orchestra\Workbench\Tests\Console; +use Database\Seeders\DatabaseSeeder; use Illuminate\Filesystem\Filesystem; use Orchestra\Canvas\LaravelServiceProvider; use Orchestra\Testbench\Foundation\Config; use Orchestra\Testbench\Foundation\TestbenchServiceProvider; use Orchestra\Workbench\Workbench; use Orchestra\Workbench\WorkbenchServiceProvider; -use Workbench\Database\Seeders\DatabaseSeeder; +use Workbench\Database\Seeders\DatabaseSeeder as WorkbenchDatabaseSeeder; use function Orchestra\Testbench\default_skeleton_path; use function Orchestra\Testbench\join_paths; @@ -31,6 +32,7 @@ protected function setUp(): void $_ENV['TESTBENCH_WORKING_PATH'] = $workingPath; $filesystem->ensureDirectoryExists($workingPath); $filesystem->copy(join_paths(__DIR__, 'stubs', 'composer.json'), join_paths($workingPath, 'composer.json')); + $filesystem->copy(join_paths(__DIR__, 'stubs', 'phpunit.xml.dist'), join_paths($workingPath, 'phpunit.xml.dist')); parent::setUp(); } @@ -49,7 +51,7 @@ protected function getPackageProviders($app) /** * Assert `workbench:devtool` or `workbench:install --devtool` command executed. */ - protected function assertCommandExecutedWithDevTool(): void + protected function assertCommandExecutedWithDevTool(bool $prefix = true): void { $workingPath = static::stubWorkingPath(); @@ -57,12 +59,36 @@ protected function assertCommandExecutedWithDevTool(): void $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'app', 'Providers')); $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'database', 'factories')); $this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'database', 'seeders')); + + $this->assertFileContains([ + \sprintf('namespace %sModels;', $prefix ? 'Workbench\App\\' : 'App\\'), + 'class User extends Authenticatable', + ], join_paths($workingPath, 'workbench', 'app', 'Models', 'User.php')); + + $this->assertFileContains([ + \sprintf('namespace %sProviders;', $prefix ? 'Workbench\App\\' : 'App\\'), + 'class WorkbenchServiceProvider extends ServiceProvider', + ], join_paths($workingPath, 'workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php')); + + $this->assertFileContains([ + \sprintf('namespace %sFactories;', $prefix ? 'Workbench\Database\\' : 'Database\\'), + \sprintf('use %sModels\User;', $prefix ? 'Workbench\App\\' : 'App\\'), + 'class UserFactory extends Factory', + ], join_paths($workingPath, 'workbench', 'database', 'factories', 'UserFactory.php')); + + $this->assertFileContains([ + \sprintf('namespace %sSeeders;', $prefix ? 'Workbench\Database\\' : 'Database\\'), + \sprintf('use %sFactories\UserFactory;', $prefix ? 'Workbench\Database\\' : 'Database\\'), + 'class DatabaseSeeder extends Seeder', + '// UserFactory::new()->times(10)->create();', + '// UserFactory::new()->create([', + ], join_paths($workingPath, 'workbench', 'database', 'seeders', 'DatabaseSeeder.php')); } /** * Assert `workbench:install` command executed with `--no-devtool`. */ - protected function assertCommandExecutedWithoutDevTool(): void + protected function assertCommandExecutedWithoutDevTool(bool $prefix = true): void { $workingPath = static::stubWorkingPath(); @@ -73,7 +99,7 @@ protected function assertCommandExecutedWithoutDevTool(): void /** * Assert command executed with `workbench:install` or `workbench:devtool --install`. */ - protected function assertCommandExecutedWithInstall(): void + protected function assertCommandExecutedWithInstall(bool $prefix = true): void { $workingPath = static::stubWorkingPath(); @@ -82,14 +108,14 @@ protected function assertCommandExecutedWithInstall(): void $config = Config::loadFromYaml($workingPath); $this->assertSame(default_skeleton_path(), $config['laravel']); - $this->assertSame([DatabaseSeeder::class], $config->seeders); + $this->assertSame([ + $prefix ? WorkbenchDatabaseSeeder::class : DatabaseSeeder::class, + ], $config->seeders); $this->assertSame([ 'asset-publish', 'create-sqlite-db', 'db-wipe', - ['migrate-fresh' => [ - '--seed' => true, - ]], + 'migrate-fresh', ], $config->getWorkbenchAttributes()['build']); $this->assertSame([ 'laravel-assets', @@ -99,7 +125,7 @@ protected function assertCommandExecutedWithInstall(): void /** * Assert `workbench:install --basic` or `workbench:devtool --basic --install` command executed. */ - protected function assertCommandExecutedWithBasicInstall(): void + protected function assertCommandExecutedWithBasicInstall(bool $prefix = true): void { $workingPath = static::stubWorkingPath(); @@ -109,7 +135,7 @@ protected function assertCommandExecutedWithBasicInstall(): void $this->assertSame(default_skeleton_path(), $config['laravel']); $this->assertSame([ - DatabaseSeeder::class, + $prefix ? WorkbenchDatabaseSeeder::class : DatabaseSeeder::class, ], $config->seeders); $this->assertSame([], $config->getWorkbenchAttributes()['build']); $this->assertSame([], $config->getWorkbenchAttributes()['assets']); @@ -118,7 +144,7 @@ protected function assertCommandExecutedWithBasicInstall(): void /** * Assert `workbench:devtool` command executed with `--no-install` */ - protected function assertCommandExecutedWithoutInstall(): void + protected function assertCommandExecutedWithoutInstall(bool $prefix = true): void { $workingPath = static::stubWorkingPath(); $environmentFiles = collect(['.env', '.env.example', '.env.dist']); @@ -147,6 +173,22 @@ protected function assertFromEnvironmentFileDataProviders(?string $answer, bool } } + /** + * Assert file does contains data. + * + * @param array $contains + */ + protected function assertFileContains(array $contains, string $file, string $message = ''): void + { + $this->assertFileExists($file); + + $haystack = file_get_contents($file); + + foreach ($contains as $needle) { + $this->assertStringContainsString($needle, $haystack, $message); + } + } + /** * `environmentFileDataProviders` data provider. */ diff --git a/tests/Console/DevToolCommandTest.php b/tests/Console/DevToolCommandTest.php index 167a02f..17ff2e7 100644 --- a/tests/Console/DevToolCommandTest.php +++ b/tests/Console/DevToolCommandTest.php @@ -76,7 +76,7 @@ public function it_can_be_installed_with_prompt_for_missing_arguments() { $this->artisan('workbench:devtool') ->expectsConfirmation('Run Workbench installation?', false) - ->expectsConfirmation('Prefix with `Workbench` namespace?', true) + ->expectsConfirmation('Prefix with `Workbench` namespace?', 'yes') ->assertSuccessful(); $this->assertCommandExecutedWithDevTool(); diff --git a/tests/Console/InstallCommandTest.php b/tests/Console/InstallCommandTest.php index 6519cff..3c09115 100644 --- a/tests/Console/InstallCommandTest.php +++ b/tests/Console/InstallCommandTest.php @@ -32,6 +32,24 @@ public function it_can_run_installation_command_with_devtool(?string $answer, bo $this->assertFromEnvironmentFileDataProviders($answer, $createEnvironmentFile); } + #[Test] + #[DataProvider('environmentFileDataProviders')] + public function it_can_run_installation_command_with_devtool_without_workbench_prefix(?string $answer, bool $createEnvironmentFile) + { + $this->artisan('workbench:install', ['--devtool' => true]) + ->expectsConfirmation('Prefix with `Workbench` namespace?', 'no') + ->expectsChoice("Export '.env' file as?", $answer, [ + 'Skip exporting .env', + '.env', + '.env.example', + '.env.dist', + ])->assertSuccessful(); + + $this->assertCommandExecutedWithInstall(prefix: false); + $this->assertCommandExecutedWithDevTool(prefix: false); + $this->assertFromEnvironmentFileDataProviders($answer, $createEnvironmentFile); + } + #[Test] #[DataProvider('environmentFileDataProviders')] public function it_can_run_installation_command_without_devtool(?string $answer, bool $createEnvironmentFile) diff --git a/tests/Console/stubs/phpunit.xml.dist b/tests/Console/stubs/phpunit.xml.dist new file mode 100644 index 0000000..b29e977 --- /dev/null +++ b/tests/Console/stubs/phpunit.xml.dist @@ -0,0 +1,2 @@ + +