Completed
Pull Request — master (#2751)
by Jeroen
06:09
created

unit/Helper/VersionCheck/VersionCheckTest.php (1 issue)

assigning incompatible types to properties.

Bug Documentation Major

Upgrade to new PHP Analysis Engine

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 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
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);
33
34
        /* @var Cache $cache */
35
        $this->cache = $this->createMock(Cache::class);
0 ignored issues
show
Documentation Bug introduced by
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..

Loading history...
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
     * @param array|null $methods
46
     *
47
     * @return \PHPUnit\Framework\MockObject\MockObject|VersionChecker
48
     */
49
    public function setUpVersionCheckerMock(?array $methods)
50
    {
51
        $versionCheckerMock = $this->getMockBuilder(VersionChecker::class)
52
            ->setConstructorArgs([$this->container, $this->cache, $this->translator])
53
            ->setMethods($methods)
54
            ->getMock()
55
        ;
56
57
        return $versionCheckerMock;
58
    }
59
60
    public function testIsEnabled()
61
    {
62
        $this->container
63
            ->expects($this->exactly(3))
64
            ->method('getParameter')
65
            ->will($this->onConsecutiveCalls('url', 300, true))
66
        ;
67
68
        $versionCheckerMock = $this->setUpVersionCheckerMock(null);
69
        $this->assertIsBool($versionCheckerMock->isEnabled());
70
    }
71
72
    public function testPeriodicallyCheck()
73
    {
74
        $this->container
75
            ->expects($this->exactly(3))
76
            ->method('getParameter')
77
            ->will($this->onConsecutiveCalls('url', 300, true))
78
        ;
79
80
        $this->cache
81
            ->expects($this->once())
82
            ->method('fetch')
83
            ->willReturn([])
84
        ;
85
        $versionCheckerMock = $this->setUpVersionCheckerMock(null);
86
        $versionCheckerMock->periodicallyCheck();
87
    }
88
89
    public function testCheckWithInvalidResponse()
90
    {
91
        $this->container
92
            ->expects($this->exactly(4))
93
            ->method('getParameter')
94
            ->will($this->onConsecutiveCalls('url', 300, true, 'title'))
95
        ;
96
97
        $requestMock = $this->createMock(Request::class);
98
99
        $stackMock = $this->createMock(RequestStack::class);
100
        $stackMock
101
            ->expects($this->once())
102
            ->method('getCurrentRequest')
103
            ->willReturn($requestMock)
104
        ;
105
        $kernelMock = $this->createMock(Kernel::class);
106
107
        $this->container
108
            ->expects($this->exactly(2))
109
            ->method('get')
110
            ->will($this->onConsecutiveCalls($stackMock, $kernelMock))
111
        ;
112
113
        $versionCheckerMock = $this->setUpVersionCheckerMock(['parseComposer']);
114
        $versionCheckerMock
115
            ->expects($this->once())
116
            ->method('parseComposer')
117
            ->willReturn(['name' => 'box/spout'])
118
        ;
119
        $this->assertFalse($versionCheckerMock->check());
120
    }
121
122
    /**
123
     * @dataProvider provider
124
     */
125
    public function testCheck(string $lockPath, string $expectedType, string $expected)
126
    {
127
        if ('exception' === $expectedType) {
128
            $this->expectException(ParseException::class);
129
            $this->expectExceptionMessage($expected);
130
        }
131
132
        $this->container
133
            ->expects($this->any())
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
147
        $translatorMock = $this->createMock(Translator::class);
148
        $translatorMock
149
            ->expects($this->any())
150
            ->method('trans')
151
            ->willReturn('translated')
152
        ;
153
154
        $kernelMock = $this->createMock(Kernel::class);
155
156
        $this->container
157
            ->expects($this->exactly(3))
158
            ->method('get')
159
            ->will($this->onConsecutiveCalls($stackMock, $kernelMock, $translatorMock))
160
        ;
161
162
        $mock = new MockHandler([
163
            new Response(200, ['X-Foo' => 'Bar'], \json_encode(['foo' => 'bar'])),
164
        ]);
165
166
        $handler = HandlerStack::create($mock);
167
        $client = new Client(['handler' => $handler]);
168
169
        $versionCheckerMock = $this->setUpVersionCheckerMock(['getClient', 'getLockPath']);
170
        $versionCheckerMock
171
            ->expects($this->any())
172
            ->method('getClient')
173
            ->willReturn($client)
174
        ;
175
        $versionCheckerMock
176
            ->expects($this->once())
177
            ->method('getLockPath')
178
            ->willReturn($lockPath)
179
        ;
180
181
        if ('instanceOf' === $expectedType) {
182
            $this->assertInstanceOf($expected, $versionCheckerMock->check());
183
        } else {
184
            $versionCheckerMock->check();
185
        }
186
    }
187
188
    public function provider()
189
    {
190
        $baseDir = __DIR__ . '/testdata';
191
192
        return [
193
            'composer.lock ok' => [$baseDir.'/composer_ok.lock', 'instanceOf', \stdClass::class],
194
            'composer.lock broken' => [$baseDir.'/composer_broken.lock', 'exception', 'translated (#4)'],
195
            'composer.lock bundleless' => [$baseDir.'/composer_bundleless.lock', 'exception', 'translated'],
196
            'composer.lock not found' => [$baseDir.'/composer_not_there.lock', 'exception', 'translated'],
197
        ];
198
    }
199
}
200