Passed
Push — master ( 1e6084...cf392e )
by Vincent
02:40
created

AssertRelationshipsObject::isToManyResourceLinkage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 5
cts 8
cp 0.625
crap 4.8437
rs 10
c 0
b 0
f 0
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
use VGirol\JsonApiAssert\Messages;
6
use PHPUnit\Framework\ExpectationFailedException;
7
8
trait AssertRelationshipsObject
9
{
10
    /**
11
     * Asserts that a relationships object is valid.
12
     *
13
     * @param array     $relationships
14
     * @param boolean   $strict         If true, excludes not safe characters when checking members name
15
     *
16
     * @throws PHPUnit\Framework\ExpectationFailedException
17
     */
18 9
    public static function assertIsValidRelationshipsObject($relationships, $strict)
19
    {
20 9
        static::assertIsNotArrayOfObjects($relationships);
21
22 8
        foreach ($relationships as $key => $relationship) {
23 8
            static::assertIsValidMemberName($key, $strict);
24 6
            static::assertIsValidRelationshipObject($relationship, $strict);
25
        }
26 5
    }
27
28
    /**
29
     * Asserts that a relationship object is valid.
30
     *
31
     * @param array     $relationship
32
     * @param boolean   $strict         If true, excludes not safe characters when checking members name
33
     *
34
     * @throws PHPUnit\Framework\ExpectationFailedException
35
     */
36 15
    public static function assertIsValidRelationshipObject($relationship, $strict)
37
    {
38 15
        $expected = ['links', 'data', 'meta'];
39 15
        static::assertContainsAtLeastOneMember($expected, $relationship);
40
41 13
        if (isset($relationship['data'])) {
42 12
            $data = $relationship['data'];
43 12
            static::assertIsValidResourceLinkage($data, $strict);
44
        }
45
46 12
        if (isset($relationship['links'])) {
47 6
            $links = $relationship['links'];
48 6
            $withPagination = isset($relationship['data']) && static::isArrayOfObjects($data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
49 6
            static::assertIsValidRelationshipLinksObject($links, $withPagination, $strict);
50
        }
51
52 10
        if (isset($relationship['meta'])) {
53 3
            static::assertIsValidMetaObject($relationship['meta'], $strict);
0 ignored issues
show
Bug introduced by
The method assertIsValidMetaObject() does not exist on VGirol\JsonApiAssert\Ass...sertRelationshipsObject. Did you maybe mean assertIsValidRelationshipObject()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            static::/** @scrutinizer ignore-call */ 
54
                    assertIsValidMetaObject($relationship['meta'], $strict);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        }
55 9
    }
56
57
    /**
58
     * Asserts that a link object extracted from a relationship object is valid.
59
     *
60
     * @param array     $data
61
     * @param boolean   $withPagination
62
     * @param boolean   $strict         If true, excludes not safe characters when checking members name
63
     *
64
     * @throws PHPUnit\Framework\ExpectationFailedException
65
     */
66 10
    public static function assertIsValidRelationshipLinksObject($data, $withPagination, $strict)
67
    {
68 10
        $allowed = ['self', 'related'];
69 10
        if ($withPagination) {
70 2
            $allowed = array_merge($allowed, ['first', 'last', 'next', 'prev']);
71
        }
72 10
        static::assertIsValidLinksObject($data, $allowed, $strict);
73 6
    }
74
75
    /**
76
     * Asserts that a resource linkage object is valid.
77
     *
78
     * @param array     $data
79
     * @param boolean   $strict     If true, excludes not safe characters when checking members name
80
     *
81
     * @throws PHPUnit\Framework\ExpectationFailedException
82
     */
83 20
    public static function assertIsValidResourceLinkage($data, $strict)
84
    {
85
        try {
86 20
            PHPUnit::assertIsArray(
87 20
                $data,
88 20
                Messages::RESOURCE_LINKAGE_NOT_ARRAY
89
            );
90 18
            if (empty($data)) {
91 18
                return;
92
            }
93 2
        } catch (ExpectationFailedException $e) {
94 2
            PHPUnit::assertNull(
95 2
                $data,
96 2
                Messages::RESOURCE_LINKAGE_NOT_ARRAY
97
            );
98 1
            return;
99
        }
100
101 16
        if (static::isArrayOfObjects($data)) {
102 3
            foreach ($data as $resource) {
103 3
                static::assertIsValidResourceIdentifierObject($resource, $strict);
0 ignored issues
show
Bug introduced by
The method assertIsValidResourceIdentifierObject() does not exist on VGirol\JsonApiAssert\Ass...sertRelationshipsObject. Did you maybe mean assertIsValidResourceLinkage()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
                static::/** @scrutinizer ignore-call */ 
104
                        assertIsValidResourceIdentifierObject($resource, $strict);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
            }
105
        } else {
106 13
            static::assertIsValidResourceIdentifierObject($data, $strict);
107
        }
108 12
    }
109
}
110