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

AssertResource   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertResourceCollectionContains() 0 8 3
A assertResourceObjectEquals() 0 3 1
A assertResourceCollectionEquals() 0 9 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