Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

AssertResource::assertResourceCollectionContains()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert\Asserts\Content;
6
7
use PHPUnit\Framework\Assert as PHPUnit;
8
9
trait AssertResource
10
{
11
    /**
12
     * Asserts that a resource object correspond to a given model.
13
     *
14
     * @param array $expected
15
     * @param array $json
16
     */
17
    public static function assertResourceObjectEquals($expected, $json)
18
    {
19
        PHPUnit::assertEquals($expected, $json);
20
    }
21
22
    /**
23
     * Asserts that an array of resource objects is equal to a given collection (same values and same order).
24
     *
25
     * @param array $expected
26
     * @param array $json
27
     */
28
    public static function assertResourceCollectionEquals($expected, $json)
29
    {
30
        static::assertIsArrayOfObjects($expected);
31
        PHPUnit::assertEquals(count($expected), count($json));
32
33
        $index = 0;
34
        foreach ($expected as $resource) {
35
            static::assertResourceObjectEquals($resource, $json[$index]);
36
            $index++;
37
        }
38
    }
39
40
    /**
41
     * Asserts that an array of resource objects contains a given collection.
42
     *
43
     * @param array $expected
44
     * @param array $json
45
     */
46
    public static function assertResourceCollectionContains($expected, $json)
47
    {
48
        if (!static::isArrayOfObjects($expected)) {
49
            $expected = [$expected];
50
        }
51
52
        foreach ($expected as $resource) {
53
            PHPUnit::assertContains($resource, $json);
54
        }
55
    }
56
}
57