| 1 | <?php |
||
| 2 | |||
| 3 | namespace VGirol\JsonApiAssert\Laravel\Asserts\Content; |
||
| 4 | |||
| 5 | use Illuminate\Testing\TestResponse; |
||
| 6 | use VGirol\JsonApiConstant\Members; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * This trait adds the ability to test links object. |
||
| 10 | */ |
||
| 11 | trait AssertLinks |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Asserts that a document links object equals an expected array of links. |
||
| 15 | * |
||
| 16 | * @param TestResponse $response |
||
| 17 | * @param array $expected |
||
| 18 | * |
||
| 19 | * @return void |
||
| 20 | * @throws \PHPUnit\Framework\AssertionFailedError |
||
| 21 | */ |
||
| 22 | 12 | public static function assertDocumentLinksObjectEquals(TestResponse $response, $expected) |
|
| 23 | { |
||
| 24 | 12 | if (!\is_array($expected)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | 3 | static::invalidArgument( |
|
| 26 | 3 | 2, |
|
| 27 | 3 | 'array', |
|
| 28 | $expected |
||
| 29 | ); |
||
| 30 | } |
||
| 31 | |||
| 32 | // Decode JSON response |
||
| 33 | 9 | $json = $response->json(); |
|
| 34 | |||
| 35 | 9 | static::assertHasLinks($json); |
|
| 36 | |||
| 37 | 6 | $links = $json[Members::LINKS]; |
|
| 38 | |||
| 39 | 6 | static::assertLinksObjectEquals($expected, $links); |
|
| 40 | 3 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Asserts that a document links object contains an expected array of links. |
||
| 44 | * |
||
| 45 | * @param TestResponse $response |
||
| 46 | * @param array $expected |
||
| 47 | * |
||
| 48 | * @return void |
||
| 49 | * @throws \PHPUnit\Framework\AssertionFailedError |
||
| 50 | */ |
||
| 51 | 12 | public static function assertDocumentLinksObjectContains(TestResponse $response, $expected) |
|
| 52 | { |
||
| 53 | 12 | if (!\is_array($expected)) { |
|
|
0 ignored issues
–
show
|
|||
| 54 | 3 | static::invalidArgument( |
|
| 55 | 3 | 2, |
|
| 56 | 3 | 'array', |
|
| 57 | $expected |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | // Decode JSON response |
||
| 62 | 9 | $json = $response->json(); |
|
| 63 | |||
| 64 | 9 | static::assertHasLinks($json); |
|
| 65 | |||
| 66 | 6 | $links = $json[Members::LINKS]; |
|
| 67 | |||
| 68 | 6 | foreach ($expected as $name => $value) { |
|
| 69 | 6 | static::assertLinksObjectContains($name, $value, $links); |
|
| 70 | } |
||
| 71 | 3 | } |
|
| 72 | } |
||
| 73 |