Completed
Push — ezp25946-migrate_files_to_othe... ( 5995f4...e1d3cb )
by
unknown
40:18 queued 27:23
created

FlysystemTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 248
Duplicated Lines 16.13 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 40
loc 248
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 5

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B testCreate() 0 31 1
A testDelete() 0 5 1
B testLoad() 0 25 1
A testLoadNotFound() 10 10 1
A testExists() 10 10 1
A testExistsNot() 10 10 1
A testGetMimeType() 10 10 1
A testDeleteDirectory() 0 5 1
A testLoadNoTimestamp() 0 17 1
A testCount() 0 18 1
B providerLoadList() 0 38 1
B testLoadList() 0 34 3

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
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\IO\Tests\IOMetadataHandler;
10
11
use eZ\Publish\Core\IO\IOMetadataHandler\Flysystem;
12
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
13
use eZ\Publish\SPI\IO\BinaryFileCreateStruct as SPIBinaryFileCreateStruct;
14
use League\Flysystem\FileNotFoundException;
15
use PHPUnit_Framework_TestCase;
16
use DateTime;
17
18
class FlysystemTest extends PHPUnit_Framework_TestCase
19
{
20
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler|\PHPUnit_Framework_MockObject_MockObject */
21
    private $handler;
22
23
    /** @var \League\Flysystem\FilesystemInterface|\PHPUnit_Framework_MockObject_MockObject */
24
    private $filesystem;
25
26
    public function setUp()
27
    {
28
        $this->filesystem = $this->getMock('League\Flysystem\FilesystemInterface');
29
        $this->handler = new Flysystem($this->filesystem);
30
    }
31
32
    public function testCreate()
33
    {
34
        // good example of bad responsibilities... since create also loads, we test the same thing twice
35
        $spiCreateStruct = new SPIBinaryFileCreateStruct();
36
        $spiCreateStruct->id = 'prefix/my/file.png';
37
        $spiCreateStruct->size = 123;
38
        $spiCreateStruct->mtime = new DateTime('@1307155200');
39
40
        $expectedSpiBinaryFile = new SPIBinaryFile();
41
        $expectedSpiBinaryFile->id = 'prefix/my/file.png';
42
        $expectedSpiBinaryFile->size = 123;
43
        $expectedSpiBinaryFile->mtime = new DateTime('@1307155200');
44
45
        $this->filesystem
46
            ->expects($this->once())
47
            ->method('getMetadata')
48
            ->with($spiCreateStruct->id)
49
            ->will(
50
                $this->returnValue(
51
                    array(
52
                        'timestamp' => 1307155200,
53
                        'size' => 123,
54
                    )
55
                )
56
            );
57
58
        $spiBinaryFile = $this->handler->create($spiCreateStruct);
59
60
        $this->assertInstanceOf('eZ\Publish\SPI\IO\BinaryFile', $spiBinaryFile);
61
        $this->assertEquals($expectedSpiBinaryFile, $spiBinaryFile);
62
    }
63
64
    public function testDelete()
65
    {
66
        $this->filesystem->expects($this->never())->method('delete');
67
        $this->handler->delete('prefix/my/file.png');
68
    }
69
70
    public function testLoad()
71
    {
72
        $expectedSpiBinaryFile = new SPIBinaryFile();
73
        $expectedSpiBinaryFile->id = 'prefix/my/file.png';
74
        $expectedSpiBinaryFile->size = 123;
75
        $expectedSpiBinaryFile->mtime = new DateTime('@1307155200');
76
77
        $this->filesystem
78
            ->expects($this->once())
79
            ->method('getMetadata')
80
            ->with('prefix/my/file.png')
81
            ->will(
82
                $this->returnValue(
83
                    array(
84
                        'timestamp' => 1307155200,
85
                        'size' => 123,
86
                    )
87
                )
88
            );
89
90
        $spiBinaryFile = $this->handler->load('prefix/my/file.png');
91
92
        $this->assertInstanceOf('eZ\Publish\SPI\IO\BinaryFile', $spiBinaryFile);
93
        $this->assertEquals($expectedSpiBinaryFile, $spiBinaryFile);
94
    }
95
96
    /**
97
     * The timestamp index can be unset with some handlers, like AWS/S3.
98
     */
99
    public function testLoadNoTimestamp()
100
    {
101
        $this->filesystem
102
            ->expects($this->once())
103
            ->method('getMetadata')
104
            ->with('prefix/my/file.png')
105
            ->will(
106
                $this->returnValue(
107
                    array(
108
                        'size' => 123,
109
                    )
110
                )
111
            );
112
113
        $spiBinaryFile = $this->handler->load('prefix/my/file.png');
114
        $this->assertNull($spiBinaryFile->mtime);
115
    }
116
117
    /**
118
     * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException
119
     */
120 View Code Duplication
    public function testLoadNotFound()
121
    {
122
        $this->filesystem
123
            ->expects($this->once())
124
            ->method('getMetadata')
125
            ->with('prefix/my/file.png')
126
            ->will($this->throwException(new FileNotFoundException('prefix/my/file.png')));
127
128
        $this->handler->load('prefix/my/file.png');
129
    }
130
131 View Code Duplication
    public function testExists()
132
    {
133
        $this->filesystem
134
            ->expects($this->once())
135
            ->method('has')
136
            ->with('prefix/my/file.png')
137
            ->will($this->returnValue(true));
138
139
        self::assertTrue($this->handler->exists('prefix/my/file.png'));
140
    }
141
142 View Code Duplication
    public function testExistsNot()
143
    {
144
        $this->filesystem
145
            ->expects($this->once())
146
            ->method('has')
147
            ->with('prefix/my/file.png')
148
            ->will($this->returnValue(false));
149
150
        self::assertFalse($this->handler->exists('prefix/my/file.png'));
151
    }
152
153 View Code Duplication
    public function testGetMimeType()
154
    {
155
        $this->filesystem
156
            ->expects($this->once())
157
            ->method('getMimeType')
158
            ->with('file.txt')
159
            ->will($this->returnValue('text/plain'));
160
161
        self::assertEquals('text/plain', $this->handler->getMimeType('file.txt'));
162
    }
163
164
    public function testDeleteDirectory()
165
    {
166
        $this->filesystem->expects($this->never())->method('deleteDir');
167
        $this->handler->deleteDirectory('some/path');
168
    }
169
170
    public function testCount()
171
    {
172
        $this->filesystem
173
            ->expects($this->once())
174
            ->method('listContents')
175
            ->with('', true)
176
            ->will($this->returnValue(
177
                [
178
                    ['path' => 'file1', 'size' => 42],
179
                    ['path' => 'dir1'],
180
                    ['path' => 'file2', 'size' => 892753],
181
                    ['path' => 'dir2'],
182
                    ['path' => 'file3', 'size' => 0],
183
                ]
184
            ));
185
186
        self::assertEquals(3, $this->handler->count());
187
    }
188
189
    public function providerLoadList()
190
    {
191
        return [
192
            [
193
                null,
194
                null,
195
                [
196
                    ['path' => 'file1', 'size' => 42, 'timestamp' => 0],
197
                    ['path' => 'file2', 'size' => 892753, 'timestamp' => 42],
198
                    ['path' => 'file3', 'size' => 0, 'timestamp' => 982374987],
199
                ],
200
            ],
201
            [
202
                0,
203
                0,
204
                [],
205
            ],
206
            [
207
                1,
208
                null,
209
                [
210
                    ['path' => 'file1', 'size' => 42, 'timestamp' => 0],
211
                ],
212
            ],
213
            [
214
                1,
215
                2,
216
                [
217
                    ['path' => 'file3', 'size' => 0, 'timestamp' => 982374987],
218
                ],
219
            ],
220
            [
221
                1,
222
                100,
223
                [],
224
            ],
225
        ];
226
    }
227
228
    /**
229
     * @dataProvider providerLoadList
230
     */
231
    public function testLoadList($limit, $offset, $resultList)
232
    {
233
        $expectedSpiBinaryFiles = [];
234
        foreach ($resultList as $result) {
235
            $expectedSpiBinaryFile = new SPIBinaryFile();
236
            $expectedSpiBinaryFile->id = $result['path'];
237
            $expectedSpiBinaryFile->size = $result['size'];
238
            $expectedSpiBinaryFile->mtime = new DateTime('@' . $result['timestamp']);
239
240
            $expectedSpiBinaryFiles[] = $expectedSpiBinaryFile;
241
        }
242
243
        $this->filesystem
244
            ->expects($this->once())
245
            ->method('listContents')
246
            ->with('', true)
247
            ->will($this->returnValue(
248
                [
249
                    ['path' => 'file1', 'size' => 42, 'timestamp' => 0],
250
                    ['path' => 'dir1'],
251
                    ['path' => 'file2', 'size' => 892753, 'timestamp' => 42],
252
                    ['path' => 'dir2'],
253
                    ['path' => 'file3', 'size' => 0, 'timestamp' => 982374987],
254
                ]
255
            ));
256
257
        $loadedList = $this->handler->loadList($limit, $offset);
258
        $this->assertEquals(count($loadedList), count($resultList));
259
260
        foreach ($loadedList as $key => $spiBinaryFile) {
261
            $this->assertInstanceOf('eZ\Publish\SPI\IO\BinaryFile', $spiBinaryFile);
262
            $this->assertEquals($expectedSpiBinaryFiles[$key], $spiBinaryFile);
263
        }
264
    }
265
}
266