-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUrlTest.php
158 lines (138 loc) · 4.67 KB
/
UrlTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward\Test\Resolver;
use Magento\Upward\Definition;
use Magento\Upward\DefinitionIterator;
use Magento\Upward\Resolver\Url;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use function BeBat\Verify\verify;
class UrlTest extends TestCase
{
use MockeryPHPUnitIntegration;
/**
* @var DefinitionIterator|Mockery\MockInterface
*/
private $definitionIteratorMock;
/**
* @var Url
*/
private $resolver;
protected function setUp(): void
{
$this->resolver = new Url();
$this->definitionIteratorMock = Mockery::mock(DefinitionIterator::class);
$this->definitionIteratorMock->shouldReceive('get')
->with(Mockery::type('string'), Mockery::type(Definition::class))
->andReturnUsing(function (string $key, Definition $definition) {
$returnValue = $definition->get($key);
return $returnValue instanceof Definition ? $returnValue->toArray() : $returnValue;
});
$this->resolver->setIterator($this->definitionIteratorMock);
}
public function isValidDataProvider()
{
return [
'Valid' => [
'definition' => new Definition([
'baseUrl' => 'https://upward.test',
'query' => ['foo' => 'bar'],
'username' => 'user',
'password' => 'pass',
]),
'expected' => true,
],
'Invalid - Wrong Query Type' => [
'definition' => new Definition([
'baseUrl' => false,
'query' => 'foo=bar',
]),
'expected' => false,
],
'Invalid - Password w/o User' => [
'definition' => new Definition([
'baseUrl' => false,
'password' => 'pass',
]),
'expected' => false,
],
'Invalid - Relative Parts w/o Host' => [
'definition' => new Definition([
'baseUrl' => false,
'username' => 'user',
]),
'expected' => false,
],
];
}
public function testIndicator(): void
{
verify($this->resolver->getIndicator())->is()->sameAs('baseUrl');
}
/**
* @dataProvider isValidDataProvider
*/
public function testIsValid(Definition $definition, bool $expected): void
{
verify($this->resolver->isValid($definition))->is()->sameAs($expected);
}
public function testResolveAbsoluteAllParams(): void
{
$definition = new Definition([
'baseUrl' => 'https://upward.test',
'hostname' => 'new.host',
'protocol' => 'http',
'pathname' => '/path',
'search' => 'foo=bar&dog=cat',
'query' => ['foo' => 'baz'],
'port' => 8080,
'username' => 'user',
'password' => 'pass',
'hash' => '#hash',
]);
verify($this->resolver->resolve($definition))->is()->sameAs(
'http://user:pass@new.host:8080/path?foo=baz&dog=cat#hash'
);
}
public function testResolveAbsoluteWithPathAppend(): void
{
$definition = new Definition([
'baseUrl' => 'https://upward.test/path/',
'pathname' => 'append',
]);
verify($this->resolver->resolve($definition))->is()->sameAs(
'https://upward.test/path/append'
);
}
public function testResolveAbsoluteWithPathSegmentReplace(): void
{
$definition = new Definition([
'baseUrl' => 'https://upward.test/path/segment',
'pathname' => 'replace',
]);
verify($this->resolver->resolve($definition))->is()->sameAs(
'https://upward.test/path/replace'
);
}
public function testResolveRelativeNoParams(): void
{
$definition = new Definition([
'baseUrl' => false,
'pathname' => 'no-slash-path',
]);
verify($this->resolver->resolve($definition))->is()->sameAs(
'/no-slash-path'
);
}
public function testResolveThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$definition must be an instance of Magento\\Upward\\Definition');
$this->resolver->resolve('Not a Definition');
}
}