|
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
|
|
|
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\Helper\LanguageResolver|\PHPUnit_Framework_MockObject_MockObject |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $languageHelperMock; |
|
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->languageHelperMock = $this->getMockBuilder(LanguageResolver::class) |
|
51
|
|
|
->disableOriginalConstructor() |
|
52
|
|
|
->getMock(); |
|
53
|
|
|
$serviceClassName = $this->getSiteAccessAwareServiceClassName(); |
|
54
|
|
|
|
|
55
|
|
|
$this->service = new $serviceClassName( |
|
56
|
|
|
$this->innerApiServiceMock, |
|
57
|
|
|
$this->languageHelperMock // Not all services needs or expects this, however once they do it will be there |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function tearDown() |
|
62
|
|
|
{ |
|
63
|
|
|
unset($this->service); |
|
64
|
|
|
unset($this->languageHelperMock); |
|
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
|
|
|
* Test that language aware methods does a language lookup when language is not set. |
|
126
|
|
|
* |
|
127
|
|
|
* @dataProvider providerForLanguagesLookupMethods |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $method |
|
130
|
|
|
* @param array $arguments |
|
131
|
|
|
* @param bool $return |
|
132
|
|
|
* @param int $languageArgumentIndex From 0 and up, so the array index on $arguments. |
|
133
|
|
|
*/ |
|
134
|
|
View Code Duplication |
final public function testForLanguagesLookup($method, array $arguments, $return, $languageArgumentIndex, callable $callback = null) |
|
135
|
|
|
{ |
|
136
|
|
|
$languages = ['eng-GB', 'eng-US']; |
|
137
|
|
|
$arguments[$languageArgumentIndex] = []; |
|
138
|
|
|
|
|
139
|
|
|
$expectedArguments = $this->setLanguagesLookupExpectedArguments($arguments, $languageArgumentIndex, $languages); |
|
140
|
|
|
|
|
141
|
|
|
$this->languageHelperMock |
|
142
|
|
|
->expects($this->once()) |
|
143
|
|
|
->method('getPrioritizedLanguages') |
|
144
|
|
|
->with([]) |
|
145
|
|
|
->willReturn($languages); |
|
146
|
|
|
|
|
147
|
|
|
$this->innerApiServiceMock |
|
148
|
|
|
->expects($this->once()) |
|
149
|
|
|
->method($method) |
|
150
|
|
|
->with(...$expectedArguments) |
|
151
|
|
|
->willReturn($return); |
|
152
|
|
|
|
|
153
|
|
|
if ($callback instanceof Closure) { |
|
154
|
|
|
$callback->bindTo($this, static::class)(true); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$actualReturn = $this->service->$method(...$arguments); |
|
158
|
|
|
|
|
159
|
|
|
if ($return) { |
|
160
|
|
|
$this->assertTrue($actualReturn); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Method to be able to customize the logic for setting expected language argument during {@see testForLanguagesPassTrough()}. |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $arguments |
|
168
|
|
|
* @param int $languageArgumentIndex |
|
169
|
|
|
* @param array $languages |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function setLanguagesPassTroughArguments(array $arguments, $languageArgumentIndex, array $languages) |
|
174
|
|
|
{ |
|
175
|
|
|
$arguments[$languageArgumentIndex] = $languages; |
|
176
|
|
|
|
|
177
|
|
|
return $arguments; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Make sure these methods does nothing more then passing the arguments to inner service. |
|
182
|
|
|
* |
|
183
|
|
|
* @dataProvider providerForLanguagesLookupMethods |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $method |
|
186
|
|
|
* @param array $arguments |
|
187
|
|
|
* @param bool $return |
|
188
|
|
|
* @param int $languageArgumentIndex From 0 and up, so the array index on $arguments. |
|
189
|
|
|
*/ |
|
190
|
|
View Code Duplication |
final public function testForLanguagesPassTrough($method, array $arguments, $return, $languageArgumentIndex, callable $callback = null) |
|
191
|
|
|
{ |
|
192
|
|
|
$languages = ['eng-GB', 'eng-US']; |
|
193
|
|
|
$arguments = $this->setLanguagesPassTroughArguments($arguments, $languageArgumentIndex, $languages); |
|
194
|
|
|
|
|
195
|
|
|
$this->languageHelperMock |
|
196
|
|
|
->expects($this->once()) |
|
197
|
|
|
->method('getPrioritizedLanguages') |
|
198
|
|
|
->with($languages) |
|
199
|
|
|
->willReturn($languages); |
|
200
|
|
|
|
|
201
|
|
|
$this->innerApiServiceMock |
|
202
|
|
|
->expects($this->once()) |
|
203
|
|
|
->method($method) |
|
204
|
|
|
->with(...$arguments) |
|
205
|
|
|
->willReturn($return); |
|
206
|
|
|
|
|
207
|
|
|
if ($callback instanceof Closure) { |
|
208
|
|
|
$callback->bindTo($this, static::class)(false); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$actualReturn = $this->service->$method(...$arguments); |
|
212
|
|
|
|
|
213
|
|
|
if ($return) { |
|
214
|
|
|
$this->assertTrue($actualReturn); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|