FindResourceOptionsCommandTest::getCommand()   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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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\File\ResourceDO;
7
use Staticus\Resources\ResourceDOAbstract;
8
9
require_once 'AddWrongFilesToDiskHelper.php';
10
11
class FindResourceOptionsCommandTest extends \PHPUnit_Framework_TestCase
12
{
13
    const BASE_DIR = '/this/is/a/test';
14
15
    /**
16
     * @var ResourceDO
17
     */
18
    protected $resourceDO;
19
    /**
20
     * @var Filesystem
21
     */
22
    protected $filesystem;
23
    /**
24
     * @var AddWrongFilesToDiskHelper
25
     */
26
    protected $wrongFiles;
27
28 6
    protected function setUp()
29
    {
30 6
        parent::setUp();
31 6
        $this->resourceDO = new ResourceDO();
32 6
        $this->filesystem = new Filesystem(new MemoryAdapter());
33 6
        $this->wrongFiles = new AddWrongFilesToDiskHelper($this->filesystem, $this);
34 6
    }
35
36
    /**
37
     * @return FindResourceOptionsCommand
38
     */
39 5
    public function getCommand(ResourceDO $resourceDO)
40
    {
41 5
        return new FindResourceOptionsCommand($resourceDO, $this->filesystem);
42
    }
43
44
    /**
45
     * @return ResourceDO
46
     */
47 5
    public function getResourceDO()
48 1
    {
49 5
        return clone $this->resourceDO;
50
    }
51
52
    /**
53
     * @expectedException \Staticus\Resources\Exceptions\CommandErrorException
54
     * @expectedExceptionMessage Can not look for options: resource is empty
55
     */
56 1
    public function testFindEmptyResource()
57
    {
58 1
        $resourceDO = $this->getResourceDO();
59 1
        $command = $this->getCommand($resourceDO);
60 1
        $command();
61
    }
62
63 1
    public function testFindResourceDataProvider()
64
    {
65
        return [
66
            //  $namespace, $type, $variant, $version, $name, $nameAlternative, $author
67 1
            [ '', 'jpg', 'def', '0', 'Aloha', '', 'user-puser' ],
68 1
            [ 'space', 'txt', 'def', '0', 'Hello', '', 'user-cucuser' ],
69 1
            [ 'space', 'txt', 'def', '0', 'Hello', 'Привет', 'user-vantuser' ],
70 1
            [ 'my/long/space', 'jpg', 'varvarvar', '3', 'Aloha', '$%^&*O', 'user-shmuser' ],
71 1
        ];
72
    }
73
74
    /**
75
     * @dataProvider testFindResourceDataProvider
76
     */
77 4
    public function testFindResourceMock($namespace, $type, $variant, $version, $name, $nameAlternative, $author)
78
    {
79 4
        $content = 'just a test';
80 4
        $resourceDO = $this->prepareResource($namespace, $type, $variant, $version, $name, $nameAlternative, $author);
81 4
        $uuid = $resourceDO->getUuid();
82
83
        // SAVE CURRENT
84 4
        $filePath = $resourceDO->getFilePath();
85 4
        $this->filesystem->put($filePath, $content);
86 4
        $this->assertTrue($this->filesystem->has($filePath));
87
88
        // SAVE ANOTHER VERSION
89 4
        $resourceDOVersion = clone $resourceDO;
90 4
        $version2 = (string)($version + 1);
91 4
        $resourceDOVersion->setVersion($version2);
92
93 4
        $filePath = $resourceDOVersion->getFilePath();
94 4
        $this->filesystem->put($filePath, $content);
95 4
        $this->assertTrue($this->filesystem->has($filePath));
96
97
        // SAVE ANOTHER VARIANT
98 4
        $resourceDOVariant = clone $resourceDO;
99 4
        $variant2 = $variant . '_second';
100 4
        $resourceDOVariant->setVariant($variant2);
101
102 4
        $filePath = $resourceDOVariant->getFilePath();
103 4
        $this->filesystem->put($filePath, $content);
104 4
        $this->assertTrue($this->filesystem->has($filePath));
105
106
        // SAVE WRONG FILES
107 4
        $this->wrongFiles->create($resourceDO, $content);
108
109 4
        $modelBaseDir = substr(self::BASE_DIR, 1, 100);
110 4
        $shardVariant = substr($variant, 0, ResourceDOAbstract::SHARD_SLICE_LENGTH);
111 4
        $shardFilename = substr($uuid, 0, ResourceDOAbstract::SHARD_SLICE_LENGTH);
112 4
        $namespacePath = $namespace ? $namespace . '/' : '';
113
        $model = [
114
            [
115 4
                ResourceDOAbstract::TOKEN_TYPE => $type,
116 4
                'visibility' => 'public',
117 4
                'path' => $modelBaseDir . '/' . $namespacePath . $type . '/' . $shardVariant . '/' . $variant . '/' . $version . '/' . $shardFilename . '/' . $uuid . '.' . $type,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 178 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
118 4
                'dirname' => $modelBaseDir . '/' . $namespacePath . $type . '/' . $shardVariant . '/' . $variant . '/' . $version . '/' . $shardFilename,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 153 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
119 4
                'basename' => $uuid . '.' . $type,
120 4
                'extension' => $type,
121 4
                'filename' => $uuid,
122 4
                'directory_relative' => $type . '/' . $shardVariant . '/' . $variant . '/' . $version . '/' . $shardFilename,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
123 4
                ResourceDOAbstract::TOKEN_SHARD_VARIANT => $shardVariant,
124 4
                ResourceDOAbstract::TOKEN_VARIANT => $variant,
125 4
                ResourceDOAbstract::TOKEN_VERSION => $version,
126 4
                ResourceDOAbstract::TOKEN_SHARD_FILENAME => $shardFilename,
127 4
            ],
128
            [
129 4
                ResourceDOAbstract::TOKEN_TYPE => $type,
130 4
                'visibility' => 'public',
131 4
                'path' => $modelBaseDir . '/' . $namespacePath . $type . '/' . $shardVariant . '/' . $variant . '/' . $version2 . '/' . $shardFilename . '/' . $uuid . '.' . $type,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 179 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
132 4
                'dirname' => $modelBaseDir . '/' . $namespacePath . $type . '/' . $shardVariant . '/' . $variant . '/' . $version2 . '/' . $shardFilename,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 154 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
133 4
                'basename' => $uuid . '.' . $type,
134 4
                'extension' => $type,
135 4
                'filename' => $uuid,
136 4
                'directory_relative' => $type . '/' . $shardVariant . '/' . $variant . '/' . $version2 . '/' . $shardFilename,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
137 4
                ResourceDOAbstract::TOKEN_SHARD_VARIANT => $shardVariant,
138 4
                ResourceDOAbstract::TOKEN_VARIANT => $variant,
139 4
                ResourceDOAbstract::TOKEN_VERSION => $version2,
140 4
                ResourceDOAbstract::TOKEN_SHARD_FILENAME => $shardFilename,
141 4
            ],
142
            [
143 4
                ResourceDOAbstract::TOKEN_TYPE => $type,
144 4
                'visibility' => 'public',
145 4
                'path' => $modelBaseDir . '/' . $namespacePath . $type . '/' . $shardVariant . '/' . $variant2 . '/' . $version . '/' . $shardFilename . '/' . $uuid . '.' . $type,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 179 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
146 4
                'dirname' => $modelBaseDir . '/' . $namespacePath . $type . '/' . $shardVariant . '/' . $variant2 . '/' . $version . '/' . $shardFilename,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 154 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
147 4
                'basename' => $uuid . '.' . $type,
148 4
                'extension' => $type,
149 4
                'filename' => $uuid,
150 4
                'directory_relative' => $type . '/' . $shardVariant . '/' . $variant2 . '/' . $version . '/' . $shardFilename,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
151 4
                ResourceDOAbstract::TOKEN_SHARD_VARIANT => $shardVariant,
152 4
                ResourceDOAbstract::TOKEN_VARIANT => $variant2,
153 4
                ResourceDOAbstract::TOKEN_VERSION => $version,
154 4
                ResourceDOAbstract::TOKEN_SHARD_FILENAME => $shardFilename,
155 4
            ],
156 4
        ];
157
158 4
        $command = $this->getCommand($resourceDO);
159 4
        $result = $command();
160 4
        foreach ($result as &$item) {
161 4
            $this->assertArrayHasKey('size', $item);
162 4
            $this->assertArrayHasKey('timestamp', $item);
163 4
            unset($item['size'], $item['timestamp']);
164 4
        }
165
166 4
        $this->assertEquals($model, $result);
167 4
    }
168
169 4
    protected function prepareResource($namespace, $type, $variant, $version, $name, $nameAlternative, $author)
170
    {
171 4
        $resourceDO = $this->getResourceDO();
172 4
        $resourceDO->setBaseDirectory(self::BASE_DIR)
173 4
            ->setNamespace($namespace)
174 4
            ->setType($type)
175 4
            ->setVariant($variant)
176 4
            ->setVersion($version)
177 4
            ->setName($name)
178 4
            ->setNameAlternative($nameAlternative)
179 4
            ->setAuthor($author);
180
181 4
        return $resourceDO;
182
    }
183
184
185
}