Completed
Push — EZP-31044-site-access-provider ( 8220ce )
by
unknown
14:19
created

RouterBaseTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 8
loc 66
rs 10
c 0
b 0
f 0
wmc 5
lcom 2
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testConstruct() 0 4 1
A testMatch() 8 8 1
matchProvider() 0 1 ?
createRouter() 0 1 ?
A createSiteAccessProviderMock() 0 24 2
getSiteAccessProviderSettings() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\RouterURITextTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests;
10
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIText;
13
use PHPUnit\Framework\TestCase;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIText as URITextMatcher;
16
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
17
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder;
18
use Psr\Log\LoggerInterface;
19
20
abstract class RouterBaseTest extends TestCase
21
{
22
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder */
23
    protected $matcherBuilder;
24
25
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */
26
    protected $siteAccessProvider;
27
28
    protected function setUp(): void
29
    {
30
        parent::setUp();
31
        $this->matcherBuilder = new MatcherBuilder();
32
        $this->siteAccessProvider = $this->createSiteAccessProviderMock();
33
    }
34
35
    public function testConstruct(): Router
36
    {
37
        return $this->createRouter();
38
    }
39
40
    /**
41
     * @dataProvider matchProvider
42
     */
43 View Code Duplication
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
44
    {
45
        $router = $this->createRouter();
46
        $sa = $router->match($request);
47
        $this->assertInstanceOf(SiteAccess::class, $sa);
48
        $this->assertSame($siteAccess, $sa->name);
49
        $router->setSiteAccess();
50
    }
51
52
    abstract public function matchProvider(): array;
53
54
    abstract protected function createRouter(): Router;
55
56
    private function createSiteAccessProviderMock(): SiteAccess\SiteAccessProviderInterface
57
    {
58
        $isDefinedMap = [];
59
        $getSiteAccessMap = [];
60
        foreach ($this->getSiteAccessProviderSettings() as $sa) {
61
            $isDefinedMap[] = [$sa->name, $sa->isDefined];
62
            $getSiteAccessMap[] = [
63
                $sa->name,
64
                new SiteAccess(
65
                    $sa->name,
66
                    $sa->matchingType
67
                )
68
            ];
69
        }
70
        $siteAccessProviderMock = $this->createMock(SiteAccess\SiteAccessProviderInterface::class);
71
        $siteAccessProviderMock
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
            ->method('isDefined')
73
            ->willReturnMap($isDefinedMap);
74
        $siteAccessProviderMock
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            ->method('getSiteAccess')
76
            ->willReturnMap($getSiteAccessMap);
77
78
        return $siteAccessProviderMock;
79
    }
80
81
    /**
82
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\SiteAccessSetting[]
83
     */
84
    abstract public function getSiteAccessProviderSettings(): array;
85
}
86