Completed
Push — master ( d055c4...9f7aba )
by timegryd
04:27
created

OpcacheResetCommandHelperTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 208
Duplicated Lines 32.21 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 67
loc 208
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A tearDown() 0 4 1
A testGetFileName() 0 6 1
A testGetFilePath() 0 6 1
A testGenerateUrl() 0 6 1
A testCreateFileDirNotExists() 0 4 1
A testCreateFileDirExists() 0 21 1
A testClean() 0 19 1
A testHandleResponseBadStatusCode() 0 11 1
A testHandleResponseBadJson() 13 13 1
A testHandleResponsePropertySuccessMissing() 13 13 1
A testHandleResponsePropertyMessageMissing() 13 13 1
A testHandleResponseFailure() 13 13 1
A testHandleResponseSuccess() 15 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        $this->filesystem = new Filesystem();
39
        $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
40
        mkdir($this->workspace, 0777, true);
41
        $this->workspace = realpath($this->workspace);
42
    }
43
44
    protected function tearDown()
45
    {
46
        $this->filesystem->remove($this->workspace);
47
    }
48
49
    public function testGetFileName()
50
    {
51
        $fileName = $this->helper->getFileName('test');
52
53
        $this->assertEquals($fileName, 'opcache-reset-test.php');
54
    }
55
56
    public function testGetFilePath()
57
    {
58
        $filePath = $this->helper->getFilePath('dirtest', 'test');
59
60
        $this->assertEquals($filePath, 'dirtest/opcache-reset-test.php');
61
    }
62
63
    public function testGenerateUrl()
64
    {
65
        $url = $this->helper->generateUrl('timegryd.io', 'test');
66
67
        $this->assertEquals($url, 'timegryd.io/opcache-reset-test.php');
68
    }
69
70
    /**
71
     * @expectedException RuntimeException
72
     */
73
    public function testCreateFileDirNotExists()
74
    {
75
        $this->helper->createFile('dir-not-existing', 'token');
76
    }
77
78
    public function testCreateFileDirExists()
79
    {
80
        $sourceFile = $this->workspace.DIRECTORY_SEPARATOR.'source_file';
81
82
        file_put_contents($sourceFile, 'SOURCE FILE');
83
84
        $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...
85
            ->kernel
86
            ->shouldReceive('locateResource')
87
            ->andReturn($sourceFile)
88
        ;
89
90
        $return = $this->helper->createFile($this->workspace, 'token');
91
92
        $this->assertInstanceOf(OpcacheResetCommandHelper::class, $return);
93
94
        $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'opcache-reset-token.php';
95
96
        $this->assertFileExists($targetFilePath);
97
        $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
98
    }
99
100
    public function testClean()
101
    {
102
        $fileToDelete1 = $this->workspace.DIRECTORY_SEPARATOR.'opcache-reset-1.php';
103
        touch($fileToDelete1);
104
105
        $fileToDelete2 = $this->workspace.DIRECTORY_SEPARATOR.'opcache-reset-2.php';
106
        touch($fileToDelete2);
107
108
        $fileNotToDelete = $this->workspace.DIRECTORY_SEPARATOR.'filenottodelete.php';
109
        touch($fileNotToDelete);
110
111
        $return = $this->helper->clean($this->workspace);
112
113
        $this->assertFileNotExists($fileToDelete1);
114
        $this->assertFileNotExists($fileToDelete2);
115
        $this->assertFileExists($fileNotToDelete);
116
117
        $this->assertInstanceOf(OpcacheResetCommandHelper::class, $return);
118
    }
119
120
    /**
121
     * @expectedException RuntimeException
122
     */
123
    public function testHandleResponseBadStatusCode()
124
    {
125
        $response = \Mockery::mock(HttpResponse::class);
126
127
        $response
128
            ->shouldReceive('getStatusCode')
129
            ->andReturn('404')
130
        ;
131
132
        $this->helper->handleResponse($response);
133
    }
134
135
    /**
136
     * @expectedException RuntimeException
137
     */
138 View Code Duplication
    public function testHandleResponseBadJson()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $response = \Mockery::mock(HttpResponse::class);
141
142
        $response
143
            ->shouldReceive('getStatusCode')
144
                ->andReturn(Response::HTTP_OK)
145
            ->shouldReceive('getBody')
146
                ->andReturn('bad-json')
147
        ;
148
149
        $this->helper->handleResponse($response);
150
    }
151
152
    /**
153
     * @expectedException InvalidArgumentException
154
     */
155 View Code Duplication
    public function testHandleResponsePropertySuccessMissing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $response = \Mockery::mock(HttpResponse::class);
158
159
        $response
160
            ->shouldReceive('getStatusCode')
161
                ->andReturn(Response::HTTP_OK)
162
            ->shouldReceive('getBody')
163
                ->andReturn('{"message": "failure"}')
164
        ;
165
166
        $this->helper->handleResponse($response);
167
    }
168
169
    /**
170
     * @expectedException InvalidArgumentException
171
     */
172 View Code Duplication
    public function testHandleResponsePropertyMessageMissing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $response = \Mockery::mock(HttpResponse::class);
175
176
        $response
177
            ->shouldReceive('getStatusCode')
178
                ->andReturn(Response::HTTP_OK)
179
            ->shouldReceive('getBody')
180
                ->andReturn('{"success": true}')
181
        ;
182
183
        $this->helper->handleResponse($response);
184
    }
185
186
    /**
187
     * @expectedException RuntimeException
188
     */
189 View Code Duplication
    public function testHandleResponseFailure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        $response = \Mockery::mock(HttpResponse::class);
192
193
        $response
194
            ->shouldReceive('getStatusCode')
195
            ->andReturn(Response::HTTP_OK)
196
            ->shouldReceive('getBody')
197
            ->andReturn('{"success": false, "message": "failure"}')
198
        ;
199
200
        $this->helper->handleResponse($response);
201
    }
202
203 View Code Duplication
    public function testHandleResponseSuccess()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        $response = \Mockery::mock(HttpResponse::class);
206
207
        $response
208
            ->shouldReceive('getStatusCode')
209
                ->andReturn(Response::HTTP_OK)
210
            ->shouldReceive('getBody')
211
                ->andReturn('{"success": "true", "message": "Success"}')
212
        ;
213
214
        $message = $this->helper->handleResponse($response);
215
216
        $this->assertEquals('Success', $message);
217
    }
218
}
219