10
10
use Symfony \Component \Console \Exception \ExceptionInterface ;
11
11
use Symfony \Component \Console \Input \InputInterface ;
12
12
use Symfony \Component \Console \Output \OutputInterface ;
13
+ use Symfony \Component \Console \Output \StreamOutput ;
13
14
use Wundii \DataMapper \SymfonyBundle \Command \DefaultConfigCommand ;
14
15
15
16
class DefaultConfigCommandTest extends TestCase
@@ -22,6 +23,8 @@ class DefaultConfigCommandTest extends TestCase
22
23
23
24
private string $ defaultConfigFile ;
24
25
26
+ private OutputInterface $ output ;
27
+
25
28
protected function setUp (): void
26
29
{
27
30
$ this ->tempDir = sys_get_temp_dir () . '/datamapper_test_ ' . uniqid ();
@@ -31,13 +34,32 @@ protected function setUp(): void
31
34
$ this ->configFile = $ this ->configDir . '/data_mapper.yaml ' ;
32
35
33
36
$ this ->defaultConfigFile = dirname (__DIR__ ) . '/src/Resources/config/packages/data_mapper.yaml ' ;
37
+
38
+ $ this ->output = new StreamOutput (fopen ('php://memory ' , 'w+ ' ));
34
39
}
35
40
36
41
protected function tearDown (): void
37
42
{
38
43
$ this ->removeDirectory ($ this ->tempDir );
39
44
}
40
45
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
+
41
63
/**
42
64
* @throws ExceptionInterface
43
65
* @throws Exception
@@ -49,16 +71,17 @@ public function testCreatesDefaultConfigFileSuccessfully(): void
49
71
$ command = new DefaultConfigCommand ();
50
72
51
73
$ input = $ this ->createMock (InputInterface::class);
52
- $ output = $ this ->createMock (OutputInterface::class);
53
74
54
- $ result = $ command ->run ($ input , $ output );
75
+ $ result = $ command ->run ($ input , $ this -> output );
55
76
56
77
$ this ->assertSame (Command::SUCCESS , $ result );
57
78
$ this ->assertFileExists ($ this ->configFile );
58
79
$ this ->assertSame (
59
80
file_get_contents ($ this ->defaultConfigFile ),
60
81
file_get_contents ($ this ->configFile )
61
82
);
83
+
84
+ $ this ->assertStringContainsString ('Default configuration file created at config/packages/data_mapper.yaml ' , $ this ->getDisplay ());
62
85
}
63
86
64
87
/**
@@ -75,12 +98,22 @@ public function testWarnsIfConfigAlreadyExists(): void
75
98
$ command = new DefaultConfigCommand ();
76
99
77
100
$ input = $ this ->createMock (InputInterface::class);
78
- $ output = $ this ->createMock (OutputInterface::class);
79
101
80
- $ result = $ command ->run ($ input , $ output );
102
+ $ result = $ command ->run ($ input , $ this -> output );
81
103
82
104
$ this ->assertSame (Command::FAILURE , $ result );
83
105
$ 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 );
84
117
}
85
118
86
119
private function removeDirectory (string $ dir ): void
0 commit comments