Completed
Push — master ( 1af7e7...796d56 )
by Jeroen
16s
created

unit/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
     *
25
     * @throws Exception
26
     */
27
    public function post()
28
    {
29
        throw new Exception('bang!');
30
    }
31
}
32
33
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...
34
{
35
    /**
36
     * @var VersionChecker
37
     */
38
    protected $object;
39
40
    /**
41
     * @var ContainerInterface
42
     */
43
    protected $container;
44
45
    /**
46
     * @var Cache
47
     */
48
    protected $cache;
49
50
    public function setUp()
51
    {
52
        $this->container = $this->createMock(ContainerInterface::class);
53
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, true));
54
        $this->cache = $this->createMock(Cache::class);
55
        $this->object = new VersionChecker($this->container, $this->cache);
56
    }
57
58
    /**
59
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
60
     */
61
    public function testVersionChecker()
62
    {
63
        $currentPath = getcwd();
64
        if (strpos($currentPath, 'src') !== false) {
65
            $currentPath = strstr($currentPath, 'src', true);
66
        }
67
        $path = realpath($currentPath.'/src');
68
        $trans = $this->createMock(Translator::class);
69
        $kernel = $this->createMock(Kernel::class);
70
        $request = $this->createMock(Request::class);
71
        $stack = $this->createMock(RequestStack::class);
72
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
73
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
74
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
75
        $request->expects($this->once())->method('getHttpHost')->willReturn('https://nasa.gov');
76
        $this->cache->expects($this->once())->method('fetch')->willReturn('not_an_array');
77
        $this->container->expects($this->any())->method('get')->will(
78
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
79
        );
80
81
        $object = $this->object;
82
        $this->assertTrue($object->isEnabled());
83
        $object->periodicallyCheck();
84
    }
85
86
    /**
87
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
88
     */
89
    public function testPeriodicCheckReturnsNothingWhenDisabled()
90
    {
91
        $this->container = $this->createMock(ContainerInterface::class);
92
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, false, false));
93
        $this->cache = $this->createMock(Cache::class);
94
        $this->object = new VersionChecker($this->container, $this->cache);
95
        $this->cache->expects($this->never())->method('fetch');
96
        $this->object->periodicallyCheck();
97
        $this->object->check();
98
    }
99
100
    /**
101
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
102
     */
103
    public function testCheckReturnsFalseOnException()
104
    {
105
        $this->container = $this->createMock(ContainerInterface::class);
106
        $this->container->expects($this->any())->method('getParameter')->will($this->onConsecutiveCalls('https://nasa.gov', 123, true, true));
107
        $this->cache = $this->createMock(Cache::class);
108
        $this->object = new VersionChecker($this->container, $this->cache);
109
        $this->cache->expects($this->never())->method('fetch');
110
        $currentPath = getcwd();
111
        if (strpos($currentPath, 'src') !== false) {
112
            $currentPath = strstr($currentPath, 'src', true);
113
        }
114
        $path = realpath($currentPath.'/src');
115
        $client = new FakeClient();
116
        $this->object->setClient($client);
117
        $trans = $this->createMock(Translator::class);
118
        $kernel = $this->createMock(Kernel::class);
119
        $request = $this->createMock(Request::class);
120
        $stack = $this->createMock(RequestStack::class);
121
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
122
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
123
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
124
        $this->container->expects($this->any())->method('get')->will(
125
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
126
        );
127
        $this->assertFalse($this->object->check());
128
    }
129
130
    /**
131
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
132
     */
133
    public function testGetData()
134
    {
135
        $mock = new MockHandler([new Response(200, ['Content-Type: application/json'], '{"fake": "data"}')]);
136
        $handler = HandlerStack::create($mock);
137
        $client = new Client(['handler' => $handler]);
138
        $this->object->setClient($client);
139
        $this->cache->expects($this->once())->method('fetch')->willReturn('not_an_array');
140
        $this->cache->expects($this->once())->method('save')->willReturn(true);
141
        $currentPath = getcwd();
142
        if (strpos($currentPath, 'src') !== false) {
143
            $currentPath = strstr($currentPath, 'src', true);
144
        }
145
        $path = realpath($currentPath.'/src');
146
        $trans = $this->createMock(Translator::class);
147
        $kernel = $this->createMock(Kernel::class);
148
        $request = $this->createMock(Request::class);
149
        $stack = $this->createMock(RequestStack::class);
150
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
151
        $stack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
152
        $kernel->expects($this->exactly(2))->method('getRootDir')->willReturn($path);
153
        $this->container->expects($this->any())->method('get')->will(
154
            $this->onConsecutiveCalls($stack, $kernel, $trans, $kernel)
155
        );
156
        $this->object->periodicallyCheck();
157
    }
158
159
    /**
160
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
161
     * @throws \ReflectionException
162
     */
163
    public function testCheckGetPackagesThrowsException()
164
    {
165
        $this->expectException(Exception::class);
166
        $trans = $this->createMock(Translator::class);
167
        $kernel = $this->createMock(Kernel::class);
168
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
169
        $this->container->expects($this->any())->method('get')->will(
170
            $this->onConsecutiveCalls($trans, $kernel)
171
        );
172
173
        $mirror = new ReflectionClass(VersionChecker::class);
174
        $method = $mirror->getMethod('getPackages');
175
        $method->setAccessible(true);
176
        $method->invoke($this->object);
177
    }
178
179
    /**
180
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
181
     * @throws \ReflectionException
182
     */
183 View Code Duplication
    public function testCheckGetPackagesThrowsExceptionWhenNoPackagesInLock()
184
    {
185
        $currentPath = getcwd();
186
        if (strpos($currentPath, 'src') !== false) {
187
            $currentPath = strstr($currentPath, 'src', true);
188
        }
189
        $path = realpath($currentPath.'/src/Kunstmaan/AdminBundle/Tests/Helper/VersionCheck');
190
        $this->expectException(Exception::class);
191
        $trans = $this->createMock(Translator::class);
192
        $kernel = $this->createMock(Kernel::class);
193
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
194
        $kernel->expects($this->once())->method('getRootDir')->willReturn($path);
195
        $this->container->expects($this->any())->method('get')->will(
196
            $this->onConsecutiveCalls($trans, $kernel)
197
        );
198
199
        $mirror = new ReflectionClass(VersionChecker::class);
200
        $method = $mirror->getMethod('getPackages');
201
        $method->setAccessible(true);
202
        $method->invoke($this->object);
203
    }
204
205
    /**
206
     * @throws \Kunstmaan\AdminBundle\Helper\VersionCheck\Exception\ParseException
207
     * @throws \ReflectionException
208
     */
209 View Code Duplication
    public function testCheckGetPackagesThrowsExceptionWithBadJson()
210
    {
211
        $currentPath = getcwd();
212
        if (strpos($currentPath, 'src') !== false) {
213
            $currentPath = strstr($currentPath, 'src', true);
214
        }
215
        $path = realpath($currentPath.'/src/Kunstmaan/AdminBundle/Tests/Helper');
216
        $this->expectException(Exception::class);
217
        $trans = $this->createMock(Translator::class);
218
        $kernel = $this->createMock(Kernel::class);
219
        $trans->expects($this->any())->method('trans')->willReturn('algo en una differente idioma');
220
        $kernel->expects($this->once())->method('getRootDir')->willReturn($path);
221
        $this->container->expects($this->any())->method('get')->will(
222
            $this->onConsecutiveCalls($trans, $kernel)
223
        );
224
225
        $mirror = new ReflectionClass(VersionChecker::class);
226
        $method = $mirror->getMethod('getPackages');
227
        $method->setAccessible(true);
228
        $method->invoke($this->object);
229
    }
230
}
231