forked from tighten/ziggy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCase.php
51 lines (40 loc) · 1.25 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Tightenco\Tests;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Tightenco\Ziggy\ZiggyServiceProvider;
class TestCase extends OrchestraTestCase
{
protected function getPackageProviders($app)
{
return [
ZiggyServiceProvider::class,
];
}
protected function assertJsonContains(array $haystack, array $needle)
{
$actual = json_encode(Arr::sortRecursive(
(array) $haystack
));
foreach (Arr::sortRecursive($needle) as $key => $value) {
$expected = $this->formatToExpectedJson($key, $value);
$this->assertTrue(
Str::contains($actual, $expected),
'Unable to find JSON fragment'.PHP_EOL."[{$expected}]".PHP_EOL.'within'.PHP_EOL."[{$actual}]."
);
}
return $this;
}
protected function formatToExpectedJson($key, $value)
{
$expected = json_encode([$key => $value]);
if (Str::startsWith($expected, '{')) {
$expected = substr($expected, 1);
}
if (Str::endsWith($expected, '}')) {
$expected = substr($expected, 0, -1);
}
return trim($expected);
}
}