|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Mockery |
|
4
|
|
|
* |
|
5
|
|
|
* LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
8
|
|
|
* with this package in the file LICENSE.txt. |
|
9
|
|
|
* It is also available through the world-wide-web at this URL: |
|
10
|
|
|
* http://github.com/padraic/mockery/blob/master/LICENSE |
|
11
|
|
|
* If you did not receive a copy of the license and are unable to |
|
12
|
|
|
* obtain it through the world-wide-web, please send an email |
|
13
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
14
|
|
|
* |
|
15
|
|
|
* @category Mockery |
|
16
|
|
|
* @package Mockery |
|
17
|
|
|
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com) |
|
18
|
|
|
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Mockery\Matcher; |
|
22
|
|
|
|
|
23
|
|
|
class Subset extends MatcherAbstract |
|
24
|
|
|
{ |
|
25
|
|
|
private $expected; |
|
26
|
|
|
private $strict = true; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $expected Expected subset of data |
|
30
|
|
|
* @param bool $strict Whether to run a strict or loose comparison |
|
31
|
|
|
*/ |
|
32
|
8 |
|
public function __construct(array $expected, $strict = true) |
|
33
|
|
|
{ |
|
34
|
8 |
|
$this->expected = $expected; |
|
35
|
8 |
|
$this->strict = $strict; |
|
36
|
8 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $expected Expected subset of data |
|
40
|
|
|
* |
|
41
|
|
|
* @return Subset |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public static function strict(array $expected) |
|
44
|
|
|
{ |
|
45
|
2 |
|
return new static($expected, true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array $expected Expected subset of data |
|
50
|
|
|
* |
|
51
|
|
|
* @return Subset |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public static function loose(array $expected) |
|
54
|
|
|
{ |
|
55
|
1 |
|
return new static($expected, false); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if the actual value matches the expected. |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $actual |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
7 |
|
public function match(&$actual) |
|
65
|
|
|
{ |
|
66
|
7 |
|
if (!is_array($actual)) { |
|
67
|
1 |
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
if ($this->strict) { |
|
71
|
5 |
|
return $actual === array_replace_recursive($actual, $this->expected); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $actual == array_replace_recursive($actual, $this->expected); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return a string representation of this Matcher |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __toString() |
|
83
|
|
|
{ |
|
84
|
|
|
$return = '<Subset['; |
|
85
|
|
|
$elements = array(); |
|
86
|
|
|
foreach ($this->expected as $k=>$v) { |
|
87
|
|
|
$elements[] = $k . '=' . (string) $v; |
|
88
|
|
|
} |
|
89
|
|
|
$return .= implode(', ', $elements) . ']>'; |
|
90
|
|
|
return $return; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|