1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiAssert\Asserts\Content; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
8
|
|
|
use VGirol\JsonApiAssert\Messages; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This trait adds the ability to test resource object (single or collection). |
12
|
|
|
*/ |
13
|
|
|
trait AssertResource |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Asserts that a resource object equals a given resource object. |
17
|
|
|
* |
18
|
|
|
* @link https://jsonapi.org/format/#document-resource-objects |
19
|
|
|
* |
20
|
|
|
* @param array $expected |
21
|
|
|
* @param array $json |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
25
|
|
|
*/ |
26
|
12 |
|
public static function assertResourceObjectEquals($expected, $json) |
27
|
|
|
{ |
28
|
12 |
|
PHPUnit::assertEquals( |
29
|
12 |
|
$expected, |
30
|
12 |
|
$json, |
31
|
12 |
|
sprintf(Messages::RESOURCE_IS_NOT_EQUAL, var_export($json, true), var_export($expected, true)) |
32
|
|
|
); |
33
|
9 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Asserts that an array of resource objects is equal to a given collection (same values and same order). |
37
|
|
|
* |
38
|
|
|
* It will do the following checks : |
39
|
|
|
* 1) asserts that the array to check is an array of objects (@see assertIsArrayOfObjects). |
40
|
|
|
* 2) asserts that the two arrays have the same count of items. |
41
|
|
|
* 3) asserts that each resource object of the array correspond to the resource object |
42
|
|
|
* at the same index in the given expected array (@see assertResourceObjectEquals). |
43
|
|
|
* |
44
|
|
|
* @link https://jsonapi.org/format/#document-resource-objects |
45
|
|
|
* |
46
|
|
|
* @param array $expected |
47
|
|
|
* @param array $json |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
51
|
|
|
*/ |
52
|
12 |
|
public static function assertResourceCollectionEquals($expected, $json) |
53
|
|
|
{ |
54
|
12 |
|
static::assertIsArrayOfObjects($expected); |
55
|
|
|
|
56
|
9 |
|
$count = count($json); |
57
|
9 |
|
$expectedCount = count($expected); |
58
|
9 |
|
PHPUnit::assertEquals( |
59
|
9 |
|
$expectedCount, |
60
|
9 |
|
$count, |
61
|
9 |
|
sprintf(Messages::RESOURCE_COLLECTION_HAVE_NOT_SAME_LENGTH, $count, $expectedCount) |
62
|
|
|
); |
63
|
|
|
|
64
|
6 |
|
foreach ($expected as $index => $resource) { |
65
|
6 |
|
static::assertResourceObjectEquals($resource, $json[$index]); |
66
|
|
|
} |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Asserts that an array of resource objects contains a given subset of resource objects. |
71
|
|
|
* |
72
|
|
|
* @link https://jsonapi.org/format/#document-resource-objects |
73
|
|
|
* |
74
|
|
|
* @param array $expected |
75
|
|
|
* @param array $json |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
79
|
|
|
*/ |
80
|
15 |
|
public static function assertResourceCollectionContains($expected, $json) |
81
|
|
|
{ |
82
|
15 |
|
if (!static::isArrayOfObjects($expected)) { |
83
|
12 |
|
$expected = [$expected]; |
84
|
|
|
} |
85
|
|
|
|
86
|
15 |
|
foreach ($expected as $resource) { |
87
|
15 |
|
PHPUnit::assertContains($resource, $json); |
88
|
|
|
} |
89
|
9 |
|
} |
90
|
|
|
} |
91
|
|
|
|