1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Tests\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Handler\MockHandler; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException; |
11
|
|
|
use Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
14
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
15
|
|
|
use Symfony\Component\Cache\CacheItem; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
19
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
20
|
|
|
use Symfony\Component\Translation\Translator; |
21
|
|
|
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; |
22
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
23
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
24
|
|
|
|
25
|
|
|
class VersionCheckTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|LegacyTranslatorInterface|TranslatorInterface */ |
28
|
|
|
private $translator; |
29
|
|
|
/** @var ContainerInterface (mock) */ |
30
|
|
|
private $container; |
31
|
|
|
|
32
|
|
|
/** @var ArrayAdapter */ |
33
|
|
|
private $cache; |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
/* @var ContainerInterface $container */ |
38
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->cache = $this->createMock(AdapterInterface::class); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if (\interface_exists(TranslatorInterface::class)) { |
43
|
|
|
$this->translator = $this->createMock(TranslatorInterface::class); |
44
|
|
|
} else { |
45
|
|
|
$this->translator = $this->createMock(LegacyTranslatorInterface::class); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @group legacy |
51
|
|
|
* @expectedDeprecation Passing an instance of "Doctrine\Common\Cache\Cache" as the second argument in "Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker::__construct" is deprecated since KunstmaanAdminBundle 5.7 and an instance of "Symfony\Component\Cache\Adapter\AdapterInterface" will be required in KunstmaanAdminBundle 6.0. |
52
|
|
|
*/ |
53
|
|
|
public function testDeprecatedCacheConstructorParameter() |
54
|
|
|
{ |
55
|
|
|
new VersionChecker($this->createMock(ContainerInterface::class), new ArrayCache(), $this->translator); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @group legacy |
60
|
|
|
*/ |
61
|
|
|
public function testCacheConstructorParameterType() |
62
|
|
|
{ |
63
|
|
|
$this->expectException(\InvalidArgumentException::class); |
64
|
|
|
$this->expectExceptionMessage('The "$cache" parameter should implement "Doctrine\Common\Cache\Cache" or "Symfony\Component\Cache\Adapter\AdapterInterface"'); |
65
|
|
|
|
66
|
|
|
new VersionChecker($this->createMock(ContainerInterface::class), new \stdClass(), $this->translator); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @group legacy |
71
|
|
|
*/ |
72
|
|
|
public function testTranslatorConstructorParameterType() |
73
|
|
|
{ |
74
|
|
|
$this->expectException(\InvalidArgumentException::class); |
75
|
|
|
$this->expectExceptionMessage('The "$translator" parameter should be instance of "Symfony\Contracts\Translation\TranslatorInterface" or "Symfony\Component\Translation\TranslatorInterface"'); |
76
|
|
|
|
77
|
|
|
new VersionChecker($this->createMock(ContainerInterface::class), new ArrayCache(), new \stdClass()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array|null $methods |
82
|
|
|
* |
83
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject|VersionChecker |
84
|
|
|
*/ |
85
|
|
|
public function setUpVersionCheckerMock(?array $methods) |
86
|
|
|
{ |
87
|
|
|
$versionCheckerMock = $this->getMockBuilder(VersionChecker::class) |
88
|
|
|
->setConstructorArgs([$this->container, $this->cache, $this->translator]) |
89
|
|
|
->setMethods($methods) |
90
|
|
|
->getMock() |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
return $versionCheckerMock; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testIsEnabled() |
97
|
|
|
{ |
98
|
|
|
$this->container |
|
|
|
|
99
|
|
|
->expects($this->exactly(3)) |
100
|
|
|
->method('getParameter') |
101
|
|
|
->will($this->onConsecutiveCalls('url', 300, true)) |
102
|
|
|
; |
103
|
|
|
|
104
|
|
|
$versionChecker = $this->getVersionChecker($this->container, new ArrayAdapter(), $this->translator); |
105
|
|
|
|
106
|
|
|
$this->assertTrue($versionChecker->isEnabled()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testPeriodicallyCheck() |
110
|
|
|
{ |
111
|
|
|
$this->container |
|
|
|
|
112
|
|
|
->expects($this->exactly(3)) |
113
|
|
|
->method('getParameter') |
114
|
|
|
->will($this->onConsecutiveCalls('url', 300, true)) |
115
|
|
|
; |
116
|
|
|
|
117
|
|
|
$cacheItem = $this->createMock(ItemInterface::class); |
118
|
|
|
$cacheItem->method('isHit')->willReturn(true); |
119
|
|
|
$cacheItem->method('get')->willReturn([]); |
120
|
|
|
|
121
|
|
|
$this->cache |
|
|
|
|
122
|
|
|
->expects($this->once()) |
123
|
|
|
->method('getItem') |
124
|
|
|
->willReturn($cacheItem) |
125
|
|
|
; |
126
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(null); |
127
|
|
|
$versionCheckerMock->periodicallyCheck(); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testCheckWithInvalidResponse() |
131
|
|
|
{ |
132
|
|
|
$this->container |
|
|
|
|
133
|
|
|
->expects($this->exactly(4)) |
134
|
|
|
->method('getParameter') |
135
|
|
|
->will($this->onConsecutiveCalls('url', 300, true, 'title')) |
136
|
|
|
; |
137
|
|
|
|
138
|
|
|
$requestMock = $this->createMock(Request::class); |
139
|
|
|
|
140
|
|
|
$stackMock = $this->createMock(RequestStack::class); |
141
|
|
|
$stackMock |
142
|
|
|
->expects($this->once()) |
143
|
|
|
->method('getCurrentRequest') |
144
|
|
|
->willReturn($requestMock) |
145
|
|
|
; |
146
|
|
|
$kernelMock = $this->createMock(Kernel::class); |
147
|
|
|
|
148
|
|
|
$this->container |
|
|
|
|
149
|
|
|
->expects($this->exactly(2)) |
150
|
|
|
->method('get') |
151
|
|
|
->will($this->onConsecutiveCalls($stackMock, $kernelMock)) |
152
|
|
|
; |
153
|
|
|
|
154
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(['parseComposer']); |
155
|
|
|
$versionCheckerMock |
|
|
|
|
156
|
|
|
->expects($this->once()) |
157
|
|
|
->method('parseComposer') |
158
|
|
|
->willReturn(['name' => 'box/spout']) |
159
|
|
|
; |
160
|
|
|
$this->assertFalse($versionCheckerMock->check()); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @dataProvider provider |
165
|
|
|
*/ |
166
|
|
|
public function testCheck(string $lockPath, string $expectedType, string $expected) |
167
|
|
|
{ |
168
|
|
|
if ('exception' === $expectedType) { |
169
|
|
|
$this->expectException(ParseException::class); |
170
|
|
|
$this->expectExceptionMessage($expected); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->container |
|
|
|
|
174
|
|
|
->expects($this->any()) |
175
|
|
|
->method('getParameter') |
176
|
|
|
->will($this->onConsecutiveCalls('url', 300, true, 'title')) |
177
|
|
|
; |
178
|
|
|
|
179
|
|
|
$requestStack = new RequestStack(); |
180
|
|
|
$requestStack->push(new Request()); |
181
|
|
|
|
182
|
|
|
$translatorMock = $this->createMock(Translator::class); |
183
|
|
|
$translatorMock |
184
|
|
|
->expects($this->any()) |
185
|
|
|
->method('trans') |
186
|
|
|
->willReturn('translated') |
187
|
|
|
; |
188
|
|
|
|
189
|
|
|
$kernelMock = $this->createMock(Kernel::class); |
190
|
|
|
|
191
|
|
|
if ('instanceOf' === $expectedType) { |
192
|
|
|
$cacheItem = $this->createMock(ItemInterface::class); |
193
|
|
|
$cacheItem->method('isHit')->willReturn(false); |
194
|
|
|
$cacheItem->expects($this->once())->method('expiresAfter')->with(300); |
195
|
|
|
$cacheItem->expects($this->once())->method('set')->with($this->isInstanceOf($expected)); |
196
|
|
|
|
197
|
|
|
$this->cache |
|
|
|
|
198
|
|
|
->expects($this->once()) |
199
|
|
|
->method('getItem') |
200
|
|
|
->willReturn($cacheItem); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->container |
|
|
|
|
204
|
|
|
->expects($this->exactly(3)) |
205
|
|
|
->method('get') |
206
|
|
|
->will($this->onConsecutiveCalls($requestStack, $kernelMock, $translatorMock)) |
207
|
|
|
; |
208
|
|
|
|
209
|
|
|
$mock = new MockHandler([ |
210
|
|
|
new Response(200, ['X-Foo' => 'Bar'], \json_encode(['foo' => 'bar'])), |
211
|
|
|
]); |
212
|
|
|
|
213
|
|
|
$handler = HandlerStack::create($mock); |
214
|
|
|
$client = new Client(['handler' => $handler]); |
215
|
|
|
|
216
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(['getClient', 'getLockPath']); |
217
|
|
|
$versionCheckerMock |
|
|
|
|
218
|
|
|
->expects($this->any()) |
219
|
|
|
->method('getClient') |
220
|
|
|
->willReturn($client) |
221
|
|
|
; |
222
|
|
|
$versionCheckerMock |
223
|
|
|
->expects($this->once()) |
224
|
|
|
->method('getLockPath') |
225
|
|
|
->willReturn($lockPath) |
226
|
|
|
; |
227
|
|
|
|
228
|
|
|
if ('instanceOf' === $expectedType) { |
229
|
|
|
$this->assertInstanceOf($expected, $versionCheckerMock->check()); |
|
|
|
|
230
|
|
|
} else { |
231
|
|
|
$versionCheckerMock->check(); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function provider() |
236
|
|
|
{ |
237
|
|
|
$baseDir = __DIR__ . '/testdata'; |
238
|
|
|
|
239
|
|
|
return [ |
240
|
|
|
'composer.lock ok' => [$baseDir.'/composer_ok.lock', 'instanceOf', \stdClass::class], |
241
|
|
|
'composer.lock broken' => [$baseDir.'/composer_broken.lock', 'exception', 'translated (#4)'], |
242
|
|
|
'composer.lock bundleless' => [$baseDir.'/composer_bundleless.lock', 'exception', 'translated'], |
243
|
|
|
'composer.lock not found' => [$baseDir.'/composer_not_there.lock', 'exception', 'translated'], |
244
|
|
|
]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
private function getVersionChecker(ContainerInterface $container, AdapterInterface $cache, $translator) |
248
|
|
|
{ |
249
|
|
|
return new VersionChecker($container, $cache, $translator); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..