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
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..