Completed
Push — master ( 2430a5...3c523b )
by Joschi
03:35
created

UrlTest::testPSR7methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.4285
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\Object\Domain\Model\Path\ApparatUrl;
40
use Apparat\Object\Domain\Model\Path\LocalPath;
41
use Apparat\Object\Domain\Model\Path\Url;
42
use Apparat\Object\Domain\Repository\Service;
43
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
44
use Apparat\Object\Ports\Repository;
45
46
/**
47
 * Object URL tests
48
 *
49
 * @package Apparat\Object
50
 * @subpackage ApparatTest
51
 */
52
class UrlTest extends AbstractDisabledAutoconnectorTest
53
{
54
    /**
55
     * Example query fragment
56
     *
57
     * @var string
58
     */
59
    const QUERY_FRAGMENT = '?param=value#fragment';
60
    /**
61
     * Repository URL
62
     *
63
     * @var string
64
     */
65
    const REPOSITORY_URL = '/repo';
66
    /**
67
     * Example path
68
     *
69
     * @var string
70
     */
71
    const PATH = '/2015/10/01/36704-event/36704-1';
72
    /**
73
     * Example path (draft mode)
74
     *
75
     * @var string
76
     */
77
    const DRAFT_PATH = '/2015/10/01/36704-event/36704+';
78
    /**
79
     * Example URL
80
     *
81
     * @var string
82
     */
83
    const URL = self::REPOSITORY_URL.self::PATH.self::QUERY_FRAGMENT;
84
    /**
85
     * Example remote repository authority
86
     *
87
     * @var string
88
     */
89
    const REMOTE_REPOSITORY_AUTHORITY = 'apparat:[email protected]:80';
90
    /**
91
     * Example remote repository URL
92
     *
93
     * @var string
94
     */
95
    const REMOTE_REPOSITORY_URL = 'http://'.self::REMOTE_REPOSITORY_AUTHORITY;
96
    /**
97
     * Example remote URL
98
     *
99
     * @var string
100
     */
101
    const REMOTE_URL = self::REMOTE_REPOSITORY_URL.self::PATH.self::QUERY_FRAGMENT;
102
    /**
103
     * Example apparat URL
104
     *
105
     * @var string
106
     */
107
    const APPARAT_URL = 'aprts://apparat:[email protected]:80'.self::PATH.self::QUERY_FRAGMENT;
108
109
    /**
110
     * Test URL comparison
111
     */
112
    public function testUrlComparison()
113
    {
114
        $this->assertFalse((new Url('http://example.com'))->matches(new Url('https://example.com')));
115
        $this->assertFalse((new Url('http://[email protected]'))->matches(new Url('http://[email protected]')));
116
        $this->assertFalse((new Url('http://user:[email protected]'))->matches(
117
            new Url('http://user:[email protected]')
118
        ));
119
        $this->assertFalse((new Url('http://example1.com'))->matches(new Url('http://example2.com')));
120
        $this->assertFalse((new Url('http://example.com:80'))->matches(new Url('http://example.com:443')));
121
        $this->assertFalse((new Url('http://example.com/a'))->matches(new Url('http://example.com/b')));
122
        $this->assertFalse((new Url('http://example.com/?a=1'))->matches(new Url('http://example.com/?a=2')));
123
        $this->assertFalse((new Url('http://example.com/#a'))->matches(new Url('http://example.com/#b')));
124
        $this->assertTrue((new Url(self::REMOTE_URL))->matches(new Url(self::REMOTE_URL)));
125
    }
126
127
    /**
128
     * Test an invalid apparat URL
129
     *
130
     * @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException
131
     * @expectedExceptionCode 1451435429
132
     */
133
    public function testInvalidApparatUrl()
134
    {
135
        new ApparatUrl(self::REMOTE_URL, true);
136
    }
137
138
    /**
139
     * Test an absolute apparat URL
140
     */
141
    public function testAbsoluteApparatUrl()
142
    {
143
        $apparatUrl = new ApparatUrl(self::APPARAT_URL, true);
144
        $this->assertInstanceOf(ApparatUrl::class, $apparatUrl);
145
        $this->assertEquals('https://apparat:[email protected]:80', Service::normalizeRepositoryUrl($apparatUrl));
146
    }
147
148
    /**
149
     * Test an unknown relative apparat URL
150
     *
151
     * @expectedException \Apparat\Object\Domain\Model\Path\ApparatInvalidArgumentException
152
     * @expectedExceptionCode 1452695654
153
     */
154
    public function testUnknownRelativeApparatUrl()
155
    {
156
        new ApparatUrl(self::PATH.self::QUERY_FRAGMENT);
157
    }
158
159
    /**
160
     * Test a relative apparat URL
161
     */
162
    public function testRelativeApparatUrl()
163
    {
164
        Repository::register(
165
            self::REPOSITORY_URL,
166
            [
167
                'type' => FileAdapterStrategy::TYPE,
168
                'root' => __DIR__,
169
            ]
170
        );
171
        $apparatUrl = new ApparatUrl(self::URL);
172
        $this->assertInstanceOf(ApparatUrl::class, $apparatUrl);
173
        $this->assertEquals(self::REPOSITORY_URL, Service::normalizeRepositoryUrl($apparatUrl));
174
    }
175
176
    /**
177
     * Test invalid date precision
178
     *
179
     * @expectedException \Apparat\Object\Domain\Model\Path\InvalidArgumentException
180
     * @expectedExceptionCode 1451514114
181
     */
182
    public function testInvalidDatePrecision()
183
    {
184
        new LocalPath(self::PATH, -1);
185
    }
186
187
    /**
188
     * Test arbitrary date precision
189
     */
190
    public function testArbitraryDatePrecision()
191
    {
192
        $path = new LocalPath(self::PATH, true);
193
        $this->assertInstanceOf(LocalPath::class, $path);
194
    }
195
196
    /**
197
     * Test draft path
198
     */
199
    public function testDraftPath()
200
    {
201
        $path = new LocalPath(self::DRAFT_PATH);
202
        $this->assertInstanceOf(LocalPath::class, $path);
203
        $this->assertTrue($path->getRevision()->isDraft());
204
    }
205
206
    /**
207
     * Test the normalization of an invalid repository URL
208
     *
209
     * @expectedException \Apparat\Object\Domain\Repository\InvalidArgumentException
210
     * @expectedExceptionCode 1453097878
211
     */
212
    public function testInvalidRepositoryUrlNormalization()
213
    {
214
        Service::normalizeRepositoryUrl(new Url(self::REMOTE_REPOSITORY_URL));
215
    }
216
217
    /**
218
     * Test the normalization of a local string repository URL
219
     */
220
    public function testLocalStringUrlNormalization()
221
    {
222
        $this->assertEquals(
223
            self::REPOSITORY_URL.self::PATH,
224
            Service::normalizeRepositoryUrl(getenv('APPARAT_BASE_URL').self::REPOSITORY_URL.self::PATH)
225
        );
226
    }
227
228
    /**
229
     * Test the remaining PSR-7 methods
230
     */
231
    public function testPSR7methods() {
232
        $url = new Url(self::REMOTE_URL);
233
        $this->assertEquals(self::REMOTE_REPOSITORY_AUTHORITY, $url->getAuthority());
234
        $this->assertEquals('apparat:tools', $url->getUserInfo());
235
        $this->assertEquals('https://'.self::REMOTE_REPOSITORY_AUTHORITY.self::PATH.self::QUERY_FRAGMENT, strval($url->withScheme('HTTPS')));
236
        $this->assertEquals('http://[email protected]:80'.self::PATH.self::QUERY_FRAGMENT, strval($url->withUserInfo('test')));
237
        $this->assertEquals('http://apparat:[email protected]:80'.self::PATH.self::QUERY_FRAGMENT, strval($url->withHost('test.com')));
238
        $this->assertEquals('http://apparat:[email protected]:443'.self::PATH.self::QUERY_FRAGMENT, strval($url->withPort(443)));
239
        $this->assertEquals('http://apparat:[email protected]'.self::PATH.self::QUERY_FRAGMENT, strval($url->withPort(null)));
240
        $this->assertEquals('http://apparat:[email protected]:80/test/path'.self::QUERY_FRAGMENT, strval($url->withPath('test/path')));
241
        $this->assertEquals('http://apparat:[email protected]:80'.self::PATH.'?param2=value2#fragment', strval($url->withQuery('param2=value2')));
242
        $this->assertEquals('http://apparat:[email protected]:80'.self::PATH.'?param=value#fragment2', strval($url->withFragment('fragment2')));
243
    }
244
}
245