Passed
Branch master (267be1)
by Eugene
03:26
created

DestroyResourceCommandTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 225
Duplicated Lines 42.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 99.28%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 95
loc 225
ccs 137
cts 138
cp 0.9928
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A getCommand() 0 4 1
A getResourceDO() 0 4 1
A getResourceDOMock() 0 10 1
A testDestroyEmptyResource() 0 6 1
A testDestroyResourceThatNotExist() 7 7 1
A testDestroyResourceByPath() 11 11 1
A testDestroyResourceByPathButLeaveOthers() 0 7 1
B testDestroyResourceVersion() 0 32 1
A testDestroyResourceVersionButLeaveOthers() 0 7 1
B testDestroyResourceVariant() 39 39 1
A testDestroyResourceVariantButLeaveOthers() 0 7 1
B testDestroyResourceAllVariantsAndVersions() 38 38 1
A testDestroyResourceAllVariantsAndVersionsButLeaveOthers() 0 7 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
namespace Staticus\Resources\Commands;
3
4
use League\Flysystem\Filesystem;
5
use League\Flysystem\Memory\MemoryAdapter;
6
use Staticus\Resources\File\ResourceDO;
7
8
class DestroyResourceCommandTest extends \PHPUnit_Framework_TestCase
9
{
10
    use AddWrongFilesToDiskTrait;
11
12
    /**
13
     * @var ResourceDO
14
     */
15
    protected $resourceDO;
16
    /**
17
     * @var Filesystem
18
     */
19
    protected $filesystem;
20
21 10
    protected function setUp()
22
    {
23 10
        parent::setUp();
24 10
        $this->resourceDO = new ResourceDO();
25 10
        $this->filesystem = new Filesystem(new MemoryAdapter());
26 10
    }
27
28
    /**
29
     * @return BackupResourceCommand
30
     */
31 10
    public function getCommand(ResourceDO $resourceDO)
32
    {
33 10
        return new DestroyResourceCommand($resourceDO, $this->filesystem);
34
    }
35
36
    /**
37
     * @return ResourceDO
38
     */
39 1
    public function getResourceDO()
40
    {
41 1
        return clone $this->resourceDO;
42
    }
43
44
    /**
45
     * @return ResourceDO
46
     */
47 9
    public function getResourceDOMock()
48
    {
49 9
        $resourceDO = clone $this->resourceDO;
50
51
        return $resourceDO
52 9
            ->setBaseDirectory('testBase')
53 9
            ->setName('testResource')
54 9
            ->setType('testType')
55 9
            ;
56
    }
57
58
    /**
59
     * @expectedException \Staticus\Resources\Exceptions\CommandErrorException
60
     * @expectedExceptionMessage Cannot destroy the empty resource
61
     */
62 1
    public function testDestroyEmptyResource()
63
    {
64 1
        $resourceDO = $this->getResourceDO();
65 1
        $command = $this->getCommand($resourceDO);
66 1
        $command();
67
    }
68
69 1 View Code Duplication
    public function testDestroyResourceThatNotExist()
1 ignored issue
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...
70
    {
71 1
        $resourceDO = $this->getResourceDOMock();
72 1
        $command = $this->getCommand($resourceDO);
73 1
        $result = $command(true);
74 1
        $this->assertEquals($resourceDO, $result);
75 1
    }
76
77 2 View Code Duplication
    public function testDestroyResourceByPath()
1 ignored issue
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...
78
    {
79 2
        $resourceDO = $this->getResourceDOMock();
80 2
        $this->filesystem->put($resourceDO->getFilePath(), '');
81
82 2
        $command = $this->getCommand($resourceDO);
83 2
        $result = $command(true);
84
85 2
        $this->assertEquals($resourceDO, $result);
86 2
        $this->assertFalse($this->filesystem->has($resourceDO->getFilePath(), ''));
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::has() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
87 2
    }
88
89 1
    public function testDestroyResourceByPathButLeaveOthers()
90
    {
91 1
        $resourceDO = $this->getResourceDOMock();
92 1
        $this->addWrongFilesToDisk($resourceDO, '');
93 1
        $this->testDestroyResourceByPath();
94 1
        $this->assertWrongFilesIsStillHere($resourceDO);
95 1
    }
96
97 2
    public function testDestroyResourceVersion()
98
    {
99 2
        $resourceDO = $this->getResourceDOMock();
100 2
        $this->filesystem->put($resourceDO->getFilePath(), '');
101
102 2
        $resourceDO_v1 = clone $resourceDO;
103 2
        $resourceDO_v1->setVersion(1);
104 2
        $this->filesystem->put($resourceDO_v1->getFilePath(), '');
105
106 2
        $resourceDO_v2 = clone $resourceDO;
107 2
        $resourceDO_v2->setVersion(2);
108 2
        $this->filesystem->put($resourceDO_v2->getFilePath(), '');
109
110 2
        $resourceDO_var = clone $resourceDO;
111 2
        $resourceDO_var->setVariant('variant');
112 2
        $this->filesystem->put($resourceDO_var->getFilePath(), '');
113
114 2
        $resourceDO_var_v2 = clone $resourceDO_var;
115 2
        $resourceDO_var_v2->setVersion(2);
116 2
        $this->filesystem->put($resourceDO_var_v2->getFilePath(), '');
117
118 2
        $command = $this->getCommand($resourceDO_v1);
119 2
        $result = $command();
120
121 2
        $this->assertEquals($resourceDO_v1, $result);
122 2
        $this->assertFalse($this->filesystem->has($resourceDO_v1->getFilePath()));
123
124 2
        $this->assertTrue($this->filesystem->has($resourceDO_v2->getFilePath()));
125 2
        $this->assertTrue($this->filesystem->has($resourceDO->getFilePath()));
126 2
        $this->assertTrue($this->filesystem->has($resourceDO_var->getFilePath()));
127 2
        $this->assertTrue($this->filesystem->has($resourceDO_var_v2->getFilePath()));
128 2
    }
129
130 1
    public function testDestroyResourceVersionButLeaveOthers()
131
    {
132 1
        $resourceDO = $this->getResourceDOMock();
133 1
        $this->addWrongFilesToDisk($resourceDO, '');
134 1
        $this->testDestroyResourceVersion();
135 1
        $this->assertWrongFilesIsStillHere($resourceDO);
136 1
    }
137
138 2 View Code Duplication
    public function testDestroyResourceVariant()
1 ignored issue
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 2
        $resourceDO = $this->getResourceDOMock();
141 2
        $this->filesystem->put($resourceDO->getFilePath(), '');
142
143 2
        $resourceDO_v2 = clone $resourceDO;
144 2
        $resourceDO_v2->setVersion(2);
145 2
        $this->filesystem->put($resourceDO_v2->getFilePath(), '');
146
147 2
        $resourceDO_var1 = clone $resourceDO;
148 2
        $resourceDO_var1->setVariant('1');
149 2
        $this->filesystem->put($resourceDO_var1->getFilePath(), '');
150
151
        // variant version must be deleted too
152 2
        $resourceDO_var1_v1 = clone $resourceDO_var1;
153 2
        $resourceDO_var1_v1->setVersion(1);
154 2
        $this->filesystem->put($resourceDO_var1_v1->getFilePath(), '');
155
156 2
        $resourceDO_var2 = clone $resourceDO;
157 2
        $resourceDO_var2->setVariant('2');
158 2
        $this->filesystem->put($resourceDO_var2->getFilePath(), '');
159
160 2
        $resourceDO_var2_v1 = clone $resourceDO_var2;
161 2
        $resourceDO_var2_v1->setVersion(2);
162 2
        $this->filesystem->put($resourceDO_var2_v1->getFilePath(), '');
163
164 2
        $command = $this->getCommand($resourceDO_var1);
165 2
        $result = $command();
166
167 2
        $this->assertEquals($resourceDO_var1, $result);
168
169 2
        $this->assertFalse($this->filesystem->has($resourceDO_var1->getFilePath()));
170 2
        $this->assertFalse($this->filesystem->has($resourceDO_var1_v1->getFilePath()));
171
172 2
        $this->assertTrue($this->filesystem->has($resourceDO_v2->getFilePath()));
173 2
        $this->assertTrue($this->filesystem->has($resourceDO_var2->getFilePath()));
174 2
        $this->assertTrue($this->filesystem->has($resourceDO_var2_v1->getFilePath()));
175 2
        $this->assertTrue($this->filesystem->has($resourceDO->getFilePath()));
176 2
    }
177
178 1
    public function testDestroyResourceVariantButLeaveOthers()
179
    {
180 1
        $resourceDO = $this->getResourceDOMock();
181 1
        $this->addWrongFilesToDisk($resourceDO, '');
182 1
        $this->testDestroyResourceVariant();
183 1
        $this->assertWrongFilesIsStillHere($resourceDO);
184 1
    }
185
186 2 View Code Duplication
    public function testDestroyResourceAllVariantsAndVersions()
1 ignored issue
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...
187
    {
188 2
        $resourceDO = $this->getResourceDOMock();
189 2
        $this->filesystem->put($resourceDO->getFilePath(), '');
190
191 2
        $resourceDO_v2 = clone $resourceDO;
192 2
        $resourceDO_v2->setVersion(2);
193 2
        $this->filesystem->put($resourceDO_v2->getFilePath(), '');
194
195 2
        $resourceDO_var1 = clone $resourceDO;
196 2
        $resourceDO_var1->setVariant('1');
197 2
        $this->filesystem->put($resourceDO_var1->getFilePath(), '');
198
199
        // variant version must be deleted too
200 2
        $resourceDO_var1_v1 = clone $resourceDO_var1;
201 2
        $resourceDO_var1_v1->setVersion(1);
202 2
        $this->filesystem->put($resourceDO_var1_v1->getFilePath(), '');
203
204 2
        $resourceDO_var2 = clone $resourceDO;
205 2
        $resourceDO_var2->setVariant('2');
206 2
        $this->filesystem->put($resourceDO_var2->getFilePath(), '');
207
208 2
        $resourceDO_var2_v1 = clone $resourceDO_var2;
209 2
        $resourceDO_var2_v1->setVersion(2);
210 2
        $this->filesystem->put($resourceDO_var2_v1->getFilePath(), '');
211
212 2
        $command = $this->getCommand($resourceDO);
213 2
        $result = $command();
214
215 2
        $this->assertEquals($resourceDO, $result);
216
217 2
        $this->assertFalse($this->filesystem->has($resourceDO_var1->getFilePath()));
218 2
        $this->assertFalse($this->filesystem->has($resourceDO_var1_v1->getFilePath()));
219 2
        $this->assertFalse($this->filesystem->has($resourceDO_v2->getFilePath()));
220 2
        $this->assertFalse($this->filesystem->has($resourceDO_var2->getFilePath()));
221 2
        $this->assertFalse($this->filesystem->has($resourceDO_var2_v1->getFilePath()));
222 2
        $this->assertFalse($this->filesystem->has($resourceDO->getFilePath()));
223 2
    }
224
225 1
    public function testDestroyResourceAllVariantsAndVersionsButLeaveOthers()
226
    {
227 1
        $resourceDO = $this->getResourceDOMock();
228 1
        $this->addWrongFilesToDisk($resourceDO, '');
229 1
        $this->testDestroyResourceAllVariantsAndVersions();
230 1
        $this->assertWrongFilesIsStillHere($resourceDO);
231 1
    }
232
}
233