Skip to content

Commit

Permalink
Merge branch '7.x' into 8.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 8, 2024
2 parents 3fdb44b + 187f3bf commit b37dece
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/analyse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: analyse

on:
push:
branches:
- '*.x'
pull_request:
workflow_dispatch:

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/audits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: audits

on:
push:
branches:
- '*.x'
pull_request:
workflow_dispatch:

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/coveralls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: coveralls

on:
push:
branches:
- '*.x'
pull_request:

jobs:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: tests

on:
push:
branches:
- '*.x'
pull_request:
workflow_dispatch:

Expand Down
23 changes: 23 additions & 0 deletions src/Actions/DumpComposerAutoloads.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Orchestra\Workbench\Actions;

class DumpComposerAutoloads
{
/**
* Construct a new action.
*/
public function __construct(
protected string $workingPath
) {}

/**
* Handle the action.
*/
public function handle(): void
{
app('workbench.composer')
->setWorkingPath($this->workingPath)
->dumpAutoloads();
}
}
25 changes: 25 additions & 0 deletions src/Actions/ModifyComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Orchestra\Workbench\Actions;

class ModifyComposer
{
/**
* Construct a new action.
*/
public function __construct(
protected string $workingPath
) {}

/**
* Handle the action.
*
* @param callable(array):array $callback
*/
public function handle(callable $callback): void
{
app('workbench.composer')
->setWorkingPath($this->workingPath)
->modify($callback);
}
}
11 changes: 0 additions & 11 deletions src/Composer.php

This file was deleted.

13 changes: 6 additions & 7 deletions src/Console/DevToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Composer;
use Orchestra\Testbench\Foundation\Console\Actions\EnsureDirectoryExists;
use Orchestra\Testbench\Foundation\Console\Actions\GeneratesFile;
use Orchestra\Workbench\Actions\DumpComposerAutoloads;
use Orchestra\Workbench\Actions\ModifyComposer;
use Orchestra\Workbench\Events\InstallEnded;
use Orchestra\Workbench\Events\InstallStarted;
use Orchestra\Workbench\Workbench;
Expand Down Expand Up @@ -53,12 +54,10 @@ public function handle(Filesystem $filesystem)
]);
}

return tap(Command::SUCCESS, function ($exitCode) use ($filesystem, $workingPath) {
return tap(Command::SUCCESS, function ($exitCode) use ($workingPath) {
event(new InstallEnded($this->input, $this->output, $this->components, $exitCode));

(new Composer($filesystem))
->setWorkingPath($workingPath)
->dumpAutoloads();
(new DumpComposerAutoloads($workingPath))->handle();
});
}

Expand Down Expand Up @@ -111,9 +110,9 @@ protected function prepareWorkbenchDirectories(Filesystem $filesystem, string $w
*/
protected function prepareWorkbenchNamespaces(Filesystem $filesystem, string $workingPath): void
{
$composer = (new Composer($filesystem))->setWorkingPath($workingPath);
$action = new ModifyComposer($workingPath);

$composer->modify(function (array $content) use ($filesystem) {
$action->handle(function (array $content) use ($filesystem) {
return $this->appendScriptsToComposer(
$this->appendAutoloadDevToComposer($content, $filesystem), $filesystem
);
Expand Down
46 changes: 34 additions & 12 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class InstallCommand extends Command implements PromptsForMissingInput
*/
public static ?string $configurationBaseFile = null;

/**
* Determine if Package also uses Testbench Dusk.
*/
protected ?bool $hasTestbenchDusk = null;

/** {@inheritDoc} */
#[\Override]
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk');

parent::initialize($input, $output);
}

/**
* Execute the console command.
*
Expand Down Expand Up @@ -59,9 +73,7 @@ public function handle(Filesystem $filesystem)
$this->copyTestbenchConfigurationFile($filesystem, $workingPath);
$this->copyTestbenchDotEnvFile($filesystem, $workingPath);

$hasTestbenchDusk = InstalledVersions::isInstalled('orchestra/testbench-dusk');

if ($hasTestbenchDusk) {
if ($this->hasTestbenchDusk) {
$this->replaceInFile($filesystem, ["laravel: '@testbench'"], ["laravel: '@testbench-dusk'"], join_paths($workingPath, 'testbench.yaml'));
}

Expand Down Expand Up @@ -123,13 +135,27 @@ protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $worki
return;
}

$to = join_paths($workbenchWorkingPath, $choice);
if ($this->hasTestbenchDusk === true) {
if ($this->components->confirm('Create separate environment file for Testbench Dusk?', false)) {
(new GeneratesFile(
filesystem: $filesystem,
components: $this->components,
force: (bool) $this->option('force'),
))->handle(
$from,
join_paths($workbenchWorkingPath, str_replace('.env', '.env.dusk', $choice))
);
}
}

(new GeneratesFile(
filesystem: $filesystem,
components: $this->components,
force: (bool) $this->option('force'),
))->handle($from, $to);
))->handle(
$from,
join_paths($workbenchWorkingPath, $choice)
);

(new GeneratesFile(
filesystem: $filesystem,
Expand All @@ -147,14 +173,10 @@ protected function copyTestbenchDotEnvFile(Filesystem $filesystem, string $worki
*/
protected function environmentFiles(): array
{
$environmentFile = \defined('TESTBENCH_DUSK') && TESTBENCH_DUSK === true
? '.env.dusk'
: '.env';

return [
$environmentFile,
"{$environmentFile}.example",
"{$environmentFile}.dist",
'.env',
'.env.example',
'.env.dist',
];
}

Expand Down
3 changes: 3 additions & 0 deletions src/WorkbenchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\Composer;
use Illuminate\Support\ServiceProvider;
use Orchestra\Canvas\Core\PresetManager;
use Orchestra\Testbench\Foundation\Events\ServeCommandEnded;
Expand All @@ -21,6 +23,7 @@ class WorkbenchServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->app->bind('workbench.composer', static fn () => new Composer(new Filesystem));
$this->app->singleton(Contracts\RecipeManager::class, static fn (Application $app) => new RecipeManager($app));

$this->callAfterResolving(PresetManager::class, static function ($manager) {
Expand Down
32 changes: 32 additions & 0 deletions tests/Actions/DumpComposerAutoloadsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Orchestra\Workbench\Tests\Actions;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Mockery as m;
use Orchestra\Workbench\Actions\DumpComposerAutoloads;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

use function Orchestra\Testbench\join_paths;

#[Group('composer')]
class DumpComposerAutoloadsTest extends TestCase
{
#[Test]
public function it_can_run_dump_autoloads()
{
$filesystem = new Filesystem;
$workingPath = join_paths(__DIR__, 'stubs');

$this->instance('workbench.composer', $composer = m::mock(Composer::class, ['files' => $filesystem]));

$composer->shouldReceive('setWorkingPath')->once()->with($workingPath)->andReturnSelf();
$composer->shouldReceive('dumpAutoloads')->once()->andReturnNull();

$action = new DumpComposerAutoloads($workingPath);

$action->handle();
}
}
65 changes: 65 additions & 0 deletions tests/Actions/ModifyComposerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Orchestra\Workbench\Tests\Actions;

use Orchestra\Workbench\Actions\ModifyComposer;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
use PHPUnit\Framework\Attributes\Test;
use RuntimeException;

use function Orchestra\Testbench\join_paths;

#[RequiresOperatingSystem('Linux|DAR')]
#[Group('composer')]
class ModifyComposerTest extends TestCase
{
/** {@inheritDoc} */
#[\Override]
protected function setUp(): void
{
$this->afterApplicationCreated(function () {
copy(join_paths(__DIR__, 'stubs', 'composer.json'), join_paths(__DIR__, 'tmp', 'composer.json'));
});

$this->beforeApplicationDestroyed(function () {
@unlink(join_paths(__DIR__, 'tmp', 'composer.json'));
});

parent::setUp();
}

#[Test]
public function it_can_modify_composer_file()
{
$workingPath = join_paths(__DIR__, 'tmp');

$action = new ModifyComposer($workingPath);

$this->assertTrue(is_file(join_paths($workingPath, 'composer.json')));
$this->assertSame('{}'.PHP_EOL, file_get_contents(join_paths($workingPath, 'composer.json')));

$action->handle(function (array $content) {
$content['$schema'] = 'https://getcomposer.org/schema.json';

return $content;
});

$this->assertTrue(is_file(join_paths($workingPath, 'composer.json')));
$this->assertSame('{
"$schema": "https://getcomposer.org/schema.json"
}', file_get_contents(join_paths($workingPath, 'composer.json')));
}

#[Test]
public function it_throws_exception_when_composer_file_does_not_exists()
{
$workingPath = __DIR__;

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(\sprintf('Unable to locate `composer.json` file at [%s]', $workingPath));

$action = new ModifyComposer($workingPath);
$action->handle(static fn (array $content) => $content);
}
}
17 changes: 17 additions & 0 deletions tests/Actions/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Orchestra\Workbench\Tests\Actions;

use Orchestra\Workbench\WorkbenchServiceProvider;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{
/** {@inheritDoc} */
#[\Override]
protected function getPackageProviders($app)
{
return [
WorkbenchServiceProvider::class,
];
}
}
1 change: 1 addition & 0 deletions tests/Actions/stubs/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions tests/Actions/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit b37dece

Please sign in to comment.