Skip to content

Commit 07b008e

Browse files
authored
feat: allow DeleteEnvironmentCommand to run in non-interactive mode
1 parent 26c77eb commit 07b008e

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

src/Command/Environment/DeleteEnvironmentCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ protected function configure()
3636
$this
3737
->setName(self::NAME)
3838
->setDescription('Delete an environment')
39-
->addArgument('environment', InputArgument::OPTIONAL, 'The name of the environment to delete');
39+
->addArgument('environment', InputArgument::OPTIONAL, 'The name of the environment to delete')
40+
->addOption('--delete-resources', null);
4041
}
4142

4243
/**
@@ -46,11 +47,11 @@ protected function perform(InputInterface $input, OutputInterface $output)
4647
{
4748
$environment = $this->determineEnvironment($input, $output);
4849

49-
if (!$output->confirm(sprintf('Are you sure you want to delete the "<comment>%s</comment>" environment?', $environment), false)) {
50+
if (!$output->confirm(sprintf('Are you sure you want to delete the "<comment>%s</comment>" environment?', $environment), !$input->isInteractive())) {
5051
return;
5152
}
5253

53-
$deleteResources = $output->confirm('Do you want to delete all the environment resources on the cloud provider?', false);
54+
$deleteResources = $this->getBooleanOption($input, 'delete-resources') || $output->confirm('Do you want to delete all the environment resources on the cloud provider?', false);
5455

5556
$this->apiClient->deleteEnvironment($this->projectConfiguration->getProjectId(), $environment, $deleteResources);
5657

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir command-line tool.
7+
*
8+
* (c) Carl Alexander <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Command\Environment;
15+
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
use Ymir\Cli\Command\Environment\DeleteEnvironmentCommand;
18+
use Ymir\Cli\Tests\Mock\ApiClientMockTrait;
19+
use Ymir\Cli\Tests\Mock\CliConfigurationMockTrait;
20+
use Ymir\Cli\Tests\Mock\ProjectConfigurationMockTrait;
21+
use Ymir\Cli\Tests\Unit\TestCase;
22+
23+
/**
24+
* @covers \Ymir\Cli\Command\Environment\DeleteEnvironmentCommand
25+
*/
26+
class DeleteEnvironmentCommandTest extends TestCase
27+
{
28+
use ApiClientMockTrait;
29+
use CliConfigurationMockTrait;
30+
use ProjectConfigurationMockTrait;
31+
32+
public function testDeletesEnvironment()
33+
{
34+
$environment = $this->faker->word;
35+
$inputs = ['yes', 'no'];
36+
$input = ['environment' => $environment];
37+
$interactive = true;
38+
$expectedWithResourcesValue = false;
39+
40+
$this->assertDeletesEnvironment($environment, $inputs, $input, $interactive, $expectedWithResourcesValue);
41+
}
42+
43+
public function testDeletesEnvironmentWithoutInteraction()
44+
{
45+
$environment = $this->faker->word;
46+
$inputs = [];
47+
$input = ['environment' => $environment];
48+
$interactive = false;
49+
$expectedWithResourcesValue = false;
50+
51+
$this->assertDeletesEnvironment($environment, $inputs, $input, $interactive, $expectedWithResourcesValue);
52+
}
53+
54+
public function testDeletesEnvironmentWithoutInteractionWithResources()
55+
{
56+
$environment = $this->faker->word;
57+
$inputs = [];
58+
$input = ['environment' => $environment, '--delete-resources' => null];
59+
$interactive = false;
60+
$expectedWithResourcesValue = true;
61+
62+
$this->assertDeletesEnvironment($environment, $inputs, $input, $interactive, $expectedWithResourcesValue);
63+
}
64+
65+
public function testDeletesEnvironmentWithResources()
66+
{
67+
$environment = $this->faker->word;
68+
$inputs = ['yes', 'yes'];
69+
$input = ['environment' => $environment];
70+
$interactive = true;
71+
$expectedWithResourcesValue = true;
72+
73+
$this->assertDeletesEnvironment($environment, $inputs, $input, $interactive, $expectedWithResourcesValue);
74+
}
75+
76+
private function assertDeletesEnvironment(string $environment, array $inputs, array $input, bool $interactive, bool $expectedWithResourcesValue)
77+
{
78+
$apiClient = $this->getApiClientMock();
79+
$apiClient->expects($this->once())
80+
->method('isAuthenticated')
81+
->willReturn(true);
82+
$cliConfiguration = $this->getCliConfigurationMock();
83+
$projectConfiguration = $this->getProjectConfigurationMock();
84+
85+
$apiClient->expects($this->once())
86+
->method('getEnvironments')
87+
->with($this->equalTo($projectConfiguration->getProjectId()))
88+
->willReturn(collect([['name' => $environment]]));
89+
90+
$apiClient->expects($this->once())
91+
->method('deleteEnvironment')
92+
->with($this->equalTo($projectConfiguration->getProjectId()), $this->equalTo($environment), $this->equalTo($expectedWithResourcesValue));
93+
94+
$projectConfiguration->expects($this->once())
95+
->method('deleteEnvironment')
96+
->with($this->equalTo($environment));
97+
98+
$commandTester = new CommandTester(new DeleteEnvironmentCommand($apiClient, $cliConfiguration, $projectConfiguration));
99+
100+
$commandTester->setInputs($inputs);
101+
102+
$commandTester->execute($input, ['interactive' => $interactive]);
103+
104+
$commandTester->assertCommandIsSuccessful();
105+
106+
$this->assertStringContainsString('Environment deleted', $commandTester->getDisplay());
107+
}
108+
}

0 commit comments

Comments
 (0)