Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

unit/Helper/VersionCheck/VersionCheckTest.php (6 issues)

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 Doctrine\Common\Cache\Cache;
6
use Exception;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Handler\MockHandler;
9
use GuzzleHttp\HandlerStack;
10
use GuzzleHttp\Psr7\Response;
11
use Kunstmaan\AdminBundle\Helper\VersionCheck\VersionChecker;
12
use Kunstmaan\TranslatorBundle\Service\Translator\Translator;
13
use PHPUnit_Framework_TestCase;
14
use ReflectionClass;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Component\HttpKernel\Kernel;
19
20
class FakeClient extends Client
21
{
22
    /**
23
     * @return \Psr\Http\Message\ResponseInterface|void
24
     * @throws Exception
25
     */
26
    public function post()
27
    {
28
        throw new Exception('bang!');
29
    }
30
}
31
32
class VersionCheckTest extends PHPUnit_Framework_TestCase
33
{
34
    /**
35
     * @var VersionChecker
36
     */
37
    protected $object;
38
39
    /**
40
     * @var ContainerInterface
41
     */
42
    protected $container;
43
44
    /**
45
     * @var Cache
46
     */
47
    protected $cache;
48
49
    public function setUp()
50
    {
51
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
52
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, true ));
53
        $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...
54
        $this->object = new VersionChecker($this->container, $this->cache);
55
    }
56
57
    /**
58
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
59
     */
60
    public function testVersionChecker()
61
    {
62
        $currentPath = getcwd();
63
        if(strpos($currentPath, 'src') !== false) {
64
            $currentPath = strstr($currentPath, 'src', true);
65
        }
66
        $path = realpath($currentPath.'/src');
67
        $trans = $this->createMock(Translator::class);
68
        $kernel = $this->createMock(Kernel::class);
69
        $request = $this->createMock(Request::class);
70
        $stack = $this->createMock(RequestStack::class);
71
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
72
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
73
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
74
        $request->expects($this->once())->method('getHttpHost')->willReturn('https://nasa.gov');
75
        $this->cache->expects($this->once())->method('fetch')->willReturn('not_an_array');
76
        $this->container->expects($this->any())->method('get')->will(
77
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
78
        );
79
80
        $object = $this->object;
81
        $this->assertTrue($object->isEnabled());
82
        $object->periodicallyCheck();
83
    }
84
85
    /**
86
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
87
     */
88
    public function testPeriodicCheckReturnsNothingWhenDisabled()
89
    {
90
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
91
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, false, false));
92
        $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...
93
        $this->object = new VersionChecker($this->container, $this->cache);
94
        $this->cache->expects($this->never())->method('fetch');
95
        $this->object->periodicallyCheck();
96
        $this->object->check();
97
    }
98
99
    /**
100
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
101
     */
102
    public function testCheckReturnsFalseOnException()
103
    {
104
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
105
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, true, true));
106
        $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...
107
        $this->object = new VersionChecker($this->container, $this->cache);
108
        $this->cache->expects($this->never())->method('fetch');
109
        $currentPath = getcwd();
110
        if(strpos($currentPath, 'src') !== false) {
111
            $currentPath = strstr($currentPath, 'src', true);
112
        }
113
        $path = realpath($currentPath.'/src');
114
        $client = new FakeClient();
115
        $this->object->setClient($client);
116
        $trans = $this->createMock(Translator::class);
117
        $kernel = $this->createMock(Kernel::class);
118
        $request = $this->createMock(Request::class);
119
        $stack = $this->createMock(RequestStack::class);
120
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
121
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
122
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
123
        $this->container->expects($this->any())->method('get')->will(
124
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
125
        );
126
        $this->assertFalse($this->object->check());
127
    }
128
129
    /**
130
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
131
     */
132
    public function testGetData()
133
    {
134
        $mock = new MockHandler([new Response(200, ['Content-Type: application/json'], '{"fake": "data"}')]);
135
        $handler = HandlerStack::create($mock);
136
        $client = new Client(['handler' => $handler]);
137
        $this->object->setClient($client);
138
        $this->cache->expects($this->once())->method('fetch')->willReturn('not_an_array');
139
        $this->cache->expects($this->once())->method('save')->willReturn(true);
140
        $currentPath = getcwd();
141
        if(strpos($currentPath, 'src') !== false) {
142
            $currentPath = strstr($currentPath, 'src', true);
143
        }
144
        $path = realpath($currentPath.'/src');
145
        $trans = $this->createMock(Translator::class);
146
        $kernel = $this->createMock(Kernel::class);
147
        $request = $this->createMock(Request::class);
148
        $stack = $this->createMock(RequestStack::class);
149
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
150
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
151
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
152
        $this->container->expects($this->any())->method('get')->will(
153
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
154
        );
155
        $this->object->periodicallyCheck();
156
    }
157
158
    /**
159
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
160
     * @throws \ReflectionException
161
     */
162
    public function testCheckGetPackagesThrowsException()
163
    {
164
        $this->expectException(Exception::class);
165
        $trans = $this->createMock(Translator::class);
166
        $kernel = $this->createMock(Kernel::class);
167
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
168
        $this->container->expects($this->any())->method('get')->will(
169
            $this->onConsecutiveCalls($trans, $kernel)
170
        );
171
172
        $mirror = new ReflectionClass(VersionChecker::class);
173
        $method = $mirror->getMethod('getPackages');
174
        $method->setAccessible(true);
175
        $method->invoke($this->object);
176
    }
177
178
    /**
179
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
180
     * @throws \ReflectionException
181
     */
182 View Code Duplication
    public function testCheckGetPackagesThrowsExceptionWhenNoPackagesInLock()
183
    {
184
        $currentPath = getcwd();
185
        if(strpos($currentPath, 'src') !== false) {
186
            $currentPath = strstr($currentPath, 'src', true);
187
        }
188
        $path = realpath($currentPath.'/src/Kunstmaan/AdminBundle/Tests/Helper/VersionCheck');
189
        $this->expectException(Exception::class);
190
        $trans = $this->createMock(Translator::class);
191
        $kernel = $this->createMock(Kernel::class);
192
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
193
        $kernel->expects($this->once())->method('getRootDir')->willReturn($path);
194
        $this->container->expects($this->any())->method('get')->will(
195
            $this->onConsecutiveCalls($trans, $kernel)
196
        );
197
198
        $mirror = new ReflectionClass(VersionChecker::class);
199
        $method = $mirror->getMethod('getPackages');
200
        $method->setAccessible(true);
201
        $method->invoke($this->object);
202
    }
203
204
    /**
205
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
206
     * @throws \ReflectionException
207
     */
208 View Code Duplication
    public function testCheckGetPackagesThrowsExceptionWithBadJson()
209
    {
210
        $currentPath = getcwd();
211
        if(strpos($currentPath, 'src') !== false) {
212
            $currentPath = strstr($currentPath, 'src', true);
213
        }
214
        $path = realpath($currentPath.'/src/Kunstmaan/AdminBundle/Tests/Helper');
215
        $this->expectException(Exception::class);
216
        $trans = $this->createMock(Translator::class);
217
        $kernel = $this->createMock(Kernel::class);
218
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
219
        $kernel->expects($this->once())->method('getRootDir')->willReturn($path);
220
        $this->container->expects($this->any())->method('get')->will(
221
            $this->onConsecutiveCalls($trans, $kernel)
222
        );
223
224
        $mirror = new ReflectionClass(VersionChecker::class);
225
        $method = $mirror->getMethod('getPackages');
226
        $method->setAccessible(true);
227
        $method->invoke($this->object);
228
    }
229
}
230