|
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 the string value is end with the expected string. |
|
17
|
|
|
* |
|
18
|
|
|
* <code> |
|
19
|
|
|
* $matcher = new ToEndWith('foo'); |
|
20
|
|
|
* $matcher->match('barfoo'); //return true |
|
21
|
|
|
* |
|
22
|
|
|
* $matcher->match('foobar'); //return false |
|
23
|
|
|
* <code> |
|
24
|
|
|
* |
|
25
|
|
|
* @author Noritaka Horio <[email protected]> |
|
26
|
|
|
* @copyright Noritaka Horio <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
final class ToEndWith implements ReportableMatcher |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $actual; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $pattern; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $expected pattern keyword |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($expected) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->pattern = preg_quote($expected, '/'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function match($actual) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->actual = $actual; |
|
54
|
|
|
$patternMatcher = new PatternMatcher("/{$this->pattern}$/"); |
|
55
|
|
|
|
|
56
|
|
|
return $patternMatcher->match($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 end with ') |
|
67
|
|
|
->appendValue($this->pattern); |
|
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 end with ') |
|
78
|
|
|
->appendValue($this->pattern); |
|
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.