1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Prophecy\Argument\Token; |
4
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
6
|
|
|
|
7
|
|
|
class ExactValueTokenSpec extends ObjectBehavior |
8
|
|
|
{ |
9
|
|
|
function let() |
10
|
|
|
{ |
11
|
|
|
$this->beConstructedWith(42); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
function it_implements_TokenInterface() |
15
|
|
|
{ |
16
|
|
|
$this->shouldBeAnInstanceOf('Prophecy\Argument\Token\TokenInterface'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function it_is_not_last() |
20
|
|
|
{ |
21
|
|
|
$this->shouldNotBeLast(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function it_holds_value() |
25
|
|
|
{ |
26
|
|
|
$this->getValue()->shouldReturn(42); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function it_scores_10_if_value_is_equal_to_argument() |
30
|
|
|
{ |
31
|
|
|
$this->scoreArgument(42)->shouldReturn(10); |
32
|
|
|
$this->scoreArgument('42')->shouldReturn(10); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
function it_scores_10_if_value_is_an_object_and_equal_to_argument() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$value = new \DateTime(); |
38
|
|
|
$value2 = clone $value; |
39
|
|
|
|
40
|
|
|
$this->beConstructedWith($value); |
41
|
|
|
$this->scoreArgument($value2)->shouldReturn(10); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function it_does_not_scores_if_value_is_not_equal_to_argument() |
45
|
|
|
{ |
46
|
|
|
$this->scoreArgument(50)->shouldReturn(false); |
47
|
|
|
$this->scoreArgument(new \stdClass())->shouldReturn(false); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
function it_does_not_scores_if_value_an_object_and_is_not_equal_to_argument() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$value = new ExactValueTokenFixtureB('ABC'); |
53
|
|
|
$value2 = new ExactValueTokenFixtureB('CBA'); |
54
|
|
|
|
55
|
|
|
$this->beConstructedWith($value); |
56
|
|
|
$this->scoreArgument($value2)->shouldReturn(false); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function it_does_not_scores_if_value_type_and_is_not_equal_to_argument() |
60
|
|
|
{ |
61
|
|
|
$this->beConstructedWith(false); |
62
|
|
|
$this->scoreArgument(0)->shouldReturn(false); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function it_generates_proper_string_representation_for_integer() |
66
|
|
|
{ |
67
|
|
|
$this->beConstructedWith(42); |
68
|
|
|
$this->__toString()->shouldReturn('exact(42)'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function it_generates_proper_string_representation_for_string() |
72
|
|
|
{ |
73
|
|
|
$this->beConstructedWith('some string'); |
74
|
|
|
$this->__toString()->shouldReturn('exact("some string")'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
function it_generates_single_line_representation_for_multiline_string() |
78
|
|
|
{ |
79
|
|
|
$this->beConstructedWith("some\nstring"); |
80
|
|
|
$this->__toString()->shouldReturn('exact("some\\nstring")'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function it_generates_proper_string_representation_for_double() |
84
|
|
|
{ |
85
|
|
|
$this->beConstructedWith(42.3); |
86
|
|
|
$this->__toString()->shouldReturn('exact(42.3)'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function it_generates_proper_string_representation_for_boolean_true() |
90
|
|
|
{ |
91
|
|
|
$this->beConstructedWith(true); |
92
|
|
|
$this->__toString()->shouldReturn('exact(true)'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function it_generates_proper_string_representation_for_boolean_false() |
96
|
|
|
{ |
97
|
|
|
$this->beConstructedWith(false); |
98
|
|
|
$this->__toString()->shouldReturn('exact(false)'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function it_generates_proper_string_representation_for_null() |
102
|
|
|
{ |
103
|
|
|
$this->beConstructedWith(null); |
104
|
|
|
$this->__toString()->shouldReturn('exact(null)'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function it_generates_proper_string_representation_for_empty_array() |
108
|
|
|
{ |
109
|
|
|
$this->beConstructedWith(array()); |
110
|
|
|
$this->__toString()->shouldReturn('exact([])'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function it_generates_proper_string_representation_for_array() |
114
|
|
|
{ |
115
|
|
|
$this->beConstructedWith(array('zet', 42)); |
116
|
|
|
$this->__toString()->shouldReturn('exact(["zet", 42])'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
function it_generates_proper_string_representation_for_resource() |
120
|
|
|
{ |
121
|
|
|
$resource = fopen(__FILE__, 'r'); |
122
|
|
|
$this->beConstructedWith($resource); |
123
|
|
|
$this->__toString()->shouldReturn('exact(stream:'.$resource.')'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
function it_generates_proper_string_representation_for_object(\stdClass $object) |
127
|
|
|
{ |
128
|
|
|
$objHash = sprintf('exact(%s:%s', |
129
|
|
|
get_class($object->getWrappedObject()), |
130
|
|
|
spl_object_hash($object->getWrappedObject()) |
131
|
|
|
) . " Object (\n 'objectProphecyClosure' => Closure:%s Object (\n 0 => Closure:%s Object\n )\n))"; |
132
|
|
|
|
133
|
|
|
$this->beConstructedWith($object); |
134
|
|
|
|
135
|
|
|
$hashRegexExpr = '[a-f0-9]{32}'; |
136
|
|
|
$this->__toString()->shouldMatch(sprintf('/^%s$/', sprintf(preg_quote("$objHash"), $hashRegexExpr, $hashRegexExpr))); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
class ExactValueTokenFixtureA |
141
|
|
|
{ |
142
|
|
|
public $errors; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
class ExactValueTokenFixtureB extends ExactValueTokenFixtureA |
146
|
|
|
{ |
147
|
|
|
public $errors; |
148
|
|
|
public $value = null; |
149
|
|
|
|
150
|
|
|
public function __construct($value) |
151
|
|
|
{ |
152
|
|
|
$this->value = $value; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.