|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Test suite for the FileManager |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\FileBundle\Tests; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\FileBundle\FileManager; |
|
9
|
|
|
use Graviton\TestBundle\Test\WebTestCase; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Basic functional test for /file |
|
15
|
|
|
* |
|
16
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
17
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
18
|
|
|
* @link http://swisscom.ch |
|
19
|
|
|
*/ |
|
20
|
|
|
class FileManagerTest extends WebTestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var \Gaufrette\Filesystem $fileSystem */ |
|
23
|
|
|
private $fileSystem; |
|
24
|
|
|
|
|
25
|
|
|
/** @var \Graviton\FileBundle\FileDocumentFactory $fileDocumentFactory */ |
|
26
|
|
|
private $fileDocumentFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Initiates mandatory properties |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setUp() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->fileSystem = $this->getMockBuilder('\Gaufrette\Filesystem') |
|
36
|
|
|
->disableOriginalConstructor() |
|
37
|
|
|
->getMock(); |
|
38
|
|
|
|
|
39
|
|
|
$this->fileDocumentFactory = $this->getMockBuilder('\Graviton\FileBundle\FileDocumentFactory') |
|
40
|
|
|
->getMock(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Verifies the correct behavior of has method |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function testHas() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$this->fileSystem |
|
|
|
|
|
|
51
|
|
|
->expects($this->once()) |
|
52
|
|
|
->method('has') |
|
53
|
|
|
->willReturn(true); |
|
54
|
|
|
|
|
55
|
|
|
$manager = new FileManager($this->fileSystem, $this->fileDocumentFactory); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertTrue($manager->has('myKey')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Verifies the correct behavior of read method |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function testRead() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$this->fileSystem |
|
|
|
|
|
|
68
|
|
|
->expects($this->once()) |
|
69
|
|
|
->method('read') |
|
70
|
|
|
->willReturn('myData'); |
|
71
|
|
|
|
|
72
|
|
|
$manager = new FileManager($this->fileSystem, $this->fileDocumentFactory); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals('myData', $manager->read('myKey')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Verifies the correct behavior of read method |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$this->fileSystem |
|
|
|
|
|
|
85
|
|
|
->expects($this->once()) |
|
86
|
|
|
->method('delete') |
|
87
|
|
|
->willReturn(true); |
|
88
|
|
|
|
|
89
|
|
|
$manager = new FileManager($this->fileSystem, $this->fileDocumentFactory); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertTrue($manager->delete('myKey')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Verifies the correct behavior of the FileManager |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testSaveFiles() |
|
100
|
|
|
{ |
|
101
|
|
|
$jsonData = '{ |
|
102
|
|
|
"links": [ |
|
103
|
|
|
{ |
|
104
|
|
|
"$ref": "http://localhost/testcase/readonly/101", |
|
105
|
|
|
"type": "owner" |
|
106
|
|
|
}, |
|
107
|
|
|
{ |
|
108
|
|
|
"$ref": "http://localhost/testcase/readonly/102", |
|
109
|
|
|
"type": "module" |
|
110
|
|
|
} |
|
111
|
|
|
], |
|
112
|
|
|
"metadata": { |
|
113
|
|
|
"action":[{"command":"print"},{"command":"archive"}] |
|
114
|
|
|
} |
|
115
|
|
|
}'; |
|
116
|
|
|
|
|
117
|
|
|
copy(__DIR__ . '/Fixtures/test.txt', sys_get_temp_dir() . '/test.txt'); |
|
118
|
|
|
$file = sys_get_temp_dir() . '/test.txt'; |
|
119
|
|
|
$uploadedFile = new UploadedFile($file, 'test.txt', 'text/plain', 15); |
|
120
|
|
|
$client = $this->createClient(); |
|
121
|
|
|
$client->request( |
|
122
|
|
|
'POST', |
|
123
|
|
|
'/file', |
|
124
|
|
|
[ |
|
125
|
|
|
'metadata' => $jsonData, |
|
126
|
|
|
], |
|
127
|
|
|
[ |
|
128
|
|
|
'upload' => $uploadedFile, |
|
129
|
|
|
] |
|
130
|
|
|
); |
|
131
|
|
|
$response = $client->getResponse(); |
|
132
|
|
|
$location = $response->headers->get('location'); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
|
135
|
|
|
$this->assertContains('/file/', $location); |
|
136
|
|
|
|
|
137
|
|
|
// receive generated file information |
|
138
|
|
|
$client = $this->createClient(); |
|
139
|
|
|
$client->request( |
|
140
|
|
|
'GET', |
|
141
|
|
|
$location, |
|
142
|
|
|
[], |
|
143
|
|
|
[], |
|
144
|
|
|
[ |
|
145
|
|
|
'HTTP_ACCEPT' => 'application/json' |
|
146
|
|
|
] |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$response = $client->getResponse(); |
|
150
|
|
|
$contentArray = json_decode($response->getContent(), true); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertEquals([["command" => "print"], ["command" => "archive"]], $contentArray['metadata']['action']); |
|
153
|
|
|
$this->assertJsonStringEqualsJsonString( |
|
154
|
|
|
'[ |
|
155
|
|
|
{ |
|
156
|
|
|
"$ref": "http://localhost/testcase/readonly/101", |
|
157
|
|
|
"type": "owner" |
|
158
|
|
|
}, |
|
159
|
|
|
{ |
|
160
|
|
|
"$ref": "http://localhost/testcase/readonly/102", |
|
161
|
|
|
"type": "module" |
|
162
|
|
|
} |
|
163
|
|
|
]', |
|
164
|
|
|
json_encode($contentArray['links']) |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
// Check contain data |
|
168
|
|
|
$this->assertArrayHasKey('modificationDate', $contentArray['metadata']); |
|
169
|
|
|
$this->assertArrayHasKey('createDate', $contentArray['metadata']); |
|
170
|
|
|
|
|
171
|
|
|
// Test Metadata, and Remove date. |
|
172
|
|
|
unset($contentArray['metadata']['modificationDate']); |
|
173
|
|
|
unset($contentArray['metadata']['createDate']); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertJsonStringEqualsJsonString( |
|
176
|
|
|
'{ |
|
177
|
|
|
"size":16, |
|
178
|
|
|
"action":[ |
|
179
|
|
|
{"command":"print"}, |
|
180
|
|
|
{"command":"archive"} |
|
181
|
|
|
], |
|
182
|
|
|
"mime":"text\/plain", |
|
183
|
|
|
"filename":"test.txt", |
|
184
|
|
|
"hash":"4f3cbec0e58903d8bdcbd03d283cf43ed49a95d8d8b341ee38c0ba085204e2d5", |
|
185
|
|
|
"additionalProperties":[] |
|
186
|
|
|
}', |
|
187
|
|
|
json_encode($contentArray['metadata']) |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
// clean up |
|
191
|
|
|
$client = $this->createClient(); |
|
192
|
|
|
$client->request( |
|
193
|
|
|
'DELETE', |
|
194
|
|
|
$location |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
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.