Completed
Push — master ( 92edcc...696b01 )
by Łukasz
27:46 queued 14:09
created

SiteAccessServiceTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 28.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 27
loc 96
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testGetCurrentSiteAccess() 0 4 1
A testGetCurrentSiteAccessesRelation() 12 12 1
A testGetFirstSiteAccessesRelation() 15 15 1
A getSiteAccessService() 0 7 1
A getAvailableSitAccesses() 0 9 2
A getConfigResolverParameters() 0 13 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
 * @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
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Routing\Tests;
10
11
use ArrayIterator;
12
use eZ\Publish\Core\MVC\ConfigResolverInterface;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessService;
16
use PHPUnit\Framework\TestCase;
17
18
class SiteAccessServiceTest extends TestCase
19
{
20
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface|\PHPUnit\Framework\MockObject\MockObject */
21
    private $provider;
22
23
    /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface|\PHPUnit\Framework\MockObject\MockObject */
24
    private $configResolver;
25
26
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */
27
    private $siteAccess;
28
29
    /** @var \ArrayIterator */
30
    private $availableSiteAccesses;
31
32
    /** @var array */
33
    private $configResolverParameters;
34
35
    protected function setUp(): void
36
    {
37
        parent::setUp();
38
        $this->provider = $this->createMock(SiteAccessProviderInterface::class);
39
        $this->configResolver = $this->createMock(ConfigResolverInterface::class);
40
        $this->siteAccess = new SiteAccess('current');
41
        $this->availableSiteAccesses = $this->getAvailableSitAccesses(['current', 'first_sa', 'second_sa', 'default']);
42
        $this->configResolverParameters = $this->getConfigResolverParameters();
43
    }
44
45
    public function testGetCurrentSiteAccess(): void
46
    {
47
        $this->assertSame($this->siteAccess, $this->getSiteAccessService()->getCurrent());
48
    }
49
50 View Code Duplication
    public function testGetCurrentSiteAccessesRelation(): void
51
    {
52
        $this->configResolver
53
            ->method('getParameter')
54
            ->willReturnMap($this->configResolverParameters);
55
56
        $this->provider
57
            ->method('getSiteAccesses')
58
            ->willReturn($this->availableSiteAccesses);
59
60
        $this->assertSame(['current', 'first_sa'], $this->getSiteAccessService()->getSiteAccessesRelation());
61
    }
62
63 View Code Duplication
    public function testGetFirstSiteAccessesRelation(): void
64
    {
65
        $this->configResolver
66
            ->method('getParameter')
67
            ->willReturnMap($this->configResolverParameters);
68
69
        $this->provider
70
            ->method('getSiteAccesses')
71
            ->willReturn($this->availableSiteAccesses);
72
73
        $this->assertSame(
74
            ['current', 'first_sa'],
75
            $this->getSiteAccessService()->getSiteAccessesRelation(new SiteAccess('first_sa'))
76
        );
77
    }
78
79
    private function getSiteAccessService(): SiteAccessService
80
    {
81
        $siteAccessService = new SiteAccessService($this->provider, $this->configResolver);
0 ignored issues
show
Bug introduced by
It seems like $this->provider can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\MVC\Symf...sService::__construct() does only seem to accept object<eZ\Publish\Core\M...ccessProviderInterface>, 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...
Bug introduced by
It seems like $this->configResolver can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\MVC\Symf...sService::__construct() does only seem to accept object<eZ\Publish\Core\M...onfigResolverInterface>, 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...
82
        $siteAccessService->setSiteAccess($this->siteAccess);
83
84
        return $siteAccessService;
85
    }
86
87
    /**
88
     * @param string[] $siteAccessNames
89
     */
90
    private function getAvailableSitAccesses(array $siteAccessNames): ArrayIterator
91
    {
92
        $availableSitAccesses = [];
93
        foreach ($siteAccessNames as $siteAccessName) {
94
            $availableSitAccesses[] = new SiteAccess($siteAccessName);
95
        }
96
97
        return new ArrayIterator($availableSitAccesses);
98
    }
99
100
    private function getConfigResolverParameters(): array
101
    {
102
        return [
103
            ['repository', 'ezsettings', 'current', 'repository_1'],
104
            ['content.tree_root.location_id', 'ezsettings', 'current', 1],
105
            ['repository', 'ezsettings', 'first_sa', 'repository_1'],
106
            ['content.tree_root.location_id', 'ezsettings', 'first_sa', 1],
107
            ['repository', 'ezsettings', 'second_sa', 'repository_1'],
108
            ['content.tree_root.location_id', 'ezsettings', 'second_sa', 2],
109
            ['repository', 'ezsettings', 'default', ''],
110
            ['content.tree_root.location_id', 'ezsettings', 'default', 3],
111
        ];
112
    }
113
}
114