Skip to content

Commit 6bd5e20

Browse files
committed
Adds a dry-run option
1 parent 240fdf7 commit 6bd5e20

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/Commands/InitCommand.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ protected function configure(): void
7575
$presetDescription,
7676
self::DEFAULT_PRESET
7777
);
78+
$this->getDefinition()->addOption(new InputOption(
79+
'dry-run',
80+
null,
81+
InputOption::VALUE_NONE,
82+
'Do not write any files. Output the content that would be written'
83+
));
7884
}
7985

8086
/**
@@ -154,6 +160,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
154160

155161
$lpvFileContent = \implode("\n", $defaultGlobPattern);
156162

163+
if ($input->getOption('dry-run') === true) {
164+
$output->writeln($lpvFileContent);
165+
166+
return self::SUCCESS;
167+
}
168+
157169
$bytesWritten = file_put_contents(
158170
$defaultLpvFile,
159171
$lpvFileContent

tests/Commands/InitCommandTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class InitCommandTest extends TestCase
2121
{
2222
/**
23-
* Set up test environment.
23+
* Set up a test environment.
2424
*/
2525
protected function setUp(): void
2626
{
@@ -32,7 +32,7 @@ protected function setUp(): void
3232
}
3333

3434
/**
35-
* Tear down test environment.
35+
* Tear down the test environment.
3636
*
3737
* @return void
3838
*/
@@ -43,6 +43,25 @@ protected function tearDown(): void
4343
}
4444
}
4545

46+
#[Test]
47+
public function printsContentWithoutWritingAFile(): void
48+
{
49+
$command = new InitCommand(new Analyser(new Finder(new PhpPreset())));
50+
$tester = new CommandTester($command);
51+
52+
$exitCode = $tester->execute([
53+
'--preset' => 'PHP',
54+
'--dry-run' => true,
55+
]);
56+
57+
$this->assertSame(0, $exitCode);
58+
59+
$display = $tester->getDisplay();
60+
61+
$this->assertStringContainsString('phpunit*', $display);
62+
63+
$this->assertFileDoesNotExist($this->temporaryDirectory.DIRECTORY_SEPARATOR.'.lpv');
64+
}
4665
#[Test]
4766
public function createsExpectedDefaultLpvFile(): void
4867
{

0 commit comments

Comments
 (0)