Completed
Push — master ( e81671...a22352 )
by André
93:06 queued 73:42
created

URLCheckerTest::testCheck()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 8
Ratio 26.67 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 0
dl 8
loc 30
rs 8.8571
c 0
b 0
f 0
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
62
            $handler
63
                ->expects($this->once())
64
                ->method('validate')
65
                ->willReturnCallback(function (array $urls) use ($scheme, $groups) {
66
                    $this->assertEqualsArrays($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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
97
            $handler
98
                ->expects($this->once())
99
                ->method('validate')
100
                ->willReturnCallback(function (array $urls) use ($scheme, $groups) {
101
                    $this->assertEqualsArrays($groups[$scheme], $urls);
102
                });
103
        }
104
105
        $this->configureUrlHandlerRegistry($handlers);
106
107
        $urlChecker = $this->createUrlChecker();
108
        $urlChecker->check($query);
109
    }
110
111
    protected function assertEqualsArrays(array $expected, array $actual, $message = '')
112
    {
113
        sort($expected);
114
        sort($actual);
115
116
        $this->assertEquals($expected, $actual, $message);
117
    }
118
119
    private function configureUrlHandlerRegistry(array $schemes)
120
    {
121
        $this->handlerRegistry
122
            ->method('supported')
123
            ->willReturnCallback(function ($scheme) use ($schemes) {
124
                return isset($schemes[$scheme]);
125
            });
126
127
        $this->handlerRegistry
128
            ->method('getHandler')
129
            ->willReturnCallback(function ($scheme) use ($schemes) {
130
                return $schemes[$scheme];
131
            });
132
    }
133
134
    private function createSearchResults(array &$urls)
135
    {
136
        $input = array_reduce($urls, 'array_merge', []);
137
138
        shuffle($input);
139
140
        return new SearchResult([
141
            'totalCount' => count($input),
142
            'items' => $input,
143
        ]);
144
    }
145
146
    private function createGroupedUrls(array $schemes, $n = 10)
147
    {
148
        $results = [];
149
150
        foreach ($schemes as $i => $scheme) {
151
            $results[$scheme] = [];
152
            for ($j = 0; $j < $n; ++$j) {
153
                $results[$scheme][] = new URL([
154
                    'id' => $i * 100 + $j,
155
                    'url' => $scheme . '://' . $j,
156
                ]);
157
            }
158
        }
159
160
        return $results;
161
    }
162
163
    /**
164
     * @return \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLChecker
165
     */
166
    private function createUrlChecker()
167
    {
168
        $urlChecker = new URLChecker(
169
            $this->urlService,
170
            $this->handlerRegistry
171
        );
172
        $urlChecker->setLogger($this->logger);
173
174
        return $urlChecker;
175
    }
176
}
177