1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use GroupByInc\API\Url\UrlReplacement; |
4
|
|
|
use GroupByInc\API\Url\OperationType; |
5
|
|
|
use GroupByInc\API\Url\ParserException; |
6
|
|
|
use GroupByInc\API\Util\StringBuilder; |
7
|
|
|
|
8
|
|
|
class UrlReplacementTest extends PHPUnit_Framework_TestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function testToString() |
12
|
|
|
{ |
13
|
|
|
$replacement = new UrlReplacement(2, "a", OperationType::Swap); |
14
|
|
|
$this->assertEquals("2-a", $replacement); |
15
|
|
|
$replacement = new UrlReplacement(20, "%", OperationType::Insert); |
16
|
|
|
$this->assertEquals("i20-%", $replacement); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testFromString() |
20
|
|
|
{ |
21
|
|
|
UrlReplacement::fromString("2-a"); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testFromStringInvalidString() |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
UrlReplacement::fromString("a2-a"); |
28
|
|
|
$this->fail("Exception not thrown"); |
29
|
|
|
} catch (ParserException $e) { |
30
|
|
|
// expected |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testToStringAndFromStringWithInsert() |
35
|
|
|
{ |
36
|
|
|
$this->assertToAndFromString("i2-/"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testToStringAndFromStringWithSwap() |
40
|
|
|
{ |
41
|
|
|
$this->assertToAndFromString("35-9"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testToStringAndFromStringWithDash() |
45
|
|
|
{ |
46
|
|
|
$this->assertToAndFromString("35--"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testParseQueryString() |
50
|
|
|
{ |
51
|
|
|
$this->assertParseQuery("2-a", "3-b", "4-c"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testParseQueryStringWithInserts() |
55
|
|
|
{ |
56
|
|
|
$this->assertParseQuery("2-a", "i3-b", "4-c"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testParseQueryStringWithDash() |
60
|
|
|
{ |
61
|
|
|
$this->assertParseQuery("2--", "i3-b", "4-c"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testParseQueryStringWithDash2() |
65
|
|
|
{ |
66
|
|
|
$this->assertParseQuery("2--", "i3-b", "4--"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testParseQueryStringWithDashMismatch() |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
UrlReplacement::parseQueryString("2-a-i3-b--4-c"); |
73
|
|
|
$this->fail("Exception not thrown"); |
74
|
|
|
} catch (ParserException $e) { |
75
|
|
|
// expected |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSimpleApplyReplace() |
80
|
|
|
{ |
81
|
|
|
$this->assertApply("avc123", "abc123", new UrlReplacement(1, "b", OperationType::Swap)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testSimpleApplyReplaceAtStart() |
85
|
|
|
{ |
86
|
|
|
$this->assertApply("zbc123", "abc123", new UrlReplacement(0, "a", OperationType::Swap)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testSimpleApplyReplaceAtStartBadIndex() |
90
|
|
|
{ |
91
|
|
|
$this->assertBadApply("zbc123", new UrlReplacement(-1, "a", OperationType::Swap)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testSimpleApplyReplaceAtEnd() |
95
|
|
|
{ |
96
|
|
|
$this->assertApply("abc124", "abc123", new UrlReplacement(5, "3", OperationType::Swap)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testSimpleApplyReplaceAtEndBadIndex() |
100
|
|
|
{ |
101
|
|
|
$this->assertBadApply("abc124", new UrlReplacement(6, "3", OperationType::Swap)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSimpleApplyInsertAtEnd() |
105
|
|
|
{ |
106
|
|
|
$this->assertApply("abc12", "abc123", new UrlReplacement(5, "3", OperationType::Insert)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testSimpleApplyInsertAtStart() |
110
|
|
|
{ |
111
|
|
|
$this->assertApply("bc123", "abc123", new UrlReplacement(0, "a", OperationType::Insert)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testSimpleApplyInsertAtStartBadIndex() |
115
|
|
|
{ |
116
|
|
|
$this->assertBadApply("bc123", new UrlReplacement(-1, "a", OperationType::Insert)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testSimpleApplyInsertAtEndBadIndex() |
120
|
|
|
{ |
121
|
|
|
$this->assertBadApply("abc12", new UrlReplacement(6, "3", OperationType::Insert)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function assertBadApply($input, UrlReplacement $replacement) |
125
|
|
|
{ |
126
|
|
|
$builder = new StringBuilder($input); |
127
|
|
|
$replacement->apply($builder, 0); |
128
|
|
|
$this->assertEquals($input, $builder); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function assertApply($input, $expected, UrlReplacement $replacement) |
132
|
|
|
{ |
133
|
|
|
$builder = new StringBuilder($input); |
134
|
|
|
$replacement->apply($builder, 0); |
135
|
|
|
$this->assertEquals($expected, $builder); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function assertParseQuery() |
139
|
|
|
{ |
140
|
|
|
$builder = new StringBuilder(); |
141
|
|
|
$args = func_get_args(); |
142
|
|
|
/** @var string $replacement */ |
143
|
|
|
foreach ($args as $j => $replacement) { |
144
|
|
|
if ($builder->length() != 0) { |
145
|
|
|
$builder->append("-"); |
146
|
|
|
} |
147
|
|
|
$builder->append($replacement); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$replacements = UrlReplacement::parseQueryString($builder->__toString()); |
151
|
|
|
|
152
|
|
|
$this->assertEquals(strlen($replacement), count($replacements)); |
|
|
|
|
153
|
|
|
|
154
|
|
|
for ($i = 0; $i < count($replacements); $i++) { |
|
|
|
|
155
|
|
|
$this->assertEquals($args[strlen($replacement) - ($i + 1)], $replacements[$i]->__toString()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function assertToAndFromString($replacementString) |
160
|
|
|
{ |
161
|
|
|
$replacement1 = UrlReplacement::fromString($replacementString); |
162
|
|
|
$urlReplacementString = (string)$replacement1; |
163
|
|
|
$replacement2 = UrlReplacement::fromString($urlReplacementString); |
164
|
|
|
$this->assertEquals($replacement1, $replacement2); |
165
|
|
|
$this->assertEquals((string)$replacement2, $replacementString); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.