Completed
Push — ezp_30981_content_info_proxy ( a78a98...0757d2 )
by
unknown
15:21
created

RouterBaseTest::testConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
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\Tests;
10
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use PHPUnit\Framework\TestCase;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Router;
14
use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
15
use eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder;
16
17
abstract class RouterBaseTest extends TestCase
18
{
19
    protected const UNDEFINED_SA_NAME = 'undefined_sa';
20
    protected const ENV_SA_NAME = 'env_sa';
21
    protected const HEADERBASED_SA_NAME = 'headerbased_sa';
22
23
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\MatcherBuilder */
24
    protected $matcherBuilder;
25
26
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */
27
    protected $siteAccessProvider;
28
29
    protected function setUp(): void
30
    {
31
        parent::setUp();
32
        $this->matcherBuilder = new MatcherBuilder();
33
        $this->siteAccessProvider = $this->createSiteAccessProviderMock();
34
    }
35
36
    public function testConstruct(): Router
37
    {
38
        return $this->createRouter();
39
    }
40
41
    /**
42
     * @dataProvider matchProvider
43
     */
44
    public function testMatch(SimplifiedRequest $request, string $siteAccess)
45
    {
46
        $router = $this->createRouter();
47
        $sa = $router->match($request);
48
        $this->assertInstanceOf(SiteAccess::class, $sa);
49
        $this->assertSame($siteAccess, $sa->name);
50
        // SiteAccess must be serializable as a whole. See https://jira.ez.no/browse/EZP-21613
51
        $this->assertIsString(serialize($sa));
52
        $router->setSiteAccess();
53
    }
54
55
    abstract public function matchProvider(): array;
56
57
    abstract protected function createRouter(): Router;
58
59
    private function createSiteAccessProviderMock(): SiteAccess\SiteAccessProviderInterface
60
    {
61
        $isDefinedMap = [];
62
        $getSiteAccessMap = [];
63
        foreach ($this->getSiteAccessProviderSettings() as $sa) {
64
            $isDefinedMap[] = [$sa->name, $sa->isDefined];
65
            $getSiteAccessMap[] = [
66
                $sa->name,
67
                new SiteAccess(
68
                    $sa->name,
69
                    $sa->matchingType
70
                ),
71
            ];
72
        }
73
        $siteAccessProviderMock = $this->createMock(SiteAccess\SiteAccessProviderInterface::class);
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('isDefined')
76
            ->willReturnMap($isDefinedMap);
77
        $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...
78
            ->method('getSiteAccess')
79
            ->willReturnMap($getSiteAccessMap);
80
81
        return $siteAccessProviderMock;
82
    }
83
84
    /**
85
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Tests\SiteAccessSetting[]
86
     */
87
    abstract public function getSiteAccessProviderSettings(): array;
88
}
89