Skip to content

Commit 45f32a1

Browse files
committed
Merge branch 'master' into 6.x
2 parents 4df428f + 67a8dd8 commit 45f32a1

37 files changed

+996
-246
lines changed

README.md

+68-68
Large diffs are not rendered by default.

src/Asserts/ArtisanAsserts.php

+123-37
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,83 @@
22

33
namespace Illuminated\Testing\Asserts;
44

5-
use Mockery;
6-
use Illuminate\Support\Str;
75
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Str;
7+
use Mockery;
88

99
trait 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
}

src/Asserts/CollectionAsserts.php

+28-18
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,37 @@
66

77
trait CollectionAsserts
88
{
9-
protected function assertCollectionsEqual(Collection $collection1, Collection $collection2, $key)
9+
/**
10+
* Assert that the given collections are equal based on the specified key.
11+
*
12+
* @param \Illuminate\Support\Collection $collection1
13+
* @param \Illuminate\Support\Collection $collection2
14+
* @param string $key
15+
* @return void
16+
*/
17+
protected function assertCollectionsEqual(Collection $collection1, Collection $collection2, string $key)
1018
{
11-
$keys1 = $collection1->pluck($key)->toArray();
12-
$keys2 = $collection2->pluck($key)->toArray();
13-
14-
$diff1 = array_diff($keys1, $keys2);
15-
$diff2 = array_diff($keys2, $keys1);
16-
17-
$bothDiffsAreEmpty = (empty($diff1) && empty($diff2));
18-
$this->assertTrue($bothDiffsAreEmpty, 'Failed asserting that collections are equal.');
19+
$this->assertEquals(
20+
$collection1->pluck($key)->sort()->values(),
21+
$collection2->pluck($key)->sort()->values(),
22+
'Failed asserting that collections are equal.'
23+
);
1924
}
2025

21-
protected function assertCollectionsNotEqual(Collection $collection1, Collection $collection2, $key)
26+
/**
27+
* Assert that the given collections are not equal based on the specified key.
28+
*
29+
* @param \Illuminate\Support\Collection $collection1
30+
* @param \Illuminate\Support\Collection $collection2
31+
* @param string $key
32+
* @return void
33+
*/
34+
protected function assertCollectionsNotEqual(Collection $collection1, Collection $collection2, string $key)
2235
{
23-
$keys1 = $collection1->pluck($key)->toArray();
24-
$keys2 = $collection2->pluck($key)->toArray();
25-
26-
$diff1 = array_diff($keys1, $keys2);
27-
$diff2 = array_diff($keys2, $keys1);
28-
29-
$atLeastOneDiffIsNotEmpty = (!empty($diff1) || !empty($diff2));
30-
$this->assertTrue($atLeastOneDiffIsNotEmpty, 'Failed asserting that collections are not equal.');
36+
$this->assertNotEquals(
37+
$collection1->pluck($key)->sort()->values(),
38+
$collection2->pluck($key)->sort()->values(),
39+
'Failed asserting that collections are not equal.'
40+
);
3141
}
3242
}

src/Asserts/DatabaseAsserts.php

+38-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,42 @@
66

77
trait DatabaseAsserts
88
{
9-
protected function assertDatabaseHasTable($table)
9+
/**
10+
* Assert that database has the given table.
11+
*
12+
* @param string $table
13+
* @return void
14+
*/
15+
protected function assertDatabaseHasTable(string $table)
1016
{
11-
$this->assertTrue(Schema::hasTable($table), "Failed asserting that database has table `{$table}`.");
17+
$this->assertTrue(
18+
Schema::hasTable($table),
19+
"Failed asserting that database has table `{$table}`."
20+
);
1221
}
1322

14-
protected function assertDatabaseMissingTable($table)
23+
/**
24+
* Assert that database doesn't have the given table.
25+
*
26+
* @param string $table
27+
* @return void
28+
*/
29+
protected function assertDatabaseMissingTable(string $table)
1530
{
16-
$this->assertFalse(Schema::hasTable($table), "Failed asserting that database missing table `{$table}`.");
31+
$this->assertFalse(
32+
Schema::hasTable($table),
33+
"Failed asserting that database missing table `{$table}`."
34+
);
1735
}
1836

19-
protected function assertDatabaseHasMany($table, array $rows)
37+
/**
38+
* Assert that database has all of the given rows.
39+
*
40+
* @param string $table
41+
* @param array $rows
42+
* @return $this
43+
*/
44+
protected function assertDatabaseHasMany(string $table, array $rows)
2045
{
2146
foreach ($rows as $row) {
2247
$this->assertDatabaseHas($table, $row);
@@ -25,7 +50,14 @@ protected function assertDatabaseHasMany($table, array $rows)
2550
return $this;
2651
}
2752

28-
protected function assertDatabaseMissingMany($table, array $rows)
53+
/**
54+
* Assert that database doesn't have all of the given rows.
55+
*
56+
* @param string $table
57+
* @param array $rows
58+
* @return $this
59+
*/
60+
protected function assertDatabaseMissingMany(string $table, array $rows)
2961
{
3062
foreach ($rows as $row) {
3163
$this->assertDatabaseMissing($table, $row);

0 commit comments

Comments
 (0)