Skip to content

Commit

Permalink
Merge pull request #1170: add production and test envs aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Nov 29, 2024
2 parents 8e89922 + b379e9c commit c32dfed
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
7 changes: 7 additions & 0 deletions src/Boot/src/Environment/AppEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public static function detect(EnvironmentInterface $environment): self
{
$value = $environment->get('APP_ENV');

// Aliases
$value = match ($value) {
'production' => self::Production->value,
'test' => self::Testing->value,
default => $value,
};

return \is_string($value)
? (self::tryFrom($value) ?? self::Local)
: self::Local;
Expand Down
4 changes: 4 additions & 0 deletions src/Boot/tests/Environment/AppEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public static function envVariablesDataProvider(): \Traversable
{
yield ['wrong', AppEnvironment::Local];
yield ['prod', AppEnvironment::Production];
yield ['production', AppEnvironment::Production];
yield ['stage', AppEnvironment::Stage];
yield ['local', AppEnvironment::Local];
yield ['dev', AppEnvironment::Local];
yield ['development', AppEnvironment::Local];
yield ['testing', AppEnvironment::Testing];
yield ['test', AppEnvironment::Testing];
}

public function testClassMethods(): void
Expand Down
9 changes: 1 addition & 8 deletions src/Console/tests/SignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function perform(): int
])
);

$this->assertSame(
$this->assertStringStartsWith(
<<<'HELP'
Usage:
foo:bar [options] [--] <foo> [<bar> [<baz>...]]
Expand All @@ -150,13 +150,6 @@ public function perform(): int
-o, --id[=ID] Id option description. (multiple values allowed)
-Q, --quit Quit option description.
--naf[=NAF] Naf option description. [default: "default"]
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

HELP
,
$core->run(command: 'help', input: ['command_name' => 'foo:bar'])->getOutput()->fetch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
use Spiral\Framework\Spiral;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* The component makes it easy to ask the user for confirmation before running
* a command if the application is running in production mode.
* This can help prevent accidental or unintended changes to the production environment.
*/
#[Scope(Spiral::ConsoleCommand)]
final class ApplicationInProduction
{
Expand All @@ -22,7 +28,7 @@ public function __construct(
OutputInterface $output,
) {
$this->input = $input;
$this->output = $output;
$this->output = $output instanceof SymfonyStyle ? $output : new SymfonyStyle($input, $output);
}

public function confirmToProceed(string $message = 'Application in production.'): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ final class ApplicationInProductionCommandTest extends BaseTestCase
public function testRunCommandInProductionEnv(): void
{
$this->assertConsoleCommandOutputContainsStrings('app-in-production', [], [
'Application is in production.',
'Denied.',
]);
}

#[Env('APP_ENV', 'testing')]
public function testRunCommandInTestingEnv(): void
{
$this->assertConsoleCommandOutputContainsStrings('app-in-production', [], [
'Application is in testing.',
'Allowed.',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Spiral\Console\Confirmation\ApplicationInProduction;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class ApplicationInProductionTest extends TestCase
{
Expand All @@ -19,8 +20,8 @@ public function testNotProductionEnvironmentShouldBeConfirmed(AppEnvironment $en
{
$confirmation = new ApplicationInProduction(
$env,
m::mock(InputInterface::class),
m::mock(OutputInterface::class)
$this->createMock(InputInterface::class),
$this->createMock(OutputInterface::class)
);

$this->assertTrue($confirmation->confirmToProceed());
Expand All @@ -30,31 +31,31 @@ public function testProductionEnvWithForceOptionShouldBeConfirmed(): void
{
$confirmation = new ApplicationInProduction(
AppEnvironment::Production,
$input = m::mock(InputInterface::class),
m::mock(OutputInterface::class)
$input = $this->createMock(InputInterface::class),
$this->createMock(OutputInterface::class)
);

$input->shouldReceive('hasOption')->once()->andReturnTrue();
$input->shouldReceive('getOption')->once()->andReturnTrue();
$input->expects($this->once())->method('hasOption')->willReturn(true);
$input->expects($this->once())->method('getOption')->willReturn(true);

$this->assertTrue($confirmation->confirmToProceed());
}


public function testProductionEnvShouldBeAskAboutConfirmationAndConfirmed(): void
{
$confirmation = new ApplicationInProduction(
AppEnvironment::Production,
$input = m::mock(InputInterface::class),
$output = m::mock(OutputInterface::class)
$input = $this->createMock(InputInterface::class),
$output = $this->createMock(SymfonyStyle::class)
);

$input->shouldReceive('hasOption')->once()->andReturnFalse();

$output->shouldReceive('writeln');
$output->shouldReceive('write');
$output->shouldReceive('newLine');

$output->shouldReceive('confirm')->once()->with('Do you really wish to run command?', false)->andReturnTrue();
$input->expects($this->once())->method('hasOption')->willReturn(false);
$output
->expects($this->once())
->method('confirm')
->with('Do you really wish to run command?', false)
->willReturn(true);

$this->assertTrue($confirmation->confirmToProceed());
}
Expand All @@ -63,17 +64,16 @@ public function testProductionEnvShouldBeAskAboutConfirmationAndNotConfirmed():
{
$confirmation = new ApplicationInProduction(
AppEnvironment::Production,
$input = m::mock(InputInterface::class),
$output = m::mock(OutputInterface::class)
$input = $this->createMock(InputInterface::class),
$output = $this->createMock(SymfonyStyle::class)
);

$input->shouldReceive('hasOption')->once()->andReturnFalse();

$output->shouldReceive('writeln');
$output->shouldReceive('write');
$output->shouldReceive('newLine');

$output->shouldReceive('confirm')->once()->with('Do you really wish to run command?', false)->andReturnFalse();
$input->expects($this->once())->method('hasOption')->willReturn(false);
$output
->expects($this->once())
->method('confirm')
->with('Do you really wish to run command?', false)
->willReturn(false);

$this->assertFalse($confirmation->confirmToProceed());
}
Expand Down
5 changes: 3 additions & 2 deletions tests/app/src/Command/ApplicationInProductionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ final class ApplicationInProductionCommand extends Command
public function __invoke(ApplicationInProduction $confirmation, OutputInterface $output): int
{
if ($confirmation->confirmToProceed('Application in production.')) {
$this->writeln('Application is in production.');
$this->writeln('Allowed.');
return self::SUCCESS;
}

$this->writeln('Application is in testing.');
$this->writeln('Denied.');

return self::SUCCESS;
}
Expand Down

0 comments on commit c32dfed

Please sign in to comment.