|
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 whether a particular instance. |
|
17
|
|
|
* |
|
18
|
|
|
* <code> |
|
19
|
|
|
* $matcher = new ToBeAnInstanceOf('stdClass'); |
|
20
|
|
|
* $matcher->match(new stdClass()); //return true |
|
21
|
|
|
* |
|
22
|
|
|
* $matcher->match(new Exception()); //return false |
|
23
|
|
|
* </code> |
|
24
|
|
|
* |
|
25
|
|
|
* @author Noritaka Horio <[email protected]> |
|
26
|
|
|
* @copyright Noritaka Horio <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class ToBeAnInstanceOf implements ReportableMatcher |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var mixed |
|
32
|
|
|
*/ |
|
33
|
|
|
private $actual; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $expected; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $className; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new matcher. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $expected |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($expected) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->expected = $expected; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function match($actual) |
|
59
|
|
|
{ |
|
60
|
|
|
$result = false; |
|
61
|
|
|
$this->actual = $actual; |
|
62
|
|
|
|
|
63
|
|
|
if (is_object($this->actual)) { |
|
64
|
|
|
$this->className = get_class($this->actual); |
|
65
|
|
|
$result = $this->actual instanceof $this->expected; |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->className = $actual; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function reportFailed(FailedMessage $message) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$message->appendText('Expected ') |
|
79
|
|
|
->appendText($this->className) |
|
80
|
|
|
->appendText(' to be an instance of ') |
|
81
|
|
|
->appendText($this->expected) |
|
82
|
|
|
->appendText("\n\n") |
|
83
|
|
|
->appendText(' expected: ') |
|
84
|
|
|
->appendText($this->expected) |
|
85
|
|
|
->appendText("\n") |
|
86
|
|
|
->appendText(' got: ') |
|
87
|
|
|
->appendText($this->className); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function reportNegativeFailed(FailedMessage $message) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$message->appendText('Expected ') |
|
96
|
|
|
->appendText($this->className) |
|
97
|
|
|
->appendText(' not to be an instance of ') |
|
98
|
|
|
->appendText($this->expected) |
|
99
|
|
|
->appendText("\n\n") |
|
100
|
|
|
->appendText(' expected not: ') |
|
101
|
|
|
->appendText($this->expected) |
|
102
|
|
|
->appendText("\n") |
|
103
|
|
|
->appendText(' got: ') |
|
104
|
|
|
->appendText($this->className); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
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.