Completed
Push — siteaccessaware-layer-only ( 394151...f18ba0 )
by André
15:20 queued 03:27
created

AbstractServiceTest::testForLanguagesPassTrough()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 4
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace eZ\Publish\Core\Repository\SiteAccessAware\Tests;
4
5
use eZ\Publish\Core\Repository\Helper\LanguageResolver;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Abstract tests for SiteAccessAware Services.
10
 *
11
 * Implies convention for methods on these services to either:
12
 * - Do nothing, pass-through call and optionally (default:true) return value
13
 * - lookup languages [IF not defined by callee] on one of the arguments given and pass it to next one.
14
 */
15
abstract class AbstractServiceTest extends TestCase
16
{
17
    /**
18
     * Purely to attempt to make tests easier to read.
19
     *
20
     * As language parameter is ignored from providers and replced with values in tests, this is used to mark value of
21
     * language argument instead of either askingproviders to use 0, or a valid language array which would then not be
22
     * used.
23
     */
24
    const LANG_ARG = 0;
25
26
    /**
27
     * @var \object|\PHPUnit_Framework_MockObject_MockObject
28
     */
29
    protected $innerApiServiceMock;
30
31
    /**
32
     * @var object
33
     */
34
    protected $service;
35
36
    /**
37
     * @var \eZ\Publish\Core\Repository\Helper\LanguageResolver|\PHPUnit_Framework_MockObject_MockObject
38
     */
39
    protected $languageHelperMock;
40
41
    abstract public function getAPIServiceClassName(): string;
42
43
    abstract public function getSiteAccessAwareServiceClassName(): string;
44
45
    public function setUp()
46
    {
47
        parent::setUp();
48
        $this->innerApiServiceMock = $this->getMockBuilder($this->getAPIServiceClassName())->getMock();
49
        $this->languageHelperMock = $this->getMockBuilder(LanguageResolver::class)
50
            ->disableOriginalConstructor()
51
            ->getMock();
52
        $serviceClassName = $this->getSiteAccessAwareServiceClassName();
53
54
        $this->service = new $serviceClassName(
55
            $this->innerApiServiceMock,
56
            $this->languageHelperMock // Not all services needs or expects this, however once they do it will be there
57
        );
58
    }
59
60
    protected function tearDown()
61
    {
62
        unset($this->service);
63
        unset($this->languageHelperMock);
64
        unset($this->innerApiServiceMock);
65
        parent::tearDown();
66
    }
67
68
    /**
69
     * @return array See signature on {@link testForPassTrough} for arguments and their type.
70
     */
71
    abstract public function providerForPassTroughMethods(): array;
72
73
    /**
74
     * Make sure these methods does nothing more then passing the arguments to inner service.
75
     *
76
     * Methods tested here are basically those without as languages argument.
77
     *
78
     * @dataProvider providerForPassTroughMethods
79
     *
80
     * @param string $method
81
     * @param array $arguments
82
     * @param bool $return
83
     */
84
    final public function testForPassTrough(string $method, array $arguments, bool $return = true)
85
    {
86
        $this->innerApiServiceMock
87
            ->expects($this->once())
88
            ->method($method)
89
            ->with(...$arguments)
90
            ->willReturn($return);
91
92
        $actualReturn = $this->service->$method(...$arguments);
93
94
        if ($return) {
95
            $this->assertTrue($actualReturn);
96
        } else {
97
            $this->assertNull($actualReturn);
98
        }
99
    }
100
101
    /**
102
     * @return array See signature on {@link testForLanguageLookup} for arguments and their type.
103
     *               NOTE: languages / prioritizedLanguage, can be set to 0, it will be replaced by tests methods.
104
     */
105
    abstract public function providerForLanguagesLookupMethods(): array;
106
107
    /**
108
     * Method to be able to customize the logic for setting language argument.
109
     *
110
     * @param array $languages
111
     *
112
     * @return array
113
     */
114
    protected function getLanguageArgument(array $languages)
115
    {
116
        return $languages;
117
    }
118
119
    /**
120
     * Test that language aware methods does a language lookup when language is not set.
121
     *
122
     * @dataProvider providerForLanguagesLookupMethods
123
     *
124
     * @param string $method
125
     * @param array $arguments
126
     * @param bool $return
127
     * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments.
128
     */
129
    final public function testForLanguagesLookup(string $method, array $arguments, bool $return, int $languageArgumentIndex)
130
    {
131
        $languages = ['eng-GB', 'eng-US'];
132
        $arguments[$languageArgumentIndex] = [];
133
134
        $expectedArguments = $arguments;
135
        $expectedArguments[$languageArgumentIndex] = $this->getLanguageArgument($languages);
136
137
        $this->languageHelperMock
138
            ->expects($this->once())
139
            ->method('getLanguages')
140
            ->with([])
141
            ->willReturn($languages);
142
143
        $this->innerApiServiceMock
144
            ->expects($this->once())
145
            ->method($method)
146
            ->with(...$expectedArguments)
147
            ->willReturn($return);
148
149
        $actualReturn = $this->service->$method(...$arguments);
150
151
        if ($return) {
152
            $this->assertTrue($actualReturn);
153
        }
154
    }
155
156
    /**
157
     * Make sure these methods does nothing more then passing the arguments to inner service.
158
     *
159
     * @dataProvider providerForLanguagesLookupMethods
160
     *
161
     * @param string $method
162
     * @param array $arguments
163
     * @param bool $return
164
     * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments.
165
     */
166
    final public function testForLanguagesPassTrough(string $method, array $arguments, bool $return, int $languageArgumentIndex)
167
    {
168
        $languages = ['eng-GB', 'eng-US'];
169
        $arguments[$languageArgumentIndex] = $this->getLanguageArgument($languages);
170
171
        $this->languageHelperMock
172
            ->expects($this->once())
173
            ->method('getLanguages')
174
            ->with($languages)
175
            ->willReturn($languages);
176
177
        $this->innerApiServiceMock
178
            ->expects($this->once())
179
            ->method($method)
180
            ->with(...$arguments)
181
            ->willReturn($return);
182
183
        $actualReturn = $this->service->$method(...$arguments);
184
185
        if ($return) {
186
            $this->assertTrue($actualReturn);
187
        }
188
    }
189
}
190