1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Prophecy\Util; |
4
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
6
|
|
|
|
7
|
|
|
class StringUtilSpec extends ObjectBehavior |
8
|
|
|
{ |
9
|
|
|
function it_generates_proper_string_representation_for_integer() |
10
|
|
|
{ |
11
|
|
|
$this->stringify(42)->shouldReturn('42'); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
function it_generates_proper_string_representation_for_string() |
15
|
|
|
{ |
16
|
|
|
$this->stringify('some string')->shouldReturn('"some string"'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function it_generates_single_line_representation_for_multiline_string() |
20
|
|
|
{ |
21
|
|
|
$this->stringify("some\nstring")->shouldReturn('"some\\nstring"'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function it_generates_proper_string_representation_for_double() |
25
|
|
|
{ |
26
|
|
|
$this->stringify(42.3)->shouldReturn('42.3'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function it_generates_proper_string_representation_for_boolean_true() |
30
|
|
|
{ |
31
|
|
|
$this->stringify(true)->shouldReturn('true'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function it_generates_proper_string_representation_for_boolean_false() |
35
|
|
|
{ |
36
|
|
|
$this->stringify(false)->shouldReturn('false'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function it_generates_proper_string_representation_for_null() |
40
|
|
|
{ |
41
|
|
|
$this->stringify(null)->shouldReturn('null'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function it_generates_proper_string_representation_for_empty_array() |
45
|
|
|
{ |
46
|
|
|
$this->stringify(array())->shouldReturn('[]'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function it_generates_proper_string_representation_for_array() |
50
|
|
|
{ |
51
|
|
|
$this->stringify(array('zet', 42))->shouldReturn('["zet", 42]'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function it_generates_proper_string_representation_for_hash_containing_one_value() |
55
|
|
|
{ |
56
|
|
|
$this->stringify(array('ever' => 'zet'))->shouldReturn('["ever" => "zet"]'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function it_generates_proper_string_representation_for_hash() |
60
|
|
|
{ |
61
|
|
|
$this->stringify(array('ever' => 'zet', 52 => 'hey', 'num' => 42))->shouldReturn( |
62
|
|
|
'["ever" => "zet", 52 => "hey", "num" => 42]' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function it_generates_proper_string_representation_for_resource() |
67
|
|
|
{ |
68
|
|
|
$resource = fopen(__FILE__, 'r'); |
69
|
|
|
$this->stringify($resource)->shouldReturn('stream:'.$resource); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
function it_generates_proper_string_representation_for_object(\stdClass $object) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$objHash = sprintf('%s:%s', |
75
|
|
|
get_class($object->getWrappedObject()), |
76
|
|
|
spl_object_hash($object->getWrappedObject()) |
77
|
|
|
) . " Object (\n 'objectProphecyClosure' => Closure:%s Object (\n 0 => Closure:%s Object\n )\n)"; |
78
|
|
|
|
79
|
|
|
$hashRegexExpr = '[a-f0-9]{32}'; |
80
|
|
|
$this->stringify($object)->shouldMatch(sprintf('/^%s$/', sprintf(preg_quote("$objHash"), $hashRegexExpr, $hashRegexExpr))); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function it_generates_proper_string_representation_for_object_without_exporting(\stdClass $object) |
84
|
|
|
{ |
85
|
|
|
$objHash = sprintf('%s:%s', |
86
|
|
|
get_class($object->getWrappedObject()), |
87
|
|
|
spl_object_hash($object->getWrappedObject()) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->stringify($object, false)->shouldReturn("$objHash"); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.