Completed
Pull Request — master (#2101)
by Kevin
18:19
created

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

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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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);
52
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, true ));
53
        $this->cache = $this->createMock(Cache::class);
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
        $path = realpath(getcwd().'/src');
63
        $trans = $this->createMock(Translator::class);
64
        $kernel = $this->createMock(Kernel::class);
65
        $request = $this->createMock(Request::class);
66
        $stack = $this->createMock(RequestStack::class);
67
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
68
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
69
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
70
        $request->expects($this->once())->method('getHttpHost')->willReturn('https://nasa.gov');
71
        $this->cache->expects($this->once())->method('fetch')->willReturn('not_an_array');
72
        $this->container->expects($this->any())->method('get')->will(
73
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
74
        );
75
76
        $object = $this->object;
77
        $this->assertTrue($object->isEnabled());
78
        $object->periodicallyCheck();
79
    }
80
81
    /**
82
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
83
     */
84
    public function testPeriodicCheckReturnsNothingWhenDisabled()
85
    {
86
        $this->container = $this->createMock(ContainerInterface::class);
87
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, false, false));
88
        $this->cache = $this->createMock(Cache::class);
89
        $this->object = new VersionChecker($this->container, $this->cache);
90
        $this->cache->expects($this->never())->method('fetch');
91
        $this->object->periodicallyCheck();
92
        $this->object->check();
93
    }
94
95
    /**
96
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
97
     */
98
    public function testCheckReturnsFalseOnException()
99
    {
100
        $this->container = $this->createMock(ContainerInterface::class);
101
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, true, true));
102
        $this->cache = $this->createMock(Cache::class);
103
        $this->object = new VersionChecker($this->container, $this->cache);
104
        $this->cache->expects($this->never())->method('fetch');
105
        $path = realpath(getcwd().'/src');
106
        $client = new FakeClient();
107
        $this->object->setClient($client);
108
        $trans = $this->createMock(Translator::class);
109
        $kernel = $this->createMock(Kernel::class);
110
        $request = $this->createMock(Request::class);
111
        $stack = $this->createMock(RequestStack::class);
112
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
113
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
114
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
115
        $this->container->expects($this->any())->method('get')->will(
116
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
117
        );
118
        $this->assertFalse($this->object->check());
119
    }
120
121
    /**
122
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
123
     */
124
    public function testGetData()
125
    {
126
        $mock = new MockHandler([new Response(200, ['Content-Type: application/json'], '{"fake": "data"}')]);
127
        $handler = HandlerStack::create($mock);
128
        $client = new Client(['handler' => $handler]);
129
        $this->object->setClient($client);
130
        $this->cache->expects($this->once())->method('fetch')->willReturn('not_an_array');
131
        $this->cache->expects($this->once())->method('save')->willReturn(true);
132
        $path = realpath(getcwd().'/src');
133
        $trans = $this->createMock(Translator::class);
134
        $kernel = $this->createMock(Kernel::class);
135
        $request = $this->createMock(Request::class);
136
        $stack = $this->createMock(RequestStack::class);
137
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
138
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
139
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
140
        $this->container->expects($this->any())->method('get')->will(
141
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
142
        );
143
        $this->object->periodicallyCheck();
144
    }
145
146
    /**
147
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
148
     * @throws \ReflectionException
149
     */
150
    public function testCheckGetPackagesThrowsException()
151
    {
152
        $this->expectException(Exception::class);
153
        $trans = $this->createMock(Translator::class);
154
        $kernel = $this->createMock(Kernel::class);
155
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
156
        $this->container->expects($this->any())->method('get')->will(
157
            $this->onConsecutiveCalls($trans, $kernel)
158
        );
159
160
        $mirror = new ReflectionClass(VersionChecker::class);
161
        $method = $mirror->getMethod('getPackages');
162
        $method->setAccessible(true);
163
        $method->invoke($this->object);
164
    }
165
166
    /**
167
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
168
     * @throws \ReflectionException
169
     */
170 View Code Duplication
    public function testCheckGetPackagesThrowsExceptionWhenNoPackagesInLock()
171
    {
172
        $path = realpath(getcwd().'/src/Kunstmaan/AdminBundle/Tests/Helper/VersionCheck');
173
        $this->expectException(Exception::class);
174
        $trans = $this->createMock(Translator::class);
175
        $kernel = $this->createMock(Kernel::class);
176
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
177
        $kernel->expects($this->once())->method('getRootDir')->willReturn($path);
178
        $this->container->expects($this->any())->method('get')->will(
179
            $this->onConsecutiveCalls($trans, $kernel)
180
        );
181
182
        $mirror = new ReflectionClass(VersionChecker::class);
183
        $method = $mirror->getMethod('getPackages');
184
        $method->setAccessible(true);
185
        $method->invoke($this->object);
186
    }
187
188
    /**
189
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
190
     * @throws \ReflectionException
191
     */
192 View Code Duplication
    public function testCheckGetPackagesThrowsExceptionWithBadJson()
193
    {
194
        $path = realpath(getcwd().'/src/Kunstmaan/AdminBundle/Tests/Helper');
195
        $this->expectException(Exception::class);
196
        $trans = $this->createMock(Translator::class);
197
        $kernel = $this->createMock(Kernel::class);
198
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
199
        $kernel->expects($this->once())->method('getRootDir')->willReturn($path);
200
        $this->container->expects($this->any())->method('get')->will(
201
            $this->onConsecutiveCalls($trans, $kernel)
202
        );
203
204
        $mirror = new ReflectionClass(VersionChecker::class);
205
        $method = $mirror->getMethod('getPackages');
206
        $method->setAccessible(true);
207
        $method->invoke($this->object);
208
    }
209
}
210