Completed
Push — EZP-31044-site-access-provider ( d98e47...f0d948 )
by
unknown
15:11
created

testIsDefinedForExistingSiteAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
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
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Test\Provider;
10
11
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
13
use PHPUnit\Framework\TestCase;
14
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Provider\ChainSiteAccessProvider;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Provider\StaticSiteAccessProvider;
16
17
final class ChainSiteAccessProviderTest extends TestCase
18
{
19
    private const EXISTING_SA_NAME = 'existing_sa';
20
    private const UNDEFINED_SA_NAME = 'undefined_sa';
21
22
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] */
23
    private $providers;
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
        $this->providers = [
29
            new StaticSiteAccessProvider([self::EXISTING_SA_NAME, 'first_sa']),
30
            new StaticSiteAccessProvider(['second_sa']),
31
        ];
32
    }
33
34
    public function testIsDefinedForExistingSiteAccess(): void
35
    {
36
        $chainSiteAccessProvider = $this->getChainSiteAccessProvider();
37
38
        $this->assertTrue($chainSiteAccessProvider->isDefined(self::EXISTING_SA_NAME));
39
    }
40
41
    public function testIsDefinedForUndefinedSiteAccess(): void
42
    {
43
        $chainSiteAccessProvider = $this->getChainSiteAccessProvider();
44
45
        $this->assertFalse($chainSiteAccessProvider->isDefined(self::UNDEFINED_SA_NAME));
46
    }
47
48
    public function testGetSiteAccesses(): void
49
    {
50
        $chainSiteAccessProvider = $this->getChainSiteAccessProvider();
51
        $siteAccesses = iterator_to_array($chainSiteAccessProvider->getSiteAccesses());
52
53
        $this->assertCount(3, $siteAccesses);
54
55
        $expectedSiteAccessNames = [self::EXISTING_SA_NAME, 'first_sa', 'second_sa'];
56
57
        foreach ($expectedSiteAccessNames as $key => $expectedSiteAccessName) {
58
            $expectedSiteAccess = new SiteAccess(
59
                $expectedSiteAccessName,
60
                null,
61
                null,
62
                StaticSiteAccessProvider::class
63
            );
64
            $this->assertEquals($expectedSiteAccess, $siteAccesses[$key]);
65
        }
66
67
        $this->assertNotContains(
68
            new SiteAccess(
69
                self::UNDEFINED_SA_NAME,
70
                null,
71
                null,
72
                StaticSiteAccessProvider::class
73
            ),
74
            $siteAccesses
75
        );
76
    }
77
78
    public function testGetExistingSiteAccess(): void
79
    {
80
        $chainSiteAccessProvider = $this->getChainSiteAccessProvider();
81
        $this->assertEquals(
82
            new SiteAccess(
83
                self::EXISTING_SA_NAME,
84
                null,
85
                null,
86
                StaticSiteAccessProvider::class
87
            ),
88
            $chainSiteAccessProvider->getSiteAccess(self::EXISTING_SA_NAME)
89
        );
90
    }
91
92
    public function testGetUndefinedSiteAccess(): void
93
    {
94
        $chainSiteAccessProvider = $this->getChainSiteAccessProvider();
95
96
        $this->expectException(NotFoundException::class);
97
        $this->expectExceptionMessage("Could not find 'Site Access' with identifier 'undefined_sa'");
98
99
        $chainSiteAccessProvider->getSiteAccess(self::UNDEFINED_SA_NAME);
100
    }
101
102
    private function getChainSiteAccessProvider(): ChainSiteAccessProvider
103
    {
104
        return new ChainSiteAccessProvider($this->providers);
105
    }
106
}
107