|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of expect package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace expect\matcher; |
|
12
|
|
|
|
|
13
|
|
|
use expect\FailedMessage; |
|
14
|
|
|
use expect\Matcher; |
|
15
|
|
|
use expect\matcher\strategy\ArrayInclusionStrategy; |
|
16
|
|
|
use expect\matcher\strategy\StringInclusionStrategy; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Verify whether the value is all included. |
|
20
|
|
|
* |
|
21
|
|
|
* <code> |
|
22
|
|
|
* $matcher = new ToContain([ 'foo' ]); |
|
23
|
|
|
* $matcher->match('foo'); //return true |
|
24
|
|
|
* |
|
25
|
|
|
* $matcher = new ToContain([ 'foo', 'bar' ]); |
|
26
|
|
|
* $matcher->match('foo'); //return false |
|
27
|
|
|
* |
|
28
|
|
|
* $matcher = new ToContain([ 1, 2 ]); |
|
29
|
|
|
* $matcher->match([ 1, 2 ]); //return true |
|
30
|
|
|
* <code> |
|
31
|
|
|
* |
|
32
|
|
|
* @author Noritaka Horio <[email protected]> |
|
33
|
|
|
* @copyright Noritaka Horio <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
final class ToContain implements ReportableMatcher |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $type; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
private $actual; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var \expect\matcher\strategy\InclusionResult |
|
49
|
|
|
*/ |
|
50
|
|
|
private $matchResult; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
private $expectValues; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param strng|array $expected |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($expected) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->expectValues = is_array($expected) ? $expected : [$expected]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function match($actual) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->actual = $actual; |
|
71
|
|
|
|
|
72
|
|
|
$strategy = $this->createStrategy(); |
|
73
|
|
|
$this->matchResult = $strategy->match($this->expectValues); |
|
74
|
|
|
|
|
75
|
|
|
return $this->matchResult->isMatched(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public function reportFailed(FailedMessage $message) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$unmatchResults = $this->matchResult->getUnmatchResults(); |
|
84
|
|
|
|
|
85
|
|
|
$message->appendText('Expected ') |
|
86
|
|
|
->appendText($this->type) |
|
87
|
|
|
->appendText(' to contain ') |
|
88
|
|
|
->appendValues($unmatchResults); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
public function reportNegativeFailed(FailedMessage $message) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$matchResults = $this->matchResult->getMatchResults(); |
|
97
|
|
|
|
|
98
|
|
|
$message->appendText('Expected ') |
|
99
|
|
|
->appendText($this->type) |
|
100
|
|
|
->appendText(' not to contain ') |
|
101
|
|
|
->appendValues($matchResults); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function createStrategy() |
|
105
|
|
|
{ |
|
106
|
|
|
$strategy = null; |
|
107
|
|
|
|
|
108
|
|
|
if (is_string($this->actual)) { |
|
109
|
|
|
$this->type = 'string'; |
|
110
|
|
|
$strategy = new StringInclusionStrategy($this->actual); |
|
111
|
|
|
} elseif (is_array($this->actual)) { |
|
112
|
|
|
$this->type = 'array'; |
|
113
|
|
|
$strategy = new ArrayInclusionStrategy($this->actual); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $strategy; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.