getResourceDO()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace Staticus\Resources\Commands;
3
4
use League\Flysystem\Filesystem;
5
use League\Flysystem\Memory\MemoryAdapter;
6
use Staticus\Resources\Image\ResourceImageDO;
7
use Staticus\Resources\Png\ResourceDO;
8
9
require_once 'AddWrongFilesToDiskHelper.php';
10
11
class DeleteImageSizesResourceCommandTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var ResourceImageDO
15
     */
16
    protected $resourceDO;
17
    /**
18
     * @var Filesystem
19
     */
20
    protected $filesystem;
21
    /**
22
     * @var AddWrongFilesToDiskHelper
23
     */
24
    protected $wrongFiles;
25
26 3
    protected function setUp()
27
    {
28 3
        parent::setUp();
29 3
        $this->resourceDO = new ResourceDO();
30 3
        $this->filesystem = new Filesystem(new MemoryAdapter());
31 3
        $this->wrongFiles = new AddWrongFilesToDiskHelper($this->filesystem, $this);
32 3
    }
33
34
    /**
35
     * @return DeleteImageSizesResourceCommand
36
     */
37 3
    public function getCommand(ResourceImageDO $resourceDO)
38
    {
39 3
        return new DeleteImageSizesResourceCommand($resourceDO, $this->filesystem);
40
    }
41
42
    /**
43
     * @return ResourceImageDO
44
     */
45 1
    public function getResourceDO()
46
    {
47 1
        return clone $this->resourceDO;
48
    }
49
50
    /**
51
     * @return ResourceImageDO
52
     */
53 2
    public function getResourceDOMock()
54
    {
55 2
        $resourceDO = clone $this->resourceDO;
56
57
        return $resourceDO
58 2
            ->setBaseDirectory('testBase')
59 2
            ->setName('testResource')
60 2
            ->setType('testType')
61 2
            ;
62
    }
63
64
    /**
65
     * @expectedException \Staticus\Resources\Exceptions\CommandErrorException
66
     * @expectedExceptionMessage Can not look for options: resource is empty
67
     */
68 1
    public function testDeleteEmptyResourceSizes()
69
    {
70 1
        $resourceDO = $this->getResourceDO();
71 1
        $command = $this->getCommand($resourceDO);
72 1
        $command();
73
    }
74
75 1
    public function testDeleteMockResourceNotExistSizes()
76
    {
77 1
        $resourceDO = $this->getResourceDOMock();
78 1
        $command = $this->getCommand($resourceDO);
79 1
        $result = $command();
80 1
        $this->assertTrue($result);
81 1
    }
82
83 1
    public function testDeleteMockResourceExistsSizes()
84
    {
85 1
        $resourceDO = $this->getResourceDOMock();
86 1
        $this->filesystem->put($resourceDO->getFilePath(), '');
87
88 1
        $resourceDOSize10x11 = $this->getResourceDOMock();
89 1
        $resourceDOSize10x11->setWidth(10);
90 1
        $resourceDOSize10x11->setHeight(11);
91 1
        $this->filesystem->put($resourceDOSize10x11->getFilePath(), '');
92
93 1
        $resourceDOSize20x21 = $this->getResourceDOMock();
94 1
        $resourceDOSize20x21->setWidth(20);
95 1
        $resourceDOSize20x21->setHeight(21);
96 1
        $this->filesystem->put($resourceDOSize20x21->getFilePath(), '');
97
98 1
        $yetAnotherWrongDO = clone $resourceDO;
99 1
        $yetAnotherWrongDO->setWidth(998);
100 1
        $yetAnotherWrongDO->setHeight(999);
101 1
        $this->wrongFiles->create($resourceDO, 'Wrong1');
102 1
        $this->wrongFiles->create($resourceDOSize10x11, 'Wrong2');
103 1
        $this->wrongFiles->create($resourceDOSize20x21, 'Wrong3');
104 1
        $this->wrongFiles->create($yetAnotherWrongDO, 'Wrong4');
105
106 1
        $modelFiles = $this->filesystem->listContents('/', true);
107
108
        // Make the expected model looks like filesystem after the command execution
109
        // With this model we will be sure that other files have not been deleted
110 1
        unset($modelFiles[56], $modelFiles[57]);
111 1
        $modelFiles[55] = [
112 1
            'type' => 'dir',
113 1
            'path' => 'testBase/png/def/def/0/c9f/20x21',
114 1
            'dirname' => 'testBase/png/def/def/0/c9f',
115 1
            'basename' => '20x21',
116 1
            'filename' => '20x21',
117
        ];
118
119 1
        $command = $this->getCommand($resourceDO);
120 1
        $result = $command();
121 1
        $this->assertTrue($result);
122 1
        $this->assertFalse($this->filesystem->has($resourceDOSize10x11->getFilePath()));
123 1
        $this->assertFalse($this->filesystem->has($resourceDOSize20x21->getFilePath()));
124 1
        $result = $this->filesystem->listContents('/', true);
125 1
        $this->assertEquals($modelFiles, $result);
126 1
    }
127
}
128