Skip to content

Commit

Permalink
[7.x] Set default laravel configuration to either @testbench or `…
Browse files Browse the repository at this point in the history
…@testbench-dusk` (#56)

* [7.x] Set default `laravel` configuration to either `@testbench` or `@testbench-dusk`

fixes #55

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Dec 4, 2024
1 parent 66c377f commit 4e5d672
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/Console/Concerns/InteractsWithFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Orchestra\Workbench\Console\Concerns;

use Illuminate\Filesystem\Filesystem;

trait InteractsWithFiles
{
/**
* Replace a given string within a given file.
*/
protected function replaceInFile(Filesystem $filesystem, array|string $search, array|string $replace, string $path): void
{
/** @phpstan-ignore argument.type */
$filesystem->put($path, str_replace($search, $replace, $filesystem->get($path)));
}
}
11 changes: 2 additions & 9 deletions src/Console/DevToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#[AsCommand(name: 'workbench:devtool', description: 'Configure Workbench for package development')]
class DevToolCommand extends Command
{
use Concerns\InteractsWithFiles;

/**
* Execute the console command.
*
Expand Down Expand Up @@ -264,15 +266,6 @@ protected function appendAutoloadDevToComposer(array $content, Filesystem $files
return $content;
}

/**
* Replace a given string within a given file.
*/
protected function replaceInFile(Filesystem $filesystem, array|string $search, array|string $replace, string $path): void
{
/** @phpstan-ignore argument.type */
file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
}

/**
* Get the console command options.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orchestra\Workbench\Console;

use Composer\InstalledVersions;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
Expand All @@ -19,6 +20,8 @@
#[AsCommand(name: 'workbench:install', description: 'Setup Workbench for package development')]
class InstallCommand extends Command
{
use Concerns\InteractsWithFiles;

/**
* The `testbench.yaml` default configuration file.
*/
Expand Down Expand Up @@ -51,6 +54,12 @@ public function handle(Filesystem $filesystem)
$this->copyTestbenchConfigurationFile($filesystem, $workingPath);
$this->copyTestbenchDotEnvFile($filesystem, $workingPath);

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

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

$this->call('workbench:create-sqlite-db', ['--force' => true]);

return Command::SUCCESS;
Expand Down
2 changes: 2 additions & 0 deletions src/Console/stubs/testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
laravel: '@testbench'

providers:
# - Workbench\App\Providers\WorkbenchServiceProvider

Expand Down
46 changes: 46 additions & 0 deletions tests/Console/Concerns/InteractsWithFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Orchestra\Workbench\Tests\Console\Concerns;

use Illuminate\Filesystem\Filesystem;
use Mockery as m;
use Orchestra\Testbench\Concerns\InteractsWithMockery;
use Orchestra\Workbench\Console\Concerns\InteractsWithFiles;
use PHPUnit\Framework\TestCase;

class InteractsWithFilesTest extends TestCase
{
use InteractsWithMockery;

/** {@inheritDoc} */
#[\Override]
protected function tearDown(): void
{
$this->tearDownTheTestEnvironmentUsingMockery();

parent::tearDown();
}

/** @test */
public function it_can_replace_contents()
{
$filesystem = m::mock(Filesystem::class);

$fixture = new class($filesystem)
{
use InteractsWithFiles;

public function __construct(public Filesystem $filesystem) {}

public function replace(array|string $search, array|string $replace, string $path)
{
$this->replaceInFile($this->filesystem, $search, $replace, $path);
}
};

$filesystem->shouldReceive('get')->with('testbench.yaml')->once()->andReturn("laravel: '@testbench'")
->shouldReceive('put')->with('testbench.yaml', "laravel: '@testbench-dusk'");

$fixture->replace("laravel: '@testbench'", "laravel: '@testbench-dusk'", 'testbench.yaml');
}
}

0 comments on commit 4e5d672

Please sign in to comment.