|
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
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Verify match the regular expression. |
|
17
|
|
|
* |
|
18
|
|
|
* <code> |
|
19
|
|
|
* $matcher = new ToMatch('/foo/'); |
|
20
|
|
|
* $matcher->match('foobar'); //return true |
|
21
|
|
|
* |
|
22
|
|
|
* $matcher->match('bar'); //return false |
|
23
|
|
|
* <code> |
|
24
|
|
|
* |
|
25
|
|
|
* @author Noritaka Horio <[email protected]> |
|
26
|
|
|
* @copyright Noritaka Horio <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
final class ToMatch implements ReportableMatcher |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $actual; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $expected; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $expected String of a regular expression |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($expected) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->expected = $expected; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function match($actual) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->actual = $actual; |
|
54
|
|
|
$patternMatcher = new PatternMatcher($this->expected); |
|
55
|
|
|
|
|
56
|
|
|
return $patternMatcher->match($this->actual); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function reportFailed(FailedMessage $message) |
|
63
|
|
|
{ |
|
64
|
|
|
$message->appendText('Expected ') |
|
65
|
|
|
->appendValue($this->actual) |
|
66
|
|
|
->appendText(' to match ') |
|
67
|
|
|
->appendText($this->expected); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function reportNegativeFailed(FailedMessage $message) |
|
74
|
|
|
{ |
|
75
|
|
|
$message->appendText('Expected ') |
|
76
|
|
|
->appendValue($this->actual) |
|
77
|
|
|
->appendText(' not to match ') |
|
78
|
|
|
->appendText($this->expected); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.