Completed
Push — master ( 4e0ac6...0fdc9b )
by Łukasz
25:36
created

providerForPassTroughMethods()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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