Completed
Push — EZP-31287 ( fe8c5c...af9523 )
by
unknown
34:01
created

generateRepositoryMockForSectionIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 30
loc 30
rs 9.44
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
namespace eZ\Publish\Core\MVC\Symfony\Matcher\Tests\ContentBased\Identifier;
8
9
use eZ\Publish\API\Repository\SectionService;
10
use eZ\Publish\API\Repository\Values\Content\Section;
11
use eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section as SectionIdentifierMatcher;
12
use eZ\Publish\Core\MVC\Symfony\Matcher\Tests\ContentBased\BaseTest;
13
use eZ\Publish\API\Repository\Repository;
14
15
class SectionTest extends BaseTest
16
{
17
    /** @var \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section */
18
    private $matcher;
19
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
        $this->matcher = new SectionIdentifierMatcher();
24
    }
25
26
    /**
27
     * Returns a Repository mock configured to return the appropriate Section object with given section identifier.
28
     *
29
     * @param string $sectionIdentifier
30
     *
31
     * @return \PHPUnit\Framework\MockObject\MockObject
32
     */
33 View Code Duplication
    private function generateRepositoryMockForSectionIdentifier($sectionIdentifier)
34
    {
35
        $sectionServiceMock = $this->createMock(SectionService::class);
36
        $sectionServiceMock->expects($this->once())
37
            ->method('loadSection')
38
            ->will(
39
                $this->returnValue(
40
                    $this
41
                        ->getMockBuilder(Section::class)
42
                        ->setConstructorArgs(
43
                            [
44
                                ['identifier' => $sectionIdentifier],
45
                            ]
46
                        )
47
                        ->getMockForAbstractClass()
48
                )
49
            );
50
51
        $repository = $this->getRepositoryMock();
52
        $repository
53
            ->expects($this->once())
54
            ->method('getSectionService')
55
            ->will($this->returnValue($sectionServiceMock));
56
        $repository
57
            ->expects($this->any())
58
            ->method('getPermissionResolver')
59
            ->will($this->returnValue($this->getPermissionResolverMock()));
60
61
        return $repository;
62
    }
63
64
    /**
65
     * @dataProvider matchSectionProvider
66
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section::matchLocation
67
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig
68
     * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository
69
     *
70
     * @param string|string[] $matchingConfig
71
     * @param \eZ\Publish\API\Repository\Repository $repository
72
     * @param bool $expectedResult
73
     */
74
    public function testMatchLocation($matchingConfig, Repository $repository, $expectedResult)
75
    {
76
        $this->matcher->setRepository($repository);
77
        $this->matcher->setMatchingConfig($matchingConfig);
78
        $location = $this->getLocationMock();
79
        $location
80
            ->expects($this->once())
81
            ->method('getContentInfo')
82
            ->will(
83
                $this->returnValue(
84
                    $this->getContentInfoMock()
85
                )
86
            );
87
        $this->assertSame(
88
            $expectedResult,
89
            $this->matcher->matchLocation($location)
90
        );
91
    }
92
93
    public function matchSectionProvider()
94
    {
95
        return [
96
            [
97
                'foo',
98
                $this->generateRepositoryMockForSectionIdentifier('foo'),
99
                true,
100
            ],
101
            [
102
                'foo',
103
                $this->generateRepositoryMockForSectionIdentifier('bar'),
104
                false,
105
            ],
106
            [
107
                ['foo', 'baz'],
108
                $this->generateRepositoryMockForSectionIdentifier('bar'),
109
                false,
110
            ],
111
            [
112
                ['foo', 'baz'],
113
                $this->generateRepositoryMockForSectionIdentifier('baz'),
114
                true,
115
            ],
116
        ];
117
    }
118
119
    /**
120
     * @dataProvider matchSectionProvider
121
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\Identifier\Section::matchContentInfo
122
     * @covers \eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued::setMatchingConfig
123
     * @covers \eZ\Publish\Core\MVC\RepositoryAware::setRepository
124
     *
125
     * @param string|string[] $matchingConfig
126
     * @param \eZ\Publish\API\Repository\Repository $repository
127
     * @param bool $expectedResult
128
     */
129 View Code Duplication
    public function testMatchContentInfo($matchingConfig, Repository $repository, $expectedResult)
130
    {
131
        $this->matcher->setRepository($repository);
132
        $this->matcher->setMatchingConfig($matchingConfig);
133
        $this->assertSame(
134
            $expectedResult,
135
            $this->matcher->matchContentInfo($this->getContentInfoMock())
136
        );
137
    }
138
}
139