ConstrainArray   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 95
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
B matches() 0 18 6
A count() 0 4 1
B additionalFailureDescription() 0 18 6
A toString() 0 4 1
1
<?php
2
3
namespace CL\PHPUnitExtensions\Constraint;
4
5
use PHPUnit_Framework_Constraint;
6
use PHPUnit_Util_InvalidArgumentHelper;
7
8
/**
9
 * @author    Ivan Kerin <[email protected]>
10
 * @copyright 2014, Clippings Ltd.
11
 * @license   http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class ConstrainArray extends PHPUnit_Framework_Constraint
14
{
15
    /**
16
     * @var PHPUnit_Framework_Constraint[]
17
     */
18
    protected $constraints = array();
19
20
    /**
21
     * @var boolean
22
     */
23
    protected $isStrict = true;
24
25
    /**
26
     * @param PHPUnit_Framework_Constraint[] $constraints
27
     */
28 3
    public function __construct(array $constraints, $isStrict = true)
29
    {
30 3
        parent::__construct();
31
32 3
        foreach ($constraints as $constraint) {
33 2
            if (! ($constraint instanceof PHPUnit_Framework_Constraint)) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_Constraint does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
34 2
                throw PHPUnit_Util_InvalidArgumentHelper::factory(
35 2
                    1,
36 2
                    'array of PHPUnit_Framework_Constraint',
37
                    $constraint
38 2
                );
39
            }
40 2
        }
41
42 2
        if (! is_bool($isStrict)) {
43 1
            throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'bool', $isStrict);
44
        }
45
46 1
        $this->constraints = $constraints;
47 1
        $this->isStrict = $isStrict;
48 1
    }
49
50
    /**
51
     * Evaluates the constraint for parameter $other. Returns true if the
52
     * constraint is met, false otherwise.
53
     *
54
     * @param  mixed   $other
55
     * @return boolean
56
     */
57 7
    protected function matches($other)
58
    {
59 7
        foreach ($this->constraints as $key => $constraint) {
60 7
            if (! array_key_exists($key, $other)) {
61 1
                return false;
62
            }
63
64 6
            if (! $constraint->evaluate($other[$key], '', true)) {
65 2
                return false;
66
            }
67 4
        }
68
69 4
        if ($this->isStrict and array_diff_key($other, $this->constraints)) {
70 1
            return false;
71
        }
72
73 3
        return true;
74
    }
75
76 1
    public function count()
77
    {
78 1
        return count($this->constraints);
79
    }
80
81 4
    public function additionalFailureDescription($other)
82
    {
83 4
        $errors = array();
84
85 4
        foreach ($this->constraints as $key => $constraint) {
86 4
            if (! array_key_exists($key, $other)) {
87 1
                $errors []= sprintf("key '%s' does not exist", $key);
88 4
            } elseif (! $constraint->evaluate($other[$key], '', true)) {
89 2
                $errors []= sprintf("key %s: %s", $key, $constraint->failureDescription($other[$key]));
90 2
            }
91 4
        }
92
93 4
        if ($this->isStrict and ($diff = array_diff_key($other, $this->constraints))) {
94 1
            $errors []= sprintf("Unmached keys: %s", implode(', ', array_keys($diff)));
95 1
        }
96
97 4
        return "Errors:\n".implode("\n", $errors);
98
    }
99
100
    /**
101
     * @return string
102
     */
103 1
    public function toString()
104
    {
105 1
        return 'array matches constraints';
106
    }
107
}
108