Skip to content

Commit 709de3a

Browse files
author
awu
committed
fix: refactor DefaultConfigCommand to set name and description in configure method
1 parent 2bbd084 commit 709de3a

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/Command/DefaultConfigCommand.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111

1212
class DefaultConfigCommand extends Command
1313
{
14-
/**
15-
* @phpstan-ignore-next-line
16-
*/
17-
protected static $defaultDescription = 'Create a default configuration file for the DataMapper bundle: config/packages/data_mapper.yaml';
18-
19-
/**
20-
* @phpstan-ignore-next-line
21-
*/
22-
protected static $defaultName = 'data-mapper:default-config';
14+
protected function configure(): void
15+
{
16+
$this->setName('data-mapper:default-config');
17+
$this->setDescription('Create a default configuration file for the DataMapper bundle: config/packages/data_mapper.yaml');
18+
}
2319

2420
protected function execute(InputInterface $input, OutputInterface $output): int
2521
{

tests/DefaultConfigCommandTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Console\Exception\ExceptionInterface;
1111
use Symfony\Component\Console\Input\InputInterface;
1212
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Output\StreamOutput;
1314
use Wundii\DataMapper\SymfonyBundle\Command\DefaultConfigCommand;
1415

1516
class DefaultConfigCommandTest extends TestCase
@@ -22,6 +23,8 @@ class DefaultConfigCommandTest extends TestCase
2223

2324
private string $defaultConfigFile;
2425

26+
private OutputInterface $output;
27+
2528
protected function setUp(): void
2629
{
2730
$this->tempDir = sys_get_temp_dir() . '/datamapper_test_' . uniqid();
@@ -31,13 +34,32 @@ protected function setUp(): void
3134
$this->configFile = $this->configDir . '/data_mapper.yaml';
3235

3336
$this->defaultConfigFile = dirname(__DIR__) . '/src/Resources/config/packages/data_mapper.yaml';
37+
38+
$this->output = new StreamOutput(fopen('php://memory', 'w+'));
3439
}
3540

3641
protected function tearDown(): void
3742
{
3843
$this->removeDirectory($this->tempDir);
3944
}
4045

46+
public function testDefaultInformation(): void
47+
{
48+
$command = new DefaultConfigCommand();
49+
50+
$name = $command->getName();
51+
$description = $command->getDescription();
52+
53+
$this->assertSame(
54+
'Create a default configuration file for the DataMapper bundle: config/packages/data_mapper.yaml',
55+
$description
56+
);
57+
$this->assertSame(
58+
'data-mapper:default-config',
59+
$name
60+
);
61+
}
62+
4163
/**
4264
* @throws ExceptionInterface
4365
* @throws Exception
@@ -49,16 +71,17 @@ public function testCreatesDefaultConfigFileSuccessfully(): void
4971
$command = new DefaultConfigCommand();
5072

5173
$input = $this->createMock(InputInterface::class);
52-
$output = $this->createMock(OutputInterface::class);
5374

54-
$result = $command->run($input, $output);
75+
$result = $command->run($input, $this->output);
5576

5677
$this->assertSame(Command::SUCCESS, $result);
5778
$this->assertFileExists($this->configFile);
5879
$this->assertSame(
5980
file_get_contents($this->defaultConfigFile),
6081
file_get_contents($this->configFile)
6182
);
83+
84+
$this->assertStringContainsString('Default configuration file created at config/packages/data_mapper.yaml', $this->getDisplay());
6285
}
6386

6487
/**
@@ -75,12 +98,22 @@ public function testWarnsIfConfigAlreadyExists(): void
7598
$command = new DefaultConfigCommand();
7699

77100
$input = $this->createMock(InputInterface::class);
78-
$output = $this->createMock(OutputInterface::class);
79101

80-
$result = $command->run($input, $output);
102+
$result = $command->run($input, $this->output);
81103

82104
$this->assertSame(Command::FAILURE, $result);
83105
$this->assertSame("existing: true\n", file_get_contents($this->configFile));
106+
107+
$this->assertStringContainsString('The file config/packages/data_mapper.yaml already exists. No changes were made.', $this->getDisplay());
108+
}
109+
110+
public function getDisplay(): string
111+
{
112+
rewind($this->output->getStream());
113+
114+
$display = (string) stream_get_contents($this->output->getStream());
115+
$display = str_replace(["\r\n", "\n"], '', $display);
116+
return preg_replace('/[ \t]+/', ' ', $display);
84117
}
85118

86119
private function removeDirectory(string $dir): void

0 commit comments

Comments
 (0)