Completed
Push — master ( efa9e3...a3168e )
by Vincent
12:46 queued 10s
created

PaginationLinksEqualConstraint::allowedMembers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert\Constraint;
6
7
use PHPUnit\Framework\Constraint\Constraint;
8
use VGirol\JsonApiAssert\Constraint\LinkEqualsConstraint;
9
use VGirol\JsonApiAssert\Members;
10
11
/**
12
 * A constraint class to assert that a link object equals an expected value.
13
 */
14
class PaginationLinksEqualConstraint extends Constraint
15
{
16
    /**
17
     * @var array
18
     */
19
    private $expected;
20
21
    /**
22
     * The list of the allowed link names for pagination
23
     *
24
     * @var array
25
     */
26 4
    private $allowedMembers;
27
28 4
    /**
29 4
     * Class constructor.
30
     *
31
     * @param array $expected
32
     */
33
    public function __construct($expected, $allowedMembers)
34 3
    {
35
        $this->expected = $expected;
36 3
        $this->allowedMembers = $allowedMembers;
37 3
    }
38 3
39
    /**
40
     * Returns a string representation of the constraint.
41
     */
42
    public function toString(): string
43
    {
44
        return \sprintf(
45
            'equals %s',
46
            $this->exporter()->export($this->expected)
47
        );
48 4
    }
49
50
    /**
51 4
     * Evaluates the constraint for parameter $other. Returns true if the
52 4
     * constraint is met, false otherwise.
53 4
     *
54
     * @param mixed $other value or object to evaluate
55 4
     */
56
    protected function matches($other): bool
57
    {
58 4
        // Add missing members with false value
59 4
        $cleanExpected = array_merge(
60
            array_fill_keys($this->allowedMembers, false),
61
            $this->expected
62 4
        );
63 4
        asort($cleanExpected);
64 4
65
        // Extract only pagination members from incoming json
66 4
        $cleanJson = array_intersect_key($other, array_flip($this->allowedMembers));
67 4
        asort($cleanJson);
68
69
        // Search for unexpected members
70 4
        $notExpectedMembers = array_keys(
71 1
            array_filter(
72
                $cleanExpected,
73
                function ($value) {
74
                    return $value === false;
75 3
                }
76 3
            )
77
        );
78 3
        if (count(array_intersect_key($cleanJson, array_flip($notExpectedMembers))) !== 0) {
79 3
            return false;
80
        }
81 3
82 1
        // Extracts expected members
83
        $expectedMembers = array_filter(
84
            $cleanExpected,
85
            function ($value) {
86 2
                return $value !== false;
87 2
            }
88
        );
89 2
        if (array_keys($expectedMembers) != array_keys($cleanJson)) {
90 2
            return false;
91
        }
92
93 2
        // Extracts members whose value have to be tested
94 2
        $expectedValues = array_filter(
95 2
            $expectedMembers,
96 1
            function ($value) {
97
                return $value !== true;
98
            }
99
        );
100 1
101
        foreach ($expectedValues as $name => $expectedLink) {
102
            $constraint = new LinkEqualsConstraint($expectedLink);
103
            if ($constraint->check($cleanJson[$name]) === false) {
104
                return false;
105
            }
106
        }
107
108 4
        return true;
109
    }
110
}
111