Skip to content

Commit

Permalink
Merge branch '8.15.x' into 9.11.x
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Dec 19, 2024
2 parents c490ec9 + 0490263 commit 4481795
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG-8.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 16 additions & 2 deletions src/Console/DevToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -115,6 +115,8 @@ protected function prepareWorkbenchNamespaces(Filesystem $filesystem, string $wo
->handle(fn (array $content) => $this->appendScriptsToComposer(
$this->appendAutoloadDevToComposer($content, $filesystem), $filesystem
));

Workbench::flushCachedClassAndNamespaces();
}

/**
Expand All @@ -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'));
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -140,8 +140,6 @@ protected function copyTestbenchConfigurationFile(Filesystem $filesystem, string
'{{WorkbenchDatabaseSeeder}}',
'{{ WorkbenchDatabaseSeeder }}',
'Workbench\Database\Seeders\DatabaseSeeder',

' - migrate-fresh',
],
[
$workbenchAppNamespacePrefix,
Expand All @@ -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
);
Expand Down
64 changes: 53 additions & 11 deletions tests/Console/CommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -49,20 +51,44 @@ 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();

$this->assertDirectoryExists(join_paths($workingPath, 'workbench', 'app', 'Models'));
$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();

Expand All @@ -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();

Expand All @@ -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',
Expand All @@ -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();

Expand All @@ -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']);
Expand All @@ -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']);
Expand Down Expand Up @@ -147,6 +173,22 @@ protected function assertFromEnvironmentFileDataProviders(?string $answer, bool
}
}

/**
* Assert file does contains data.
*
* @param array<int, string> $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.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/DevToolCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions tests/Console/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/Console/stubs/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></phpunit>

0 comments on commit 4481795

Please sign in to comment.