Skip to content

Commit 790ad04

Browse files
authored
Merge pull request #33 from redthor/fix/confirmation-class-usage
Fix issue with default action continuing the commands
2 parents 8ace2bb + 78156b2 commit 790ad04

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/ExecuteCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function execute(InputInterface $input, OutputInterface $output)
6969
$version->execute($direction, $replay);
7070
} else {
7171
$question = new ConfirmationQuestion(
72-
'<question>WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/n)</question> ',
73-
'n'
72+
'<question>WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/[n])</question> ',
73+
false
7474
);
7575

7676
$confirmation = $this

src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/MigrateCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public function execute(InputInterface $input, OutputInterface $output)
8383

8484
if (!$noInteraction) {
8585
$question = new ConfirmationQuestion(
86-
'<question>Are you sure you wish to continue? (y/n)</question> ',
87-
'n'
86+
'<question>Are you sure you wish to continue? (y/[n])</question> ',
87+
false
8888
);
8989

9090
$confirmation = $this
@@ -102,8 +102,8 @@ public function execute(InputInterface $input, OutputInterface $output)
102102
// warn the user if no dry run and interaction is on
103103
if (!$noInteraction) {
104104
$question = new ConfirmationQuestion(
105-
'<question>WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/n)</question> ',
106-
'n'
105+
'<question>WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/[n])</question> ',
106+
false
107107
);
108108

109109
$confirmation = $this

tests/AntiMattr/Tests/MongoDB/Migrations/Tools/Console/Command/ExecuteCommandTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Console\Application;
88
use Symfony\Component\Console\Input\ArgvInput;
99
use Symfony\Component\Console\Helper\HelperSet;
10+
use Symfony\Component\Console\Tester\CommandTester;
1011

1112
/**
1213
* @author Ryan Catlin <ryan.catlin@gmail.com>
@@ -127,6 +128,34 @@ public function testExecuteUpWithInteraction()
127128
);
128129
}
129130

131+
public function testDefaultResponseWillCancelExecute()
132+
{
133+
// Do NOT expect this to be called
134+
$this->version->expects($this->never())
135+
->method('execute')
136+
->with('up', false)
137+
;
138+
139+
$numVersion = '000123456789';
140+
$this->config->expects($this->once())
141+
->method('getVersion')
142+
->with($numVersion)
143+
->willReturn($this->version)
144+
;
145+
146+
$this->command->setMigrationConfiguration($this->config);
147+
148+
$application = new Application();
149+
$application->setAutoExit(false);
150+
$application->add($this->command);
151+
152+
$commandTester = new CommandTester($this->command);
153+
$commandTester->setInputs(["\n"]);
154+
$commandTester->execute(['version' => $numVersion]);
155+
156+
$this->assertRegExp('/Migration cancelled/', $commandTester->getDisplay());
157+
}
158+
130159
public function testExecuteReplayWithoutInteraction()
131160
{
132161
// Variables and Objects

tests/AntiMattr/Tests/MongoDB/Migrations/Tools/Console/Command/MigrateCommandTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use AntiMattr\MongoDB\Migrations\Configuration\Configuration;
66
use AntiMattr\MongoDB\Migrations\Migration;
7+
use AntiMattr\MongoDB\Migrations\Version;
78
use AntiMattr\MongoDB\Migrations\Tools\Console\Command\MigrateCommand;
89
use AntiMattr\TestCase\AntiMattrTestCase;
910
use Symfony\Component\Console\Application;
1011
use Symfony\Component\Console\Helper\HelperSet;
1112
use Symfony\Component\Console\Input\ArgvInput;
1213
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Console\Tester\CommandTester;
1315

1416
class MigrateCommandTest extends AntiMattrTestCase
1517
{
@@ -139,6 +141,78 @@ public function testExecute()
139141
$this->output
140142
);
141143
}
144+
145+
public function testDefaultInteractionWillCancelMigration()
146+
{
147+
$migration = $this->buildMock(Migration::class);
148+
$numVersion = '000123456789';
149+
150+
// We do not expect this to be called
151+
$migration->expects($this->never())
152+
->method('migrate')
153+
->with($numVersion)
154+
;
155+
$this->command->setMigration($migration);
156+
157+
$configuration = $this->buildMock(Configuration::class);
158+
$configuration->expects($this->once())
159+
->method('getAvailableVersions')
160+
->willReturn([])
161+
;
162+
$this->command->setMigrationConfiguration($configuration);
163+
164+
$executedVersion = $this->buildMock(Version::class);
165+
$configuration->expects($this->once())
166+
->method('getMigratedVersions')
167+
->willReturn([$executedVersion])
168+
;
169+
170+
$application = new Application();
171+
$application->setAutoExit(false);
172+
$application->add($this->command);
173+
174+
$commandTester = new CommandTester($this->command);
175+
$commandTester->setInputs(["\n"]);
176+
$commandTester->execute(['version' => $numVersion]);
177+
178+
$this->assertRegExp('/Migration cancelled/', $commandTester->getDisplay());
179+
}
180+
181+
public function testDefaultSecondInteractionWillCancelMigration()
182+
{
183+
$migration = $this->buildMock(Migration::class);
184+
$numVersion = '000123456789';
185+
186+
// We do not expect this to be called
187+
$migration->expects($this->never())
188+
->method('migrate')
189+
->with($numVersion)
190+
;
191+
$this->command->setMigration($migration);
192+
193+
$configuration = $this->buildMock(Configuration::class);
194+
$configuration->expects($this->once())
195+
->method('getAvailableVersions')
196+
->willReturn([])
197+
;
198+
$this->command->setMigrationConfiguration($configuration);
199+
200+
$executedVersion = $this->buildMock(Version::class);
201+
$configuration->expects($this->once())
202+
->method('getMigratedVersions')
203+
->willReturn([$executedVersion])
204+
;
205+
206+
$application = new Application();
207+
$application->setAutoExit(false);
208+
$application->add($this->command);
209+
210+
$commandTester = new CommandTester($this->command);
211+
$commandTester->setInputs(['y', "\n"]);
212+
$commandTester->execute(['version' => $numVersion]);
213+
214+
$this->assertRegExp('/Migration cancelled/', $commandTester->getDisplay());
215+
}
142216
}
143217

144218
class MigrateCommandStub extends MigrateCommand

0 commit comments

Comments
 (0)