1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
|
|
|
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Object\Tests; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Kernel\Tests\AbstractTest; |
41
|
|
|
use Apparat\Object\Domain\Model\Object\Id; |
42
|
|
|
use Apparat\Object\Domain\Model\Object\Revision; |
43
|
|
|
use Apparat\Object\Domain\Model\Object\Type; |
44
|
|
|
use Apparat\Object\Domain\Model\Path\ApparatUrl; |
45
|
|
|
use Apparat\Object\Domain\Model\Path\LocalPath; |
46
|
|
|
use Apparat\Object\Domain\Model\Path\ObjectUrl; |
47
|
|
|
use Apparat\Object\Domain\Model\Path\Url; |
48
|
|
|
use Apparat\Object\Domain\Repository\Service; |
49
|
|
|
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy; |
50
|
|
|
use Apparat\Object\Ports\Repository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Object URL tests |
54
|
|
|
* |
55
|
|
|
* @package Apparat\Object |
56
|
|
|
* @subpackage ApparatTest |
57
|
|
|
*/ |
58
|
|
|
class UrlTest extends AbstractTest |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* Example query fragment |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
const QUERY_FRAGMENT = '?param=value#fragment'; |
66
|
|
|
/** |
67
|
|
|
* Repository URL |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
const REPOSITORY_URL = '/repo'; |
72
|
|
|
/** |
73
|
|
|
* Example path |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
const PATH = '/2015/10/01/36704.event/36704-1'; |
78
|
|
|
/** |
79
|
|
|
* Example URL |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
const URL = self::REPOSITORY_URL.self::PATH.self::QUERY_FRAGMENT; |
84
|
|
|
/** |
85
|
|
|
* Example URL |
86
|
|
|
* |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
const REMOTE_URL = 'http://apparat:[email protected]:80'.self::PATH.self::QUERY_FRAGMENT; |
90
|
|
|
/** |
91
|
|
|
* Example apparat URL |
92
|
|
|
* |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
const APPARAT_URL = 'aprts://apparat:[email protected]:80'.self::PATH.self::QUERY_FRAGMENT; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Setup |
99
|
|
|
*/ |
100
|
|
|
public static function setUpBeforeClass() |
101
|
|
|
{ |
102
|
|
|
// Disable repository auto-connection |
103
|
|
|
Kernel::create(Service::class)->useAutoConnect(false); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Test an URL |
108
|
|
|
* |
109
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
110
|
|
|
* @expectedExceptionCode 1451515385 |
111
|
|
|
*/ |
112
|
|
|
public function testInvalidRemoteUrl() |
113
|
|
|
{ |
114
|
|
|
new ObjectUrl(self::REMOTE_URL); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test an URL |
119
|
|
|
*/ |
120
|
|
|
public function testRemoteUrl() |
121
|
|
|
{ |
122
|
|
|
$url = new ObjectUrl(self::REMOTE_URL, true); |
123
|
|
|
$this->assertInstanceOf(ObjectUrl::class, $url); |
124
|
|
|
$this->assertEquals(self::REMOTE_URL, strval($url)); |
125
|
|
|
$this->assertEquals('http', $url->getScheme()); |
126
|
|
|
$this->assertEquals('apparat', $url->getUser()); |
127
|
|
|
$this->assertEquals('tools', $url->getPassword()); |
128
|
|
|
$this->assertEquals('apparat.tools', $url->getHost()); |
129
|
|
|
$this->assertEquals(80, $url->getPort()); |
130
|
|
|
$this->assertEquals('', $url->getPath()); |
131
|
|
|
$this->assertEquals(['param' => 'value'], $url->getQuery()); |
132
|
|
|
$this->assertEquals('fragment', $url->getFragment()); |
133
|
|
|
$this->assertInstanceOf(\DateTimeImmutable::class, $url->getCreationDate()); |
134
|
|
|
$this->assertEquals('2015-10-01', $url->getCreationDate()->format('Y-m-d')); |
135
|
|
|
$this->assertInstanceOf(Id::class, $url->getId()); |
136
|
|
|
$this->assertEquals(new Id(36704), $url->getId()); |
137
|
|
|
$this->assertInstanceOf(Type::class, $url->getType()); |
138
|
|
|
$this->assertEquals(new Type('event'), $url->getType()); |
139
|
|
|
$this->assertInstanceOf(Revision::class, $url->getRevision()); |
140
|
|
|
$this->assertEquals(new Revision(1), $url->getRevision()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Test a local URL with path prefix |
145
|
|
|
*/ |
146
|
|
|
public function testLeadedLocalUrl() |
147
|
|
|
{ |
148
|
|
|
$pathPrefix = '/prefix/path'; |
149
|
|
|
$url = new ObjectUrl($pathPrefix.self::PATH); |
150
|
|
|
$this->assertEquals($pathPrefix, $url->getPath()); |
151
|
|
|
$this->assertEquals(self::PATH, $url->getLocalPath()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Test an invalid URL |
156
|
|
|
* |
157
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
158
|
|
|
* @expectedExceptionCode 1449873819 |
159
|
|
|
*/ |
160
|
|
|
public function testInvalidUrl() |
161
|
|
|
{ |
162
|
|
|
new ObjectUrl('invalid://'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Test an invalid URL path |
167
|
|
|
* |
168
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
169
|
|
|
* @expectedExceptionCode 1449874494 |
170
|
|
|
*/ |
171
|
|
|
public function testInvalidUrlPath() |
172
|
|
|
{ |
173
|
|
|
new ObjectUrl('http://invalid~url*path', true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Test the scheme setter |
178
|
|
|
* |
179
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
180
|
|
|
* @expectedExceptionCode 1449924914 |
181
|
|
|
*/ |
182
|
|
|
public function testUrlSchemeSetter() |
183
|
|
|
{ |
184
|
|
|
$url = new ObjectUrl(self::URL); |
185
|
|
|
$this->assertEquals(ObjectUrl::SCHEME_HTTPS, $url->setScheme(ObjectUrl::SCHEME_HTTPS)->getScheme()); |
186
|
|
|
$url->setScheme('invalid'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Test the host setter |
191
|
|
|
* |
192
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
193
|
|
|
* @expectedExceptionCode 1449925567 |
194
|
|
|
*/ |
195
|
|
|
public function testUrlHostSetter() |
196
|
|
|
{ |
197
|
|
|
$url = new ObjectUrl(self::URL); |
198
|
|
|
$this->assertEquals('apparat.com', $url->setHost('apparat.com')->getHost()); |
199
|
|
|
$url->setHost('_'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Test the port setter |
204
|
|
|
* |
205
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
206
|
|
|
* @expectedExceptionCode 1449925885 |
207
|
|
|
*/ |
208
|
|
|
public function testUrlPortSetter() |
209
|
|
|
{ |
210
|
|
|
$url = new ObjectUrl(self::URL); |
211
|
|
|
$this->assertEquals(443, $url->setPort(443)->getPort()); |
212
|
|
|
$url->setPort(123456789); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Test the remaining setter methods |
217
|
|
|
*/ |
218
|
|
|
public function testUrlSetters() |
219
|
|
|
{ |
220
|
|
|
$url = new ObjectUrl(self::URL); |
221
|
|
|
$this->assertEquals('test', $url->setUser('test')->getUser()); |
222
|
|
|
$this->assertEquals(null, $url->setUser(null)->getUser()); |
223
|
|
|
$this->assertEquals('password', $url->setPassword('password')->getPassword()); |
224
|
|
|
$this->assertEquals(null, $url->setPassword(null)->getPassword()); |
225
|
|
|
$this->assertEquals('/path/prefix', $url->setPath('/path/prefix')->getPath()); |
226
|
|
|
$this->assertEquals(['param2' => 'value2'], $url->setQuery(['param2' => 'value2'])->getQuery()); |
227
|
|
|
$this->assertEquals('fragment2', $url->setFragment('fragment2')->getFragment()); |
228
|
|
|
|
229
|
|
|
$this->assertEquals( |
230
|
|
|
'2016-01-01', |
231
|
|
|
$url->setCreationDate(new \DateTimeImmutable('@1451606400'))->getCreationDate()->format('Y-m-d') |
232
|
|
|
); |
233
|
|
|
$this->assertEquals(123, $url->setId(new Id(123))->getId()->getId()); |
|
|
|
|
234
|
|
|
$this->assertEquals('article', $url->setType(new Type('article'))->getType()->getType()); |
235
|
|
|
$this->assertEquals( |
236
|
|
|
Revision::CURRENT, |
237
|
|
|
$url->setRevision(new Revision(Revision::CURRENT))->getRevision()->getRevision() |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Test the override functionality when getting the URL path |
243
|
|
|
*/ |
244
|
|
|
public function testUrlPathOverride() |
245
|
|
|
{ |
246
|
|
|
$url = new TestObjectUrl(self::URL); |
247
|
|
|
$this->assertEquals( |
248
|
|
|
'https://user:[email protected]:443/path/prefix/2015/10/01/36704.event/36704-2?param2=value2#fragment2', |
249
|
|
|
$url->getUrlOverride() |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Test absolute URL |
255
|
|
|
*/ |
256
|
|
|
public function testUrlAbsolute() |
257
|
|
|
{ |
258
|
|
|
$url = new ObjectUrl(self::REMOTE_URL, true); |
259
|
|
|
$this->assertEquals(true, $url->isAbsolute()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Test absolute URL |
264
|
|
|
*/ |
265
|
|
|
public function testUrlAbsoluteLocal() |
266
|
|
|
{ |
267
|
|
|
$url = new ObjectUrl(rtrim(getenv('APPARAT_BASE_URL'), '/').self::REPOSITORY_URL.self::PATH, true); |
268
|
|
|
$this->assertTrue($url->isAbsoluteLocal()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Test relative URL |
273
|
|
|
*/ |
274
|
|
|
public function testUrlReative() |
275
|
|
|
{ |
276
|
|
|
$url = new ObjectUrl(self::PATH.self::QUERY_FRAGMENT); |
277
|
|
|
$this->assertEquals(false, $url->isAbsolute()); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Test URL comparison |
282
|
|
|
*/ |
283
|
|
|
public function testUrlComparison() |
284
|
|
|
{ |
285
|
|
|
$this->assertFalse((new Url('http://example.com'))->matches(new Url('https://example.com'))); |
286
|
|
|
$this->assertFalse((new Url('http://[email protected]'))->matches(new Url('http://[email protected]'))); |
287
|
|
|
$this->assertFalse( |
288
|
|
|
(new Url('http://user:[email protected]'))->matches(new Url('http://user:[email protected]')) |
289
|
|
|
); |
290
|
|
|
$this->assertFalse((new Url('http://example1.com'))->matches(new Url('http://example2.com'))); |
291
|
|
|
$this->assertFalse((new Url('http://example.com:80'))->matches(new Url('http://example.com:443'))); |
292
|
|
|
$this->assertFalse((new Url('http://example.com/a'))->matches(new Url('http://example.com/b'))); |
293
|
|
|
$this->assertFalse((new Url('http://example.com/?a=1'))->matches(new Url('http://example.com/?a=2'))); |
294
|
|
|
$this->assertFalse((new Url('http://example.com/#a'))->matches(new Url('http://example.com/#b'))); |
295
|
|
|
$this->assertTrue((new Url(self::REMOTE_URL))->matches(new Url(self::REMOTE_URL))); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Test object URL comparison |
300
|
|
|
*/ |
301
|
|
|
public function testObjectUrlComparison() |
302
|
|
|
{ |
303
|
|
|
$this->assertFalse( |
304
|
|
|
( |
305
|
|
|
new ObjectUrl( |
306
|
|
|
'http://example.com/2015/10/01/36704.event/36704-1', |
307
|
|
|
true |
308
|
|
|
) |
309
|
|
|
)->matches(new ObjectUrl('https://example.com/2015/10/01/36704.event/36704-1', true)) |
310
|
|
|
); |
311
|
|
|
$this->assertFalse( |
312
|
|
|
( |
313
|
|
|
new ObjectUrl( |
314
|
|
|
'http://example.com/2015/10/01/36704.event/36704-1', |
315
|
|
|
true |
316
|
|
|
) |
317
|
|
|
)->matches(new ObjectUrl('http://example.com/2016/10/01/36704.event/36704-1', true)) |
318
|
|
|
); |
319
|
|
|
$this->assertFalse( |
320
|
|
|
( |
321
|
|
|
new ObjectUrl( |
322
|
|
|
'http://example.com/2015/10/01/36704.event/36704-1', |
323
|
|
|
true |
324
|
|
|
) |
325
|
|
|
)->matches(new ObjectUrl('http://example.com/2015/10/01/36705.event/36705-1', true)) |
326
|
|
|
); |
327
|
|
|
$this->assertFalse( |
328
|
|
|
( |
329
|
|
|
new ObjectUrl( |
330
|
|
|
'http://example.com/2015/10/01/36704.event/36704-1', |
331
|
|
|
true |
332
|
|
|
) |
333
|
|
|
)->matches(new ObjectUrl('http://example.com/2015/10/01/36704.article/36704-1', true)) |
334
|
|
|
); |
335
|
|
|
$this->assertFalse( |
336
|
|
|
( |
337
|
|
|
new ObjectUrl( |
338
|
|
|
'http://example.com/2015/10/01/36704.event/36704-1', |
339
|
|
|
true |
340
|
|
|
) |
341
|
|
|
)->matches(new ObjectUrl('http://example.com/2015/10/01/36704.event/36704-2', true)) |
342
|
|
|
); |
343
|
|
|
$this->assertTrue((new ObjectUrl(self::REMOTE_URL, true))->matches(new ObjectUrl(self::REMOTE_URL, true))); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Test an invalid apparat URL |
348
|
|
|
* |
349
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
350
|
|
|
* @expectedExceptionCode 1451435429 |
351
|
|
|
*/ |
352
|
|
|
public function testInvalidApparatUrl() |
353
|
|
|
{ |
354
|
|
|
new ApparatUrl(self::REMOTE_URL, true); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Test an absolute apparat URL |
359
|
|
|
*/ |
360
|
|
|
public function testAbsoluteApparatUrl() |
361
|
|
|
{ |
362
|
|
|
$apparatUrl = new ApparatUrl(self::APPARAT_URL, true); |
363
|
|
|
$this->assertInstanceOf(ApparatUrl::class, $apparatUrl); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Test an unknown relative apparat URL |
368
|
|
|
* |
369
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\ApparatInvalidArgumentException |
370
|
|
|
* @expectedExceptionCode 1452695654 |
371
|
|
|
*/ |
372
|
|
|
public function testUnknownRelativeApparatUrl() |
373
|
|
|
{ |
374
|
|
|
new ApparatUrl(self::PATH.self::QUERY_FRAGMENT); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Test a relative apparat URL |
379
|
|
|
*/ |
380
|
|
|
public function testRelativeApparatUrl() |
381
|
|
|
{ |
382
|
|
|
Repository::register( |
383
|
|
|
self::REPOSITORY_URL, |
384
|
|
|
[ |
385
|
|
|
'type' => FileAdapterStrategy::TYPE, |
386
|
|
|
'root' => __DIR__, |
387
|
|
|
] |
388
|
|
|
); |
389
|
|
|
$apparatUrl = new ApparatUrl(self::URL); |
390
|
|
|
$this->assertInstanceOf(ApparatUrl::class, $apparatUrl); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Test invalid date precision |
395
|
|
|
* |
396
|
|
|
* @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException |
397
|
|
|
* @expectedExceptionCode 1451514114 |
398
|
|
|
*/ |
399
|
|
|
public function testInvalidDatePrecision() |
400
|
|
|
{ |
401
|
|
|
new LocalPath(self::PATH, -1); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Test arbitrary date precision |
406
|
|
|
*/ |
407
|
|
|
public function testArbitraryDatePrecision() |
408
|
|
|
{ |
409
|
|
|
$path = new LocalPath(self::PATH, true); |
|
|
|
|
410
|
|
|
$this->assertInstanceOf(LocalPath::class, $path); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|