1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Tests\URLChecker; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\URLService; |
10
|
|
|
use eZ\Publish\API\Repository\Values\URL\SearchResult; |
11
|
|
|
use eZ\Publish\API\Repository\Values\URL\URL; |
12
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLQuery; |
13
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct; |
14
|
|
|
use eZ\Bundle\EzPublishCoreBundle\URLChecker\URLChecker; |
15
|
|
|
use eZ\Bundle\EzPublishCoreBundle\URLChecker\URLHandlerInterface; |
16
|
|
|
use eZ\Bundle\EzPublishCoreBundle\URLChecker\URLHandlerRegistryInterface; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
|
20
|
|
|
class URLCheckerTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** @var \eZ\Publish\API\Repository\URLService|\PHPUnit\Framework\MockObject\MockObject */ |
23
|
|
|
private $urlService; |
24
|
|
|
|
25
|
|
|
/** @var \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLHandlerRegistryInterface|\PHPUnit\Framework\MockObject\MockObject */ |
26
|
|
|
private $handlerRegistry; |
27
|
|
|
|
28
|
|
|
/** @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
29
|
|
|
private $logger; |
30
|
|
|
|
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->urlService = $this->createMock(URLService::class); |
34
|
|
|
$this->urlService |
35
|
|
|
->expects($this->any()) |
36
|
|
|
->method('createUpdateStruct') |
37
|
|
|
->willReturnCallback(function () { |
38
|
|
|
return new URLUpdateStruct(); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$this->handlerRegistry = $this->createMock(URLHandlerRegistryInterface::class); |
42
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testCheck() |
46
|
|
|
{ |
47
|
|
|
$query = new URLQuery(); |
48
|
|
|
$groups = $this->createGroupedUrls(['http', 'https']); |
49
|
|
|
|
50
|
|
|
$this->urlService |
51
|
|
|
->expects($this->once()) |
52
|
|
|
->method('findUrls') |
53
|
|
|
->with($query) |
54
|
|
|
->willReturn($this->createSearchResults($groups)); |
55
|
|
|
|
56
|
|
|
$handlers = [ |
57
|
|
|
'http' => $this->createMock(URLHandlerInterface::class), |
58
|
|
|
'https' => $this->createMock(URLHandlerInterface::class), |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
View Code Duplication |
foreach ($handlers as $scheme => $handler) { |
|
|
|
|
62
|
|
|
$handler |
63
|
|
|
->expects($this->once()) |
64
|
|
|
->method('validate') |
65
|
|
|
->willReturnCallback(function (array $urls) use ($scheme, $groups) { |
66
|
|
|
$this->assertEqualsCanonicalizing($groups[$scheme], $urls); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->configureUrlHandlerRegistry($handlers); |
71
|
|
|
|
72
|
|
|
$urlChecker = $this->createUrlChecker(); |
73
|
|
|
$urlChecker->check($query); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testCheckUnsupported() |
77
|
|
|
{ |
78
|
|
|
$query = new URLQuery(); |
79
|
|
|
$groups = $this->createGroupedUrls(['http', 'https'], 10); |
80
|
|
|
|
81
|
|
|
$this->urlService |
82
|
|
|
->expects($this->once()) |
83
|
|
|
->method('findUrls') |
84
|
|
|
->with($query) |
85
|
|
|
->willReturn($this->createSearchResults($groups)); |
86
|
|
|
|
87
|
|
|
$this->logger |
88
|
|
|
->expects($this->atLeastOnce()) |
89
|
|
|
->method('error') |
90
|
|
|
->with('Unsupported URL schema: https'); |
91
|
|
|
|
92
|
|
|
$handlers = [ |
93
|
|
|
'http' => $this->createMock(URLHandlerInterface::class), |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
View Code Duplication |
foreach ($handlers as $scheme => $handler) { |
|
|
|
|
97
|
|
|
$handler |
98
|
|
|
->expects($this->once()) |
99
|
|
|
->method('validate') |
100
|
|
|
->willReturnCallback(function (array $urls) use ($scheme, $groups) { |
101
|
|
|
$this->assertEqualsCanonicalizing($groups[$scheme], $urls); |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->configureUrlHandlerRegistry($handlers); |
106
|
|
|
|
107
|
|
|
$urlChecker = $this->createUrlChecker(); |
108
|
|
|
$urlChecker->check($query); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function configureUrlHandlerRegistry(array $schemes) |
112
|
|
|
{ |
113
|
|
|
$this->handlerRegistry |
114
|
|
|
->method('supported') |
115
|
|
|
->willReturnCallback(function ($scheme) use ($schemes) { |
116
|
|
|
return isset($schemes[$scheme]); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
$this->handlerRegistry |
120
|
|
|
->method('getHandler') |
121
|
|
|
->willReturnCallback(function ($scheme) use ($schemes) { |
122
|
|
|
return $schemes[$scheme]; |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function createSearchResults(array &$urls) |
127
|
|
|
{ |
128
|
|
|
$input = array_reduce($urls, 'array_merge', []); |
129
|
|
|
|
130
|
|
|
shuffle($input); |
131
|
|
|
|
132
|
|
|
return new SearchResult([ |
133
|
|
|
'totalCount' => count($input), |
134
|
|
|
'items' => $input, |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function createGroupedUrls(array $schemes, $n = 10) |
139
|
|
|
{ |
140
|
|
|
$results = []; |
141
|
|
|
|
142
|
|
|
foreach ($schemes as $i => $scheme) { |
143
|
|
|
$results[$scheme] = []; |
144
|
|
|
for ($j = 0; $j < $n; ++$j) { |
145
|
|
|
$results[$scheme][] = new URL([ |
146
|
|
|
'id' => $i * 100 + $j, |
147
|
|
|
'url' => $scheme . '://' . $j, |
148
|
|
|
]); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $results; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLChecker |
157
|
|
|
*/ |
158
|
|
|
private function createUrlChecker() |
159
|
|
|
{ |
160
|
|
|
$urlChecker = new URLChecker( |
161
|
|
|
$this->urlService, |
|
|
|
|
162
|
|
|
$this->handlerRegistry |
|
|
|
|
163
|
|
|
); |
164
|
|
|
$urlChecker->setLogger($this->logger); |
|
|
|
|
165
|
|
|
|
166
|
|
|
return $urlChecker; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.