Completed
Push — ezp_30981_content_info_proxy ( 5d5afd...47f956 )
by
unknown
15:40
created

getConfigResolverParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
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
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->provider
48
            ->method('isDefined')
49
            ->with('current')
50
            ->willReturn(true);
51
52
        $this->provider
53
            ->method('getSiteAccess')
54
            ->with('current')
55
            ->willReturn($this->siteAccess);
56
57
        $this->assertSame($this->siteAccess, $this->getSiteAccessService()->getCurrent());
58
    }
59
60 View Code Duplication
    public function testGetCurrentSiteAccessesRelation(): void
61
    {
62
        $this->configResolver
63
            ->method('getParameter')
64
            ->willReturnMap($this->configResolverParameters);
65
66
        $this->provider
67
            ->method('getSiteAccesses')
68
            ->willReturn($this->availableSiteAccesses);
69
70
        $this->assertSame(['current', 'first_sa'], $this->getSiteAccessService()->getSiteAccessesRelation());
71
    }
72
73 View Code Duplication
    public function testGetFirstSiteAccessesRelation(): void
74
    {
75
        $this->configResolver
76
            ->method('getParameter')
77
            ->willReturnMap($this->configResolverParameters);
78
79
        $this->provider
80
            ->method('getSiteAccesses')
81
            ->willReturn($this->availableSiteAccesses);
82
83
        $this->assertSame(
84
            ['current', 'first_sa'],
85
            $this->getSiteAccessService()->getSiteAccessesRelation(new SiteAccess('first_sa'))
86
        );
87
    }
88
89
    private function getSiteAccessService(): SiteAccessService
90
    {
91
        $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...
92
        $siteAccessService->setSiteAccess($this->siteAccess);
93
94
        return $siteAccessService;
95
    }
96
97
    /**
98
     * @param string[] $siteAccessNames
99
     */
100
    private function getAvailableSitAccesses(array $siteAccessNames): ArrayIterator
101
    {
102
        $availableSitAccesses = [];
103
        foreach ($siteAccessNames as $siteAccessName) {
104
            $availableSitAccesses[] = new SiteAccess($siteAccessName);
105
        }
106
107
        return new ArrayIterator($availableSitAccesses);
108
    }
109
110
    private function getConfigResolverParameters(): array
111
    {
112
        return [
113
            ['repository', 'ezsettings', 'current', 'repository_1'],
114
            ['content.tree_root.location_id', 'ezsettings', 'current', 1],
115
            ['repository', 'ezsettings', 'first_sa', 'repository_1'],
116
            ['content.tree_root.location_id', 'ezsettings', 'first_sa', 1],
117
            ['repository', 'ezsettings', 'second_sa', 'repository_1'],
118
            ['content.tree_root.location_id', 'ezsettings', 'second_sa', 2],
119
            ['repository', 'ezsettings', 'default', ''],
120
            ['content.tree_root.location_id', 'ezsettings', 'default', 3],
121
        ];
122
    }
123
}
124