22
33namespace Illuminated \Testing \Asserts ;
44
5- use Mockery ;
6- use Illuminate \Support \Str ;
75use Illuminate \Support \Facades \File ;
6+ use Illuminate \Support \Str ;
7+ use Mockery ;
88
99trait ArtisanAsserts
1010{
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 = [])
1220 {
1321 $ mock = Mockery::mock ("{$ command }[confirm] " );
1422 $ mock ->shouldReceive ('confirm ' )->once ()->with ($ question );
1523
1624 $ this ->runArtisan ($ mock , $ parameters );
1725 }
1826
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 = [])
2036 {
2137 $ mock = Mockery::mock ("{$ command }[confirm] " );
2238 $ mock ->shouldNotReceive ('confirm ' )->with ($ question );
2339
2440 $ this ->runArtisan ($ mock , $ parameters );
2541 }
2642
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 = [])
2852 {
2953 $ mock = Mockery::mock ("{$ command }[confirm] " );
3054 $ mock ->shouldReceive ('confirm ' )->once ()->with ($ question )->andReturn (true );
3155
3256 $ this ->runArtisan ($ mock , $ parameters );
3357 }
3458
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 = [])
3668 {
3769 $ mock = Mockery::mock ("{$ command }[confirm] " );
3870 $ mock ->shouldReceive ('confirm ' )->once ()->with ($ question )->andReturn (false );
3971
4072 $ this ->runArtisan ($ mock , $ parameters );
4173 }
4274
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 )
4482 {
4583 if (File::exists ($ output )) {
4684 $ output = File::get ($ output );
@@ -51,7 +89,13 @@ protected function seeArtisanOutput($output)
5189 $ this ->assertEquals ($ expected , $ actual , "Failed asserting that artisan output is ` {$ expected }`. " );
5290 }
5391
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 )
5599 {
56100 if (File::exists ($ output )) {
57101 $ output = File::get ($ output );
@@ -62,7 +106,13 @@ protected function dontSeeArtisanOutput($output)
62106 $ this ->assertNotEquals ($ expected , $ actual , "Failed asserting that artisan output is not ` {$ expected }`. " );
63107 }
64108
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 )
66116 {
67117 if (File::exists ($ needle )) {
68118 $ needle = File::get ($ needle );
@@ -73,7 +123,13 @@ protected function seeInArtisanOutput($needle)
73123 $ this ->assertStringContainsString ($ needle , $ output , $ message );
74124 }
75125
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 )
77133 {
78134 if (File::exists ($ needle )) {
79135 $ needle = File::get ($ needle );
@@ -84,53 +140,83 @@ protected function dontSeeInArtisanOutput($needle)
84140 $ this ->assertStringNotContainsString ($ needle , $ output , $ message );
85141 }
86142
143+ /**
144+ * Assert that the given data is seen in the artisan output table.
145+ *
146+ * @param array $data
147+ * @return void
148+ */
87149 protected function seeArtisanTableOutput (array $ data )
88150 {
89151 $ message = 'Failed asserting that artisan table output consists of expected data. ' ;
90152 $ this ->assertEquals ($ data , $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput ()), $ message );
91153 }
92154
155+ /**
156+ * Assert that the given data is not seen in the artisan output table.
157+ *
158+ * @param array $data
159+ * @return void
160+ */
93161 protected function dontSeeArtisanTableOutput (array $ data )
94162 {
95163 $ message = 'Failed asserting that artisan table output not consists of expected data. ' ;
96164 $ this ->assertNotEquals ($ data , $ this ->parseArtisanTableOutput ($ this ->getArtisanOutput ()), $ message );
97165 }
98166
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 )
100174 {
101175 $ 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 );
103177 }
104178
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 )
106186 {
107187 $ 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 );
109189 }
110190
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 )
112200 {
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 ();
135221 }
136222}
0 commit comments