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