Completed
Push — master ( afc521...1cce3a )
by Vincent
03:19
created

AssertRelationshipsObject::assertIsValidResourceLinkage()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 2
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 5
rs 9.4222
c 0
b 0
f 0
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
6
trait AssertRelationshipsObject
7
{
8
    /**
9
     * Asserts that a json fragment is a valid relationships object.
10
     *
11
     * @param array     $json
12
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
13
     *
14
     * @throws PHPUnit\Framework\ExpectationFailedException
15
     */
16
    public static function assertIsValidRelationshipsObject($json, $strict)
17
    {
18 9
        static::assertIsNotArrayOfObjects($json);
19
20 9
        foreach ($json as $key => $relationship) {
21
            static::assertIsValidMemberName($key, $strict);
22 8
            static::assertIsValidRelationshipObject($relationship, $strict);
23 8
        }
24 6
    }
25
26 5
    /**
27
     * Asserts that a json fragment is a valid relationship object.
28
     *
29
     * @param array     $json
30
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
31
     *
32
     * @throws PHPUnit\Framework\ExpectationFailedException
33
     */
34
    public static function assertIsValidRelationshipObject($json, $strict)
35
    {
36 15
        $expected = ['links', 'data', 'meta'];
37
        static::assertContainsAtLeastOneMember($expected, $json);
38 15
39 15
        if (isset($json['data'])) {
40
            $data = $json['data'];
41 13
            static::assertIsValidResourceLinkage($data, $strict);
42 12
        }
43 12
44
        if (isset($json['links'])) {
45
            $links = $json['links'];
46 12
            $withPagination = isset($json['data']) && static::isArrayOfObjects($json['data']);
47 6
            static::assertIsValidRelationshipLinksObject($links, $withPagination, $strict);
48 6
        }
49 6
50
        if (isset($json['meta'])) {
51
            static::assertIsValidMetaObject($json['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

51
            static::/** @scrutinizer ignore-call */ 
52
                    assertIsValidMetaObject($json['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...
52 10
        }
53 3
    }
54
55 9
    /**
56
     * Asserts that a json fragment is a valid link object extracted from a relationship object.
57
     *
58
     * @param array     $json
59
     * @param boolean   $withPagination
60
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
61
     *
62
     * @throws PHPUnit\Framework\ExpectationFailedException
63
     */
64
    public static function assertIsValidRelationshipLinksObject($json, $withPagination, $strict)
65
    {
66 10
        $allowed = ['self', 'related'];
67
        if ($withPagination) {
68 10
            $allowed = array_merge($allowed, ['first', 'last', 'next', 'prev']);
69 10
        }
70 2
        static::assertIsValidLinksObject($json, $allowed, $strict);
71
    }
72
}
73