Completed
Push — 7.5 ( d9f9a2...5b7b2d )
by
unknown
33:54 queued 15:24
created

URLCheckerTest::assertEqualsArrays()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
rs 10
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->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) {
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->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,
0 ignored issues
show
Bug introduced by
It seems like $this->urlService can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Bundle\EzPublishCoreB...LChecker::__construct() does only seem to accept object<eZ\Publish\API\Repository\URLService>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
162
            $this->handlerRegistry
0 ignored issues
show
Bug introduced by
It seems like $this->handlerRegistry can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Bundle\EzPublishCoreB...LChecker::__construct() does only seem to accept object<eZ\Bundle\EzPubli...ndlerRegistryInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
163
        );
164
        $urlChecker->setLogger($this->logger);
0 ignored issues
show
Bug introduced by
It seems like $this->logger can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Psr\Log\LoggerAwareTrait::setLogger() does only seem to accept object<Psr\Log\LoggerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
165
166
        return $urlChecker;
167
    }
168
}
169