2
2
3
3
namespace Illuminated \Testing \Asserts ;
4
4
5
- use Mockery ;
6
- use Illuminate \Support \Str ;
7
5
use Illuminate \Support \Facades \File ;
6
+ use Illuminate \Support \Str ;
7
+ use Mockery ;
8
8
9
9
trait ArtisanAsserts
10
10
{
11
- protected function willSeeConfirmation ($ question , $ command , array $ parameters = [])
11
+ /**
12
+ * Add expectation that the given confirmation question would be shown.
13
+ *
14
+ * @param string $question
15
+ * @param string $command
16
+ * @param array $parameters
17
+ * @return void
18
+ */
19
+ protected function willSeeConfirmation (string $ question , string $ command , array $ parameters = [])
12
20
{
13
21
$ mock = Mockery::mock ("{$ command }[confirm] " );
14
22
$ mock ->shouldReceive ('confirm ' )->once ()->with ($ question );
15
23
16
24
$ this ->runArtisan ($ mock , $ parameters );
17
25
}
18
26
19
- protected function willNotSeeConfirmation ($ question , $ command , array $ parameters = [])
27
+ /**
28
+ * Add expectation that the given confirmation question would not be shown.
29
+ *
30
+ * @param string $question
31
+ * @param string $command
32
+ * @param array $parameters
33
+ * @return void
34
+ */
35
+ protected function willNotSeeConfirmation (string $ question , string $ command , array $ parameters = [])
20
36
{
21
37
$ mock = Mockery::mock ("{$ command }[confirm] " );
22
38
$ mock ->shouldNotReceive ('confirm ' )->with ($ question );
23
39
24
40
$ this ->runArtisan ($ mock , $ parameters );
25
41
}
26
42
27
- protected function willGiveConfirmation ($ question , $ command , array $ parameters = [])
43
+ /**
44
+ * Add expectation that the given confirmation question would be shown, and accept it.
45
+ *
46
+ * @param string $question
47
+ * @param string $command
48
+ * @param array $parameters
49
+ * @return void
50
+ */
51
+ protected function willGiveConfirmation (string $ question , string $ command , array $ parameters = [])
28
52
{
29
53
$ mock = Mockery::mock ("{$ command }[confirm] " );
30
54
$ mock ->shouldReceive ('confirm ' )->once ()->with ($ question )->andReturn (true );
31
55
32
56
$ this ->runArtisan ($ mock , $ parameters );
33
57
}
34
58
35
- protected function willNotGiveConfirmation ($ question , $ command , array $ parameters = [])
59
+ /**
60
+ * Add expectation that the given confirmation question would be shown, and do not accept it.
61
+ *
62
+ * @param string $question
63
+ * @param string $command
64
+ * @param array $parameters
65
+ * @return void
66
+ */
67
+ protected function willNotGiveConfirmation (string $ question , string $ command , array $ parameters = [])
36
68
{
37
69
$ mock = Mockery::mock ("{$ command }[confirm] " );
38
70
$ mock ->shouldReceive ('confirm ' )->once ()->with ($ question )->andReturn (false );
39
71
40
72
$ this ->runArtisan ($ mock , $ parameters );
41
73
}
42
74
43
- protected function seeArtisanOutput ($ output )
75
+ /**
76
+ * Assert that the given artisan output is seen.
77
+ *
78
+ * @param string $output
79
+ * @return void
80
+ */
81
+ protected function seeArtisanOutput (string $ output )
44
82
{
45
83
if (File::exists ($ output )) {
46
84
$ output = File::get ($ output );
@@ -51,7 +89,13 @@ protected function seeArtisanOutput($output)
51
89
$ this ->assertEquals ($ expected , $ actual , "Failed asserting that artisan output is ` {$ expected }`. " );
52
90
}
53
91
54
- protected function dontSeeArtisanOutput ($ output )
92
+ /**
93
+ * Assert that the given artisan output is not seen.
94
+ *
95
+ * @param string $output
96
+ * @return void
97
+ */
98
+ protected function dontSeeArtisanOutput (string $ output )
55
99
{
56
100
if (File::exists ($ output )) {
57
101
$ output = File::get ($ output );
@@ -62,7 +106,13 @@ protected function dontSeeArtisanOutput($output)
62
106
$ this ->assertNotEquals ($ expected , $ actual , "Failed asserting that artisan output is not ` {$ expected }`. " );
63
107
}
64
108
65
- protected function seeInArtisanOutput ($ needle )
109
+ /**
110
+ * Assert that the given string is seen in the artisan output.
111
+ *
112
+ * @param string $needle
113
+ * @return void
114
+ */
115
+ protected function seeInArtisanOutput (string $ needle )
66
116
{
67
117
if (File::exists ($ needle )) {
68
118
$ needle = File::get ($ needle );
@@ -73,7 +123,13 @@ protected function seeInArtisanOutput($needle)
73
123
$ this ->assertStringContainsString ($ needle , $ output , $ message );
74
124
}
75
125
76
- protected function dontSeeInArtisanOutput ($ needle )
126
+ /**
127
+ * Assert that the given string is not seen in the artisan output.
128
+ *
129
+ * @param string $needle
130
+ * @return void
131
+ */
132
+ protected function dontSeeInArtisanOutput (string $ needle )
77
133
{
78
134
if (File::exists ($ needle )) {
79
135
$ needle = File::get ($ needle );
@@ -84,53 +140,83 @@ protected function dontSeeInArtisanOutput($needle)
84
140
$ this ->assertStringNotContainsString ($ needle , $ output , $ message );
85
141
}
86
142
143
+ /**
144
+ * Assert that the given data is seen in the artisan output table.
145
+ *
146
+ * @param array $data
147
+ * @return void
148
+ */
87
149
protected function seeArtisanTableOutput (array $ data )
88
150
{
89
151
$ message = 'Failed asserting that artisan table output consists of expected data. ' ;
90
152
$ this ->assertEquals ($ data , $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput ()), $ message );
91
153
}
92
154
155
+ /**
156
+ * Assert that the given data is not seen in the artisan output table.
157
+ *
158
+ * @param array $data
159
+ * @return void
160
+ */
93
161
protected function dontSeeArtisanTableOutput (array $ data )
94
162
{
95
163
$ message = 'Failed asserting that artisan table output not consists of expected data. ' ;
96
164
$ this ->assertNotEquals ($ data , $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput ()), $ message );
97
165
}
98
166
99
- protected function seeArtisanTableRowsCount ($ count )
167
+ /**
168
+ * Assert that the artisan output table has the given number of data rows.
169
+ *
170
+ * @param int $count
171
+ * @return void
172
+ */
173
+ protected function seeArtisanTableRowsCount (int $ count )
100
174
{
101
175
$ message = "Failed asserting that artisan table rows count equals to ` {$ count }`. " ;
102
- $ this ->assertEquals ($ count , count ( $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput () )), $ message );
176
+ $ this ->assertCount ($ count , $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput ()), $ message );
103
177
}
104
178
105
- protected function dontSeeArtisanTableRowsCount ($ count )
179
+ /**
180
+ * Assert that the artisan output table doesn't have the given number of data rows.
181
+ *
182
+ * @param int $count
183
+ * @return void
184
+ */
185
+ protected function dontSeeArtisanTableRowsCount (int $ count )
106
186
{
107
187
$ message = "Failed asserting that artisan table rows count not equals to ` {$ count }`. " ;
108
- $ this ->assertNotEquals ($ count , count ( $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput () )), $ message );
188
+ $ this ->assertNotCount ($ count , $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput ()), $ message );
109
189
}
110
190
111
- private function parseArtisanTableOutput ($ output )
191
+ /**
192
+ * Parse the artisan table output.
193
+ *
194
+ * Return data rows with headers as keys.
195
+ *
196
+ * @param string $output
197
+ * @return array
198
+ */
199
+ private function parseArtisanTableOutput (string $ output )
112
200
{
113
- $ parsed = [];
114
-
115
- $ headers = [];
116
- $ outputRows = explode ("\n" , trim ($ output ));
117
- foreach ($ outputRows as $ row ) {
118
- if (!Str::contains ($ row , '| ' )) {
119
- continue ;
120
- }
121
-
122
- $ row = explode ('| ' , $ row );
123
- $ row = array_filter ($ row );
124
- $ row = array_map ('trim ' , $ row );
125
-
126
- if (empty ($ headers )) {
127
- $ headers = $ row ;
128
- continue ;
129
- }
130
-
131
- $ parsed [] = array_combine ($ headers , $ row );
132
- }
133
-
134
- return $ parsed ;
201
+ $ output = explode ("\n" , trim ($ output ));
202
+
203
+ // Filter and normalize the output
204
+ $ output = collect ($ output )
205
+ ->reject (function (string $ line ) {
206
+ return !Str::contains ($ line , '| ' );
207
+ })
208
+ ->map (function (string $ line ) {
209
+ $ line = explode ('| ' , $ line );
210
+ $ line = array_filter ($ line );
211
+ return array_map ('trim ' , $ line );
212
+ });
213
+
214
+ // The first item is headers
215
+ $ headers = $ output ->shift ();
216
+
217
+ // Combine headers with the line values
218
+ return $ output ->map (function (array $ values ) use ($ headers ) {
219
+ return array_combine ($ headers , $ values );
220
+ })->toArray ();
135
221
}
136
222
}
0 commit comments