Skip to content

Commit

Permalink
wip
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 9, 2024
1 parent 4a7296b commit 6cf2e72
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 73 deletions.
76 changes: 76 additions & 0 deletions tests/Console/CommandTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Orchestra\Workbench\Tests\Console;

use Illuminate\Filesystem\Filesystem;
use Orchestra\Testbench\Foundation\TestbenchServiceProvider;
use Orchestra\Workbench\WorkbenchServiceProvider;

use function Orchestra\Testbench\join_paths;

abstract class CommandTestCase extends \Orchestra\Testbench\TestCase
{
/** {@inheritDoc} */
#[\Override]
protected function setUp(): void
{
$filesystem = new Filesystem;
$workingPath = static::stubWorkingPath();

$this->beforeApplicationDestroyed(function () use ($filesystem, $workingPath) {
$filesystem->deleteDirectory($workingPath);
unset($_ENV['TESTBENCH_WORKING_PATH']);
});

$_ENV['TESTBENCH_WORKING_PATH'] = $workingPath;
$filesystem->ensureDirectoryExists($workingPath);

parent::setUp();
}

/** {@inheritDoc} */
#[\Override]
protected function getPackageProviders($app)
{
return [
TestbenchServiceProvider::class,
WorkbenchServiceProvider::class,
];
}

/**
* Assert from `environmentFileDataProviders`
*/
protected function assertFromEnvironmentFileDataProviders(?string $answer, bool $createEnvironmentFile): void
{
$workingPath = static::stubWorkingPath();

if (is_null($answer) || $createEnvironmentFile === false) {
collect(['.env', '.env.example', '.env.dist'])
->each(function ($file) use ($workingPath) {
$this->assertFalse(is_file(join_paths($workingPath, 'workbench', $file)));
});
} else {
$this->assertTrue(is_file(join_paths($workingPath, 'workbench', $answer)));
}
}

/**
* `environmentFileDataProviders` data provider.
*/
public static function environmentFileDataProviders()
{
yield ['Skip exporting .env', false];
yield ['.env', true];
yield ['.env.example', true];
yield ['.env.dist', true];
}

/**
* Get the stub working path.
*/
protected static function stubWorkingPath(): string
{
return join_paths(__DIR__, \sprintf('%s_stubs', class_basename(static::class)));
}
}
80 changes: 7 additions & 73 deletions tests/Console/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,24 @@

namespace Orchestra\Workbench\Tests\Console;

use Illuminate\Filesystem\Filesystem;
use Orchestra\Testbench\Foundation\Config;
use Orchestra\Testbench\Foundation\TestbenchServiceProvider;
use Orchestra\Testbench\TestCase;
use Orchestra\Workbench\WorkbenchServiceProvider;

use function Orchestra\Testbench\default_skeleton_path;
use function Orchestra\Testbench\join_paths;

class InstallCommandTest extends TestCase
class InstallCommandTest extends CommandTestCase
{
/**
* Stub
*/
protected ?string $directory = null;

/** {@inheritDoc} */
#[\Override]
protected function setUp(): void
{
$filesystem = new Filesystem;
$workingPath = static::stubWorkingPath();

$this->beforeApplicationDestroyed(function () use ($filesystem, $workingPath) {
$filesystem->deleteDirectory($workingPath);
unset($_ENV['TESTBENCH_WORKING_PATH']);
});

$_ENV['TESTBENCH_WORKING_PATH'] = $workingPath;
$filesystem->ensureDirectoryExists($workingPath);

parent::setUp();
}

/** {@inheritDoc} */
#[\Override]
protected function getPackageProviders($app)
{
return [
TestbenchServiceProvider::class,
WorkbenchServiceProvider::class,
];
}

/**
* @test
*
* @dataProvider environmentFileDataProviders
*/
public function it_can_run_installation_command_without_devtool(?string $env, bool $createEnvironmentFile)
public function it_can_run_installation_command_without_devtool(?string $answer, bool $createEnvironmentFile)
{
$workingPath = static::stubWorkingPath();

$this->artisan('workbench:install', ['--no-devtool' => true, '--no-interaction' => true])
->expectsChoice("Export '.env' file as?", $env, [
->expectsChoice("Export '.env' file as?", $answer, [
'Skip exporting .env',
'.env',
'.env.example',
Expand All @@ -82,27 +45,20 @@ public function it_can_run_installation_command_without_devtool(?string $env, bo
'laravel-assets',
], $config->getWorkbenchAttributes()['assets']);

if ($createEnvironmentFile === false) {
collect(['.env', '.env.example', '.env.dist'])
->each(function ($file) use ($workingPath) {
$this->assertFalse(is_file(join_paths($workingPath, 'workbench', $file)));
});
} else {
$this->assertTrue(is_file(join_paths($workingPath, 'workbench', $env)));
}
$this->assertFromEnvironmentFileDataProviders($answer, $createEnvironmentFile);
}

/**
* @test
*
* @dataProvider environmentFileDataProviders
*/
public function it_can_run_basic_installation_command_without_devtool(?string $env, bool $createEnvironmentFile)
public function it_can_run_basic_installation_command_without_devtool(?string $answer, bool $createEnvironmentFile)
{
$workingPath = static::stubWorkingPath();

$this->artisan('workbench:install', ['--basic' => true, '--no-devtool' => true, '--no-interaction' => true])
->expectsChoice("Export '.env' file as?", $env, [
->expectsChoice("Export '.env' file as?", $answer, [
'Skip exporting .env',
'.env',
'.env.example',
Expand All @@ -120,28 +76,6 @@ public function it_can_run_basic_installation_command_without_devtool(?string $e
$this->assertSame([], $config->getWorkbenchAttributes()['build']);
$this->assertSame([], $config->getWorkbenchAttributes()['assets']);

if ($createEnvironmentFile === false) {
collect(['.env', '.env.example', '.env.dist'])
->each(function ($file) use ($workingPath) {
$this->assertFalse(is_file(join_paths($workingPath, 'workbench', $file)));
});
} else {
$this->assertTrue(is_file(join_paths($workingPath, 'workbench', $env)));
}
}

public static function environmentFileDataProviders()
{
$workingPath = static::stubWorkingPath();

yield ['Skip exporting .env', false];
yield ['.env', true];
yield ['.env.example', true];
yield ['.env.dist', true];
}

protected static function stubWorkingPath(): string
{
return join_paths(__DIR__, \sprintf('%s_stubs', class_basename(static::class)));
$this->assertFromEnvironmentFileDataProviders($answer, $createEnvironmentFile);
}
}

0 comments on commit 6cf2e72

Please sign in to comment.