Completed
Branch master (bbf8b1)
by timegryd
03:42 queued 01:45
created

OpcacheResetCommandHelperTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 0
loc 208
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGetFileName() 0 6 1
A testGetFilePath() 0 6 1
A testGenerateUrl() 0 6 1
A testCreateFileDirNotExists() 0 4 1
A setUpFilesystem() 0 9 1
A tearDownFilesystem() 0 4 1
B testCreateFileDirExists() 0 25 1
A testClean() 0 23 1
A testHandleResponseBadStatusCode() 0 6 1
A testHandleResponseBadJson() 0 6 1
A testHandleResponsePropertySuccessMissing() 0 6 1
A testHandleResponsePropertyMessageMissing() 0 6 1
A testHandleResponseFailure() 0 6 1
A testHandleResponseSuccess() 0 8 1
A getMockedResponse() 0 18 2
1
<?php
2
3
namespace Timegryd\OpcacheResetBundle\Tests\Helper;
4
5
use Symfony\Component\HttpKernel\KernelInterface;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Filesystem\Filesystem;
8
use GuzzleHttp\Psr7\Response as HttpResponse;
9
use Timegryd\OpcacheResetBundle\Helper\OpcacheResetCommandHelper;
10
11
class OpcacheResetCommandHelperTest extends \PHPUnit\Framework\TestCase
12
{
13
    /**
14
     * @var KernelInterface
15
     */
16
    protected $kernel;
17
18
    /**
19
     * @var OpcacheResetCommandHelper
20
     */
21
    protected $helper;
22
23
    /**
24
     * @var Filesystem
25
     */
26
    protected $filesystem;
27
28
    /**
29
     * @var string
30
     */
31
    protected $workspace;
32
33
    protected function setUp()
34
    {
35
        $this->kernel = \Mockery::mock(KernelInterface::class);
36
        $this->helper = new OpcacheResetCommandHelper($this->kernel);
37
    }
38
39
    public function testGetFileName()
40
    {
41
        $fileName = $this->helper->getFileName('test');
42
43
        $this->assertEquals($fileName, 'opcache-reset-test.php');
44
    }
45
46
    public function testGetFilePath()
47
    {
48
        $filePath = $this->helper->getFilePath('dirtest', 'test');
49
50
        $this->assertEquals($filePath, 'dirtest/opcache-reset-test.php');
51
    }
52
53
    public function testGenerateUrl()
54
    {
55
        $url = $this->helper->generateUrl('timegryd.io', 'test');
56
57
        $this->assertEquals($url, 'timegryd.io/opcache-reset-test.php');
58
    }
59
60
    /**
61
     * @expectedException RuntimeException
62
     */
63
    public function testCreateFileDirNotExists()
64
    {
65
        $this->helper->createFile('dir-not-existing', 'token');
66
    }
67
68
    protected function setUpFilesystem()
69
    {
70
        $this->filesystem = new Filesystem();
71
        $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
72
73
        mkdir($this->workspace, 0777, true);
74
75
        $this->workspace = realpath($this->workspace);
76
    }
77
78
    protected function tearDownFilesystem()
79
    {
80
        $this->filesystem->remove($this->workspace);
81
    }
82
83
    public function testCreateFileDirExists()
84
    {
85
        $this->setUpFilesystem();
86
87
        $sourceFile = $this->workspace.DIRECTORY_SEPARATOR.'source_file';
88
89
        file_put_contents($sourceFile, 'SOURCE FILE');
90
91
        $this
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...Kernel\KernelInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            ->kernel
93
            ->shouldReceive('locateResource')
94
            ->andReturn($sourceFile)
95
        ;
96
97
        $return = $this->helper->createFile($this->workspace, 'token');
98
99
        $this->assertInstanceOf(OpcacheResetCommandHelper::class, $return);
100
101
        $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'opcache-reset-token.php';
102
103
        $this->assertFileExists($targetFilePath);
104
        $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
105
106
        $this->tearDownFilesystem();
107
    }
108
109
    public function testClean()
110
    {
111
        $this->setUpFilesystem();
112
113
        $fileToDelete1 = $this->workspace.DIRECTORY_SEPARATOR.'opcache-reset-1.php';
114
        touch($fileToDelete1);
115
116
        $fileToDelete2 = $this->workspace.DIRECTORY_SEPARATOR.'opcache-reset-2.php';
117
        touch($fileToDelete2);
118
119
        $fileNotToDelete = $this->workspace.DIRECTORY_SEPARATOR.'filenottodelete.php';
120
        touch($fileNotToDelete);
121
122
        $return = $this->helper->clean($this->workspace);
123
124
        $this->assertFileNotExists($fileToDelete1);
125
        $this->assertFileNotExists($fileToDelete2);
126
        $this->assertFileExists($fileNotToDelete);
127
128
        $this->assertInstanceOf(OpcacheResetCommandHelper::class, $return);
129
130
        $this->tearDownFilesystem();
131
    }
132
133
    /**
134
     * @expectedException RuntimeException
135
     */
136
    public function testHandleResponseBadStatusCode()
137
    {
138
        $response = $this->getMockedResponse(null, Response::HTTP_NOT_FOUND);
139
140
        $this->helper->handleResponse($response);
141
    }
142
143
    /**
144
     * @expectedException RuntimeException
145
     */
146
    public function testHandleResponseBadJson()
147
    {
148
        $response = $this->getMockedResponse('bad-json');
149
150
        $this->helper->handleResponse($response);
151
    }
152
153
    /**
154
     * @expectedException InvalidArgumentException
155
     */
156
    public function testHandleResponsePropertySuccessMissing()
157
    {
158
        $response = $this->getMockedResponse('{"message": "failure"}');
159
160
        $this->helper->handleResponse($response);
161
    }
162
163
    /**
164
     * @expectedException InvalidArgumentException
165
     */
166
    public function testHandleResponsePropertyMessageMissing()
167
    {
168
        $response = $this->getMockedResponse('{"success": true}');
169
170
        $this->helper->handleResponse($response);
171
    }
172
173
    /**
174
     * @expectedException RuntimeException
175
     */
176
    public function testHandleResponseFailure()
177
    {
178
        $response = $this->getMockedResponse('{"success": false, "message": "failure"}');
179
180
        $this->helper->handleResponse($response);
181
    }
182
183
    public function testHandleResponseSuccess()
184
    {
185
        $response = $this->getMockedResponse('{"success": "true", "message": "Success"}');
186
187
        $message = $this->helper->handleResponse($response);
188
189
        $this->assertEquals('Success', $message);
190
    }
191
192
    /**
193
     * Mock Response.
194
     *
195
     * @param string $body
196
     * @param int    $status
197
     *
198
     * @return HttpResponse
199
     */
200
    protected function getMockedResponse($body = null, $status = Response::HTTP_OK)
201
    {
202
        $response = \Mockery::mock(HttpResponse::class);
203
204
        $response
205
            ->shouldReceive('getStatusCode')
206
            ->andReturn($status)
207
        ;
208
209
        if (null !== $body) {
210
            $response
211
                ->shouldReceive('getBody')
212
                ->andReturn($body)
213
            ;
214
        }
215
216
        return $response;
217
    }
218
}
219