1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Tests\Helper; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Handler\MockHandler; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use GuzzleHttp\Psr7\Response; |
9
|
|
|
use Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException; |
10
|
|
|
use Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Doctrine\Common\Cache\Cache; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
17
|
|
|
use Symfony\Component\Translation\Translator; |
18
|
|
|
|
19
|
|
|
class VersionCheckTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** @var ContainerInterface (mock) */ |
22
|
|
|
private $container; |
23
|
|
|
|
24
|
|
|
/** @var Cache (mock) */ |
25
|
|
|
private $cache; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
/* @var ContainerInterface $container */ |
30
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
31
|
|
|
|
32
|
|
|
/* @var Cache $cache */ |
33
|
|
|
$this->cache = $this->createMock(Cache::class); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array|null $methods |
38
|
|
|
* |
39
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject|VersionChecker |
40
|
|
|
*/ |
41
|
|
|
public function setUpVersionCheckerMock(?array $methods) |
42
|
|
|
{ |
43
|
|
|
$versionCheckerMock = $this->getMockBuilder(VersionChecker::class) |
44
|
|
|
->setConstructorArgs([$this->container, $this->cache]) |
45
|
|
|
->setMethods($methods) |
46
|
|
|
->getMock() |
47
|
|
|
; |
48
|
|
|
|
49
|
|
|
return $versionCheckerMock; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testIsEnabled() |
53
|
|
|
{ |
54
|
|
|
$this->container |
|
|
|
|
55
|
|
|
->expects($this->exactly(3)) |
56
|
|
|
->method('getParameter') |
57
|
|
|
->will($this->onConsecutiveCalls('url', 300, true)) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(null); |
61
|
|
|
$this->assertIsBool($versionCheckerMock->isEnabled()); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testPeriodicallyCheck() |
65
|
|
|
{ |
66
|
|
|
$this->container |
|
|
|
|
67
|
|
|
->expects($this->exactly(3)) |
68
|
|
|
->method('getParameter') |
69
|
|
|
->will($this->onConsecutiveCalls('url', 300, true)) |
70
|
|
|
; |
71
|
|
|
|
72
|
|
|
$this->cache |
|
|
|
|
73
|
|
|
->expects($this->once()) |
74
|
|
|
->method('fetch') |
75
|
|
|
->willReturn([]) |
76
|
|
|
; |
77
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(null); |
78
|
|
|
$versionCheckerMock->periodicallyCheck(); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testCheckWithInvalidResponse() |
82
|
|
|
{ |
83
|
|
|
$this->container |
|
|
|
|
84
|
|
|
->expects($this->exactly(4)) |
85
|
|
|
->method('getParameter') |
86
|
|
|
->will($this->onConsecutiveCalls('url', 300, true, 'title')) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$requestMock = $this->createMock(Request::class); |
90
|
|
|
|
91
|
|
|
$stackMock = $this->createMock(RequestStack::class); |
92
|
|
|
$stackMock |
93
|
|
|
->expects($this->once()) |
94
|
|
|
->method('getCurrentRequest') |
95
|
|
|
->willReturn($requestMock) |
96
|
|
|
; |
97
|
|
|
$kernelMock = $this->createMock(Kernel::class); |
98
|
|
|
|
99
|
|
|
$this->container |
|
|
|
|
100
|
|
|
->expects($this->exactly(2)) |
101
|
|
|
->method('get') |
102
|
|
|
->will($this->onConsecutiveCalls($stackMock, $kernelMock)) |
103
|
|
|
; |
104
|
|
|
|
105
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(['parseComposer']); |
106
|
|
|
$versionCheckerMock |
|
|
|
|
107
|
|
|
->expects($this->once()) |
108
|
|
|
->method('parseComposer') |
109
|
|
|
->willReturn(['name' => 'box/spout']) |
110
|
|
|
; |
111
|
|
|
$this->assertFalse($versionCheckerMock->check()); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @dataProvider provider |
116
|
|
|
*/ |
117
|
|
|
public function testCheck(string $lockPath, string $expectedType, string $expected) |
118
|
|
|
{ |
119
|
|
|
if ('exception' === $expectedType) { |
120
|
|
|
$this->expectException(ParseException::class); |
121
|
|
|
$this->expectExceptionMessage($expected); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->container |
|
|
|
|
125
|
|
|
->expects($this->any()) |
126
|
|
|
->method('getParameter') |
127
|
|
|
->will($this->onConsecutiveCalls('url', 300, true, 'title')) |
128
|
|
|
; |
129
|
|
|
|
130
|
|
|
$requestMock = $this->createMock(Request::class); |
131
|
|
|
|
132
|
|
|
$stackMock = $this->createMock(RequestStack::class); |
133
|
|
|
$stackMock |
134
|
|
|
->expects($this->once()) |
135
|
|
|
->method('getCurrentRequest') |
136
|
|
|
->willReturn($requestMock) |
137
|
|
|
; |
138
|
|
|
|
139
|
|
|
$translatorMock = $this->createMock(Translator::class); |
140
|
|
|
$translatorMock |
141
|
|
|
->expects($this->any()) |
142
|
|
|
->method('trans') |
143
|
|
|
->willReturn('translated') |
144
|
|
|
; |
145
|
|
|
|
146
|
|
|
$kernelMock = $this->createMock(Kernel::class); |
147
|
|
|
|
148
|
|
|
$this->container |
|
|
|
|
149
|
|
|
->expects($this->exactly(3)) |
150
|
|
|
->method('get') |
151
|
|
|
->will($this->onConsecutiveCalls($stackMock, $kernelMock, $translatorMock)) |
152
|
|
|
; |
153
|
|
|
|
154
|
|
|
$mock = new MockHandler([ |
155
|
|
|
new Response(200, ['X-Foo' => 'Bar'], \json_encode(['foo' => 'bar'])), |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
$handler = HandlerStack::create($mock); |
159
|
|
|
$client = new Client(['handler' => $handler]); |
160
|
|
|
|
161
|
|
|
$versionCheckerMock = $this->setUpVersionCheckerMock(['getClient', 'getLockPath']); |
162
|
|
|
$versionCheckerMock |
|
|
|
|
163
|
|
|
->expects($this->any()) |
164
|
|
|
->method('getClient') |
165
|
|
|
->willReturn($client) |
166
|
|
|
; |
167
|
|
|
$versionCheckerMock |
168
|
|
|
->expects($this->once()) |
169
|
|
|
->method('getLockPath') |
170
|
|
|
->willReturn($lockPath) |
171
|
|
|
; |
172
|
|
|
|
173
|
|
|
if ('instanceOf' === $expectedType) { |
174
|
|
|
$this->assertInstanceOf($expected, $versionCheckerMock->check()); |
|
|
|
|
175
|
|
|
} else { |
176
|
|
|
$versionCheckerMock->check(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function provider() |
181
|
|
|
{ |
182
|
|
|
$baseDir = __DIR__ . '/testdata'; |
183
|
|
|
|
184
|
|
|
return [ |
185
|
|
|
'composer.lock ok' => [$baseDir.'/composer_ok.lock', 'instanceOf', \stdClass::class], |
186
|
|
|
'composer.lock broken' => [$baseDir.'/composer_broken.lock', 'exception', 'translated (#4)'], |
187
|
|
|
'composer.lock bundleless' => [$baseDir.'/composer_bundleless.lock', 'exception', 'translated'], |
188
|
|
|
'composer.lock not found' => [$baseDir.'/composer_not_there.lock', 'exception', 'translated'], |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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..