Skip to content

Commit

Permalink
Merge pull request #201 from paupenin/extendable-laravel-test-command
Browse files Browse the repository at this point in the history
Extendable Environment Variables in Laravel Test Command with Tests
  • Loading branch information
nunomaduro committed Nov 28, 2021
2 parents 3776524 + 615f9c0 commit d21091b
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/Adapters/Laravel/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ public function handle()
),
null,
// Envs ...
$parallel ? [
'LARAVEL_PARALLEL_TESTING' => 1,
'LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES' => $this->option('recreate-databases'),
] : [],
$parallel ? $this->paratestEnvironmentVariables() : $this->phpunitEnvironmentVariables(),
))->setTimeout(null);

try {
Expand Down Expand Up @@ -181,6 +178,29 @@ protected function paratestArguments($options)
], $options);
}

/**
* Get the array of environment variables for running PHPUnit.
*
* @return array
*/
protected function phpunitEnvironmentVariables()
{
return [];
}

/**
* Get the array of environment variables for running Paratest.
*
* @return array
*/
protected function paratestEnvironmentVariables()
{
return [
'LARAVEL_PARALLEL_TESTING' => 1,
'LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES' => $this->option('recreate-databases'),
];
}

/**
* Clears any set Environment variables set by Laravel if the --env option is empty.
*
Expand Down
92 changes: 92 additions & 0 deletions tests/LaravelApp/app/Console/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@

namespace App\Console\Commands;

use Illuminate\Support\Str;
use NunoMaduro\Collision\Adapters\Laravel\Commands\TestCommand as BaseTestCommand;

class TestCommand extends BaseTestCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test
{--without-tty : Disable output to TTY}
{--p|parallel : Indicates if the tests should run in parallel}
{--recreate-databases : Indicates if the test databases should be re-created}
{--c|custom-argument : Add custom env variables}
';

/**
* Get the PHP binary to execute.
*
Expand All @@ -33,4 +46,83 @@ protected function binary()

return [PHP_BINARY, __DIR__ . '/../../../../../' . $command];
}

/**
* Get the array of environment variables for running PHPUnit.
*
* @return array
*/
protected function phpunitEnvironmentVariables()
{
if ($this->option("custom-argument")) {
return array_merge(
parent::phpunitEnvironmentVariables(),
[
'CUSTOM_ENV_VARIABLE' => 1,
'CUSTOM_ENV_VARIABLE_FOR_PHPUNIT' => 1,
],
);
}

return parent::phpunitEnvironmentVariables();
}

/**
* Get the array of environment variables for running Paratest.
*
* @return array
*/
protected function paratestEnvironmentVariables()
{
if ($this->option("custom-argument")) {
return array_merge(
parent::paratestEnvironmentVariables(),
[
'CUSTOM_ENV_VARIABLE' => 1,
'CUSTOM_ENV_VARIABLE_FOR_PARALLEL' => 1,
],
);
}

return parent::paratestEnvironmentVariables();
}

/**
* Get the array of arguments for running PHPUnit.
*
* @param array $options
*
* @return array
*/
protected function phpunitArguments($options)
{
return parent::phpunitArguments($this->filterCustomOption($options));
}

/**
* Get the array of arguments for running Paratest.
*
* @param array $options
*
* @return array
*/
protected function paratestArguments($options)
{
return parent::paratestArguments($this->filterCustomOption($options));
}

/**
* Filters my custom argument from options list.
*
* @param array $options
*
* @return array
*/
protected function filterCustomOption($options)
{
return array_values(array_filter($options, function ($option) {
return !Str::startsWith($option, '-c')
&& !Str::startsWith($option, '--custom-argument');
}));
}
}
85 changes: 85 additions & 0 deletions tests/LaravelApp/tests/Feature/EnvironmentCustomVariablesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use Tests\TestCase;

/**
* @group environmentCustomVariables
*/
class EnvironmentCustomVariablesTest extends TestCase
{
/**
* @group environmentNoCVPhpunit
*/
public function testEnvironmentNoCustomVariablesPhpunit()
{
$this->assertEquals(null, env('LARAVEL_PARALLEL_TESTING'));
$this->assertEquals(null, env('LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PHPUNIT'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PARALLEL'));
}

/**
* @group environmentNoCVParallel
*/
public function testEnvironmentNoCustomVariablesParallel()
{
$this->assertEquals(1, env('LARAVEL_PARALLEL_TESTING'));
$this->assertEquals(null, env('LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PHPUNIT'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PARALLEL'));
}

/**
* @group environmentNoCVParallelRecreate
*/
public function testEnvironmentNoCustomVariablesParallelWithRecreate()
{
$this->assertEquals(1, env('LARAVEL_PARALLEL_TESTING'));
$this->assertEquals(1, env('LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PHPUNIT'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PARALLEL'));
}

/**
* @group environmentCVPhpunit
*/
public function testEnvironmentCustomVariablesPhpunit()
{
$this->assertEquals(null, env('LARAVEL_PARALLEL_TESTING'));
$this->assertEquals(null, env('LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES'));
$this->assertEquals(1, env('CUSTOM_ENV_VARIABLE'));
$this->assertEquals(1, env('CUSTOM_ENV_VARIABLE_FOR_PHPUNIT'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PARALLEL'));
}

/**
* @group environmentCVParallel
*/
public function testEnvironmentCustomVariablesParallel()
{
$this->assertEquals(1, env('LARAVEL_PARALLEL_TESTING'));
$this->assertEquals(null, env('LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES'));
$this->assertEquals(1, env('CUSTOM_ENV_VARIABLE'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PHPUNIT'));
$this->assertEquals(1, env('CUSTOM_ENV_VARIABLE_FOR_PARALLEL'));
}

/**
* @group environmentCVParallelRecreate
*/
public function testEnvironmentCustomVariablesParallelWithRecreate()
{
$this->assertEquals(1, env('LARAVEL_PARALLEL_TESTING'));
$this->assertEquals(1, env('LARAVEL_PARALLEL_TESTING_RECREATE_DATABASES'));
$this->assertEquals(1, env('CUSTOM_ENV_VARIABLE'));
$this->assertEquals(null, env('CUSTOM_ENV_VARIABLE_FOR_PHPUNIT'));
$this->assertEquals(1, env('CUSTOM_ENV_VARIABLE_FOR_PARALLEL'));
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Adapters/ArtisanTestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ public static function cleanUp()
@unlink(__DIR__ . '/../../../tests/LaravelApp/.env.testing');
}

/** @test */
public function testExtendableCustomVariables(): void
{
$this->runTests([
'./vendor/bin/phpunit',
'-c',
'tests/LaravelApp/phpunit.xml',
'--group',
'environmentNoCVPhpunit',
]);

// Without Custom Variables (-c|--custom-argument)
$this->runTests(['./tests/LaravelApp/artisan', 'test', '--group', 'environmentNoCVPhpunit']);
$this->runTests(['./tests/LaravelApp/artisan', 'test', '--parallel', '--group', 'environmentNoCVParallel']);
$this->runTests(['./tests/LaravelApp/artisan', 'test', '--parallel', '--recreate-databases', '--group', 'environmentNoCVParallelRecreate']);

// With Custom Variables (-c)
$this->runTests(['./tests/LaravelApp/artisan', 'test', '-c', '--group', 'environmentCVPhpunit']);
$this->runTests(['./tests/LaravelApp/artisan', 'test', '-c', '--parallel', '--group', 'environmentCVParallel']);
$this->runTests(['./tests/LaravelApp/artisan', 'test', '-c', '--parallel', '--recreate-databases', '--group', 'environmentCVParallelRecreate']);

// With Custom Variables (--custom-argument)
$this->runTests(['./tests/LaravelApp/artisan', 'test', '--custom-argument', '--group', 'environmentCVPhpunit']);
$this->runTests(['./tests/LaravelApp/artisan', 'test', '--custom-argument', '--parallel', '--group', 'environmentCVParallel']);
$this->runTests(['./tests/LaravelApp/artisan', 'test', '--custom-argument', '--parallel', '--recreate-databases', '--group', 'environmentCVParallelRecreate']);
}

private function runTests(array $arguments): void
{
$process = new Process($arguments, __DIR__ . '/../../..');
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Adapters/PhpunitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function itHasTests(): void
{
$output = $this->runCollisionTests([
'--exclude-group',
'fail,environmentTesting,custom-name',
'fail,environmentTesting,environmentCustomVariables,custom-name',
]);

$testsDir = dirname(__DIR__, 2);
Expand Down Expand Up @@ -111,7 +111,7 @@ public function itHasRecap(): void
{
$output = $this->runCollisionTests([
'--exclude-group',
'fail,environmentTesting',
'fail,environmentTesting,environmentCustomVariables',
]);

$this->assertConsoleOutputContainsString(
Expand Down

0 comments on commit d21091b

Please sign in to comment.