|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) 2018 Dennis Birkholz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* $Id$ |
|
7
|
|
|
* Author: $Format:%an <%ae>, %ai$ |
|
8
|
|
|
* Committer: $Format:%cn <%ce>, %ci$ |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace morgue\archive; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @coversDefaultClass \morgue\archive\ArchiveEntry |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ArchiveEntryTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testImmutability() |
|
21
|
|
|
{ |
|
22
|
|
|
$old = new ArchiveEntry('name'); |
|
23
|
|
|
|
|
24
|
|
|
$value = 'new name'; |
|
25
|
|
|
$this->assertNotSame($value, $old->getName()); |
|
26
|
|
|
$new = $old->withName($value); |
|
27
|
|
|
$this->assertNotSame($old, $new); |
|
28
|
|
|
$this->assertNotSame($old->getName(), $new->getName()); |
|
29
|
|
|
$this->assertSame($value, $new->getName()); |
|
30
|
|
|
$old = $new; |
|
31
|
|
|
|
|
32
|
|
|
$value = 12345; |
|
33
|
|
|
$this->assertNotSame($value, $old->getUncompressedSize()); |
|
34
|
|
|
$new = $old->withUncompressedSize($value); |
|
35
|
|
|
$this->assertNotSame($old, $new); |
|
36
|
|
|
$this->assertNotSame($old->getUncompressedSize(), $new->getUncompressedSize()); |
|
37
|
|
|
$this->assertSame($value, $new->getUncompressedSize()); |
|
38
|
|
|
$old = $new; |
|
39
|
|
|
|
|
40
|
|
|
$value = 23456; |
|
41
|
|
|
$this->assertNotSame($value, $old->getSourceSize()); |
|
42
|
|
|
$new = $old->withSourceSize($value); |
|
43
|
|
|
$this->assertNotSame($old, $new); |
|
44
|
|
|
$this->assertNotSame($old->getSourceSize(), $new->getSourceSize()); |
|
45
|
|
|
$this->assertSame($value, $new->getSourceSize()); |
|
46
|
|
|
$old = $new; |
|
47
|
|
|
|
|
48
|
|
|
$value = 34567; |
|
49
|
|
|
$this->assertNotSame($value, $old->getTargetSize()); |
|
50
|
|
|
$new = $old->withTargetSize($value); |
|
51
|
|
|
$this->assertNotSame($old, $new); |
|
52
|
|
|
$this->assertNotSame($old->getTargetSize(), $new->getTargetSize()); |
|
53
|
|
|
$this->assertSame($value, $new->getTargetSize()); |
|
54
|
|
|
$old = $new; |
|
55
|
|
|
|
|
56
|
|
|
$value = new \DateTimeImmutable(); |
|
57
|
|
|
$this->assertNotSame($value, $old->getCreationTime()); |
|
58
|
|
|
$new = $old->withCreationTime($value); |
|
59
|
|
|
$this->assertNotSame($old, $new); |
|
60
|
|
|
$this->assertNotSame($old->getCreationTime(), $new->getCreationTime()); |
|
61
|
|
|
$this->assertSame($value, $new->getCreationTime()); |
|
62
|
|
|
$old = $new; |
|
63
|
|
|
|
|
64
|
|
|
$value = new \DateTimeImmutable(); |
|
65
|
|
|
$this->assertNotSame($value, $old->getModificationTime()); |
|
66
|
|
|
$new = $old->withModificationTime($value); |
|
67
|
|
|
$this->assertNotSame($old, $new); |
|
68
|
|
|
$this->assertNotSame($old->getModificationTime(), $new->getModificationTime()); |
|
69
|
|
|
$this->assertSame($value, $new->getModificationTime()); |
|
70
|
|
|
$old = $new; |
|
71
|
|
|
|
|
72
|
|
|
$value = 45678; |
|
73
|
|
|
$this->assertNotSame($value, $old->getChecksumCrc32()); |
|
74
|
|
|
$new = $old->withChecksumCrc32($value); |
|
75
|
|
|
$this->assertNotSame($old, $new); |
|
76
|
|
|
$this->assertNotSame($old->getChecksumCrc32(), $new->getChecksumCrc32()); |
|
77
|
|
|
$this->assertSame($value, $new->getChecksumCrc32()); |
|
78
|
|
|
$old = $new; |
|
79
|
|
|
|
|
80
|
|
|
$value = 'another comment'; |
|
81
|
|
|
$this->assertNotSame($value, $old->getComment()); |
|
82
|
|
|
$new = $old->withComment($value); |
|
83
|
|
|
$this->assertNotSame($old, $new); |
|
84
|
|
|
$this->assertNotSame($old->getComment(), $new->getComment()); |
|
85
|
|
|
$this->assertSame($value, $new->getComment()); |
|
86
|
|
|
$old = $new; |
|
87
|
|
|
|
|
88
|
|
|
$value = COMPRESSION_METHOD_LZMA; |
|
89
|
|
|
$this->assertNotSame($value, $old->getSourceCompressionMethod()); |
|
90
|
|
|
$new = $old->withSourceCompressionMethod($value); |
|
91
|
|
|
$this->assertNotSame($old, $new); |
|
92
|
|
|
$this->assertNotSame($old->getSourceCompressionMethod(), $new->getSourceCompressionMethod()); |
|
93
|
|
|
$this->assertSame($value, $new->getSourceCompressionMethod()); |
|
94
|
|
|
$old = $new; |
|
95
|
|
|
|
|
96
|
|
|
$value = COMPRESSION_METHOD_BZIP2; |
|
97
|
|
|
$this->assertNotSame($value, $old->getTargetCompressionMethod()); |
|
98
|
|
|
$new = $old->withTargetCompressionMethod($value); |
|
99
|
|
|
$this->assertNotSame($old, $new); |
|
100
|
|
|
$this->assertNotSame($old->getTargetCompressionMethod(), $new->getTargetCompressionMethod()); |
|
101
|
|
|
$this->assertSame($value, $new->getTargetCompressionMethod()); |
|
102
|
|
|
$old = $new; |
|
103
|
|
|
|
|
104
|
|
|
$value = 56789; |
|
105
|
|
|
$this->assertNotSame($value, $old->getDosAttributes()); |
|
106
|
|
|
$new = $old->withDosAttributes($value); |
|
107
|
|
|
$this->assertNotSame($old, $new); |
|
108
|
|
|
$this->assertNotSame($old->getDosAttributes(), $new->getDosAttributes()); |
|
109
|
|
|
$this->assertSame($value, $new->getDosAttributes()); |
|
110
|
|
|
$old = $new; |
|
111
|
|
|
|
|
112
|
|
|
$value = 67890; |
|
113
|
|
|
$this->assertNotSame($value, $old->getUnixAttributes()); |
|
114
|
|
|
$new = $old->withUnixAttributes($value); |
|
115
|
|
|
$this->assertNotSame($old, $new); |
|
116
|
|
|
$this->assertNotSame($old->getUnixAttributes(), $new->getUnixAttributes()); |
|
117
|
|
|
$this->assertSame($value, $new->getUnixAttributes()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @covers ::getSourcePath() |
|
122
|
|
|
* @covers ::withSourcePath() |
|
123
|
|
|
* @covers ::importStat() |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function testSourcePath() |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$filename = __DIR__ . '/ipsum.txt'; |
|
128
|
|
|
$stat = \stat($filename); |
|
129
|
|
|
|
|
130
|
|
|
$entryEmpty = new ArchiveEntry(\basename($filename)); |
|
131
|
|
|
$entry = $entryEmpty->withSourcePath($filename, COMPRESSION_METHOD_STORE); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertNotSame($entryEmpty, $entry); |
|
134
|
|
|
$this->assertSame($filename, $entry->getSourcePath()); |
|
135
|
|
|
$this->assertNull($entry->getSourceStream()); |
|
136
|
|
|
$this->assertNull($entry->getSourceString()); |
|
137
|
|
|
$this->assertSame(COMPRESSION_METHOD_STORE, $entry->getSourceCompressionMethod()); |
|
138
|
|
|
$this->assertSame($stat['size'], $entry->getSourceSize()); |
|
139
|
|
|
$this->assertSame($stat['size'], $entry->getUncompressedSize()); |
|
140
|
|
|
$this->assertSame($stat['mode'], $entry->getUnixAttributes()); |
|
141
|
|
|
$this->assertSame($stat['ctime'], $entry->getCreationTime()->getTimestamp()); |
|
142
|
|
|
$this->assertSame($stat['mtime'], $entry->getModificationTime()->getTimestamp()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @covers ::getSourcePath() |
|
147
|
|
|
* @covers ::withSourcePath() |
|
148
|
|
|
* @covers ::importStat() |
|
149
|
|
|
*/ |
|
150
|
|
|
public function testSourcePathDirectory() |
|
151
|
|
|
{ |
|
152
|
|
|
$filename = __DIR__; |
|
153
|
|
|
$stat = \stat($filename); |
|
154
|
|
|
|
|
155
|
|
|
$entryEmpty = new ArchiveEntry(\basename($filename)); |
|
156
|
|
|
$entry = $entryEmpty->withSourcePath($filename); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertNotSame($entryEmpty, $entry); |
|
159
|
|
|
$this->assertSame($filename, $entry->getSourcePath()); |
|
160
|
|
|
$this->assertNull($entry->getSourceStream()); |
|
161
|
|
|
$this->assertNull($entry->getSourceString()); |
|
162
|
|
|
$this->assertSame(COMPRESSION_METHOD_STORE, $entry->getSourceCompressionMethod()); |
|
163
|
|
|
$this->assertSame(COMPRESSION_METHOD_STORE, $entry->getTargetCompressionMethod()); |
|
164
|
|
|
$this->assertSame(0, $entry->getSourceSize()); |
|
165
|
|
|
$this->assertSame(0, $entry->getTargetSize()); |
|
166
|
|
|
$this->assertSame(0, $entry->getUncompressedSize()); |
|
167
|
|
|
$this->assertSame($stat['mode'], $entry->getUnixAttributes()); |
|
168
|
|
|
$this->assertSame($stat['ctime'], $entry->getCreationTime()->getTimestamp()); |
|
169
|
|
|
$this->assertSame($stat['mtime'], $entry->getModificationTime()->getTimestamp()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @covers ::getSourceStream() |
|
174
|
|
|
* @covers ::withSourceStream() |
|
175
|
|
|
* @covers ::importStat() |
|
176
|
|
|
*/ |
|
177
|
|
View Code Duplication |
public function testSourceStream() |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$filename = __DIR__ . '/ipsum.txt'; |
|
180
|
|
|
$stream = \fopen($filename, 'r'); |
|
181
|
|
|
$stat = \fstat($stream); |
|
182
|
|
|
|
|
183
|
|
|
$entryEmpty = new ArchiveEntry(\basename($filename)); |
|
184
|
|
|
$entry = $entryEmpty->withSourceStream($stream, COMPRESSION_METHOD_STORE); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertNotSame($entryEmpty, $entry); |
|
187
|
|
|
$this->assertNull($entry->getSourcePath()); |
|
188
|
|
|
$this->assertSame($stream, $entry->getSourceStream()); |
|
189
|
|
|
$this->assertNull($entry->getSourceString()); |
|
190
|
|
|
$this->assertSame(COMPRESSION_METHOD_STORE, $entry->getSourceCompressionMethod()); |
|
191
|
|
|
$this->assertSame($stat['size'], $entry->getSourceSize()); |
|
192
|
|
|
$this->assertSame($stat['size'], $entry->getUncompressedSize()); |
|
193
|
|
|
$this->assertSame($stat['mode'], $entry->getUnixAttributes()); |
|
194
|
|
|
$this->assertSame($stat['ctime'], $entry->getCreationTime()->getTimestamp()); |
|
195
|
|
|
$this->assertSame($stat['mtime'], $entry->getModificationTime()->getTimestamp()); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @covers ::getSourceString() |
|
200
|
|
|
* @covers ::withSourceString() |
|
201
|
|
|
*/ |
|
202
|
|
|
public function testSourceString() |
|
203
|
|
|
{ |
|
204
|
|
|
$filename = __DIR__ . '/ipsum.txt'; |
|
205
|
|
|
$string = \file_get_contents($filename); |
|
206
|
|
|
|
|
207
|
|
|
$entryEmpty = new ArchiveEntry(\basename($filename)); |
|
208
|
|
|
$timestamp1 = \time(); |
|
209
|
|
|
$entry = $entryEmpty->withSourceString($string, COMPRESSION_METHOD_STORE); |
|
210
|
|
|
$timestamp2 = \time(); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertNotSame($entryEmpty, $entry); |
|
213
|
|
|
$this->assertNull($entry->getSourcePath()); |
|
214
|
|
|
$this->assertNull($entry->getSourceStream()); |
|
215
|
|
|
$this->assertSame($string, $entry->getSourceString()); |
|
216
|
|
|
$this->assertSame(COMPRESSION_METHOD_STORE, $entry->getSourceCompressionMethod()); |
|
217
|
|
|
$this->assertSame(\strlen($string), $entry->getSourceSize()); |
|
218
|
|
|
$this->assertSame(\strlen($string), $entry->getUncompressedSize()); |
|
219
|
|
|
$this->assertNull($entry->getUnixAttributes()); |
|
220
|
|
|
$this->assertTrue($timestamp1 <= $entry->getCreationTime()->getTimestamp() && $entry->getCreationTime()->getTimestamp() <= $timestamp2); |
|
221
|
|
|
$this->assertTrue($timestamp1 <= $entry->getModificationTime()->getTimestamp() && $entry->getCreationTime()->getTimestamp() <= $timestamp2); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @covers ::withSourcePath() |
|
226
|
|
|
* @expectedException \InvalidArgumentException |
|
227
|
|
|
*/ |
|
228
|
|
|
public function testSourcePathExceptionInvalidFile() |
|
229
|
|
|
{ |
|
230
|
|
|
$entry = new ArchiveEntry('entry'); |
|
231
|
|
|
$entry->withSourcePath(__DIR__ . '/non-existing-file'); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @covers ::withSourcePath() |
|
236
|
|
|
* @expectedException \InvalidArgumentException |
|
237
|
|
|
*/ |
|
238
|
|
|
public function testSourcePathExceptionPathSet() |
|
239
|
|
|
{ |
|
240
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
241
|
|
|
$entry = $entryEmpty->withSourcePath(__DIR__ . '/ipsum.txt'); |
|
242
|
|
|
$entry->withSourcePath(__FILE__); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @covers ::withSourcePath() |
|
247
|
|
|
* @expectedException \InvalidArgumentException |
|
248
|
|
|
*/ |
|
249
|
|
|
public function testSourcePathExceptionStreamSet() |
|
250
|
|
|
{ |
|
251
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
252
|
|
|
$entry = $entryEmpty->withSourceStream(\fopen('php://memory', 'r')); |
|
253
|
|
|
$entry->withSourcePath(__FILE__); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @covers ::withSourcePath() |
|
258
|
|
|
* @expectedException \InvalidArgumentException |
|
259
|
|
|
*/ |
|
260
|
|
|
public function testSourcePathExceptionStringSet() |
|
261
|
|
|
{ |
|
262
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
263
|
|
|
$entry = $entryEmpty->withSourceString('content'); |
|
264
|
|
|
$entry->withSourcePath(__FILE__); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @covers ::withSourceStream() |
|
269
|
|
|
* @expectedException \InvalidArgumentException |
|
270
|
|
|
*/ |
|
271
|
|
|
public function testSourceStreamExceptionPathSet() |
|
272
|
|
|
{ |
|
273
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
274
|
|
|
$entry = $entryEmpty->withSourcePath(__DIR__ . '/ipsum.txt'); |
|
275
|
|
|
$entry->withSourceStream(\fopen('php://memory', 'r')); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @covers ::withSourceStream() |
|
280
|
|
|
* @expectedException \InvalidArgumentException |
|
281
|
|
|
*/ |
|
282
|
|
|
public function testSourceStreamExceptionStreamSet() |
|
283
|
|
|
{ |
|
284
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
285
|
|
|
$entry = $entryEmpty->withSourceStream(\fopen('php://memory', 'r')); |
|
286
|
|
|
$entry->withSourceStream(\fopen('php://memory', 'r')); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @covers ::withSourceStream() |
|
291
|
|
|
* @expectedException \InvalidArgumentException |
|
292
|
|
|
*/ |
|
293
|
|
|
public function testSourceStreamExceptionStringSet() |
|
294
|
|
|
{ |
|
295
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
296
|
|
|
$entry = $entryEmpty->withSourceString('content'); |
|
297
|
|
|
$entry->withSourceStream(\fopen('php://memory', 'r')); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @covers ::withSourceString() |
|
302
|
|
|
* @expectedException \InvalidArgumentException |
|
303
|
|
|
*/ |
|
304
|
|
|
public function testSourceStringExceptionPathSet() |
|
305
|
|
|
{ |
|
306
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
307
|
|
|
$entry = $entryEmpty->withSourcePath(__DIR__ . '/ipsum.txt'); |
|
308
|
|
|
$entry->withSourceString('content'); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @covers ::withSourceString() |
|
313
|
|
|
* @expectedException \InvalidArgumentException |
|
314
|
|
|
*/ |
|
315
|
|
|
public function testSourceStringExceptionStreamSet() |
|
316
|
|
|
{ |
|
317
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
318
|
|
|
$entry = $entryEmpty->withSourceStream(\fopen('php://memory', 'r')); |
|
319
|
|
|
$entry->withSourceString('content'); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* @covers ::withSourceString() |
|
324
|
|
|
* @expectedException \InvalidArgumentException |
|
325
|
|
|
*/ |
|
326
|
|
|
public function testSourceStringExceptionStringSet() |
|
327
|
|
|
{ |
|
328
|
|
|
$entryEmpty = new ArchiveEntry('entry'); |
|
329
|
|
|
$entry = $entryEmpty->withSourceString('content'); |
|
330
|
|
|
$entry->withSourceString('content'); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @covers ::getSourceAsStream() |
|
335
|
|
|
*/ |
|
336
|
|
View Code Duplication |
public function testGetSourceAsStreamFromPath() |
|
|
|
|
|
|
337
|
|
|
{ |
|
338
|
|
|
$filename = __DIR__ . '/ipsum.txt'; |
|
339
|
|
|
$stream = (new ArchiveEntry(\basename($filename)))->withSourcePath($filename)->getSourceAsStream(); |
|
340
|
|
|
|
|
341
|
|
|
$this->assertTrue(\is_resource($stream)); |
|
342
|
|
|
$this->assertSame(\file_get_contents($filename), \stream_get_contents($stream)); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @covers ::getSourceAsStream() |
|
347
|
|
|
*/ |
|
348
|
|
View Code Duplication |
public function testGetSourceAsStreamFromStream() |
|
|
|
|
|
|
349
|
|
|
{ |
|
350
|
|
|
$filename = __DIR__ . '/ipsum.txt'; |
|
351
|
|
|
$stream = (new ArchiveEntry(\basename($filename)))->withSourceStream(\fopen($filename, 'r'))->getSourceAsStream(); |
|
352
|
|
|
|
|
353
|
|
|
$this->assertTrue(\is_resource($stream)); |
|
354
|
|
|
$this->assertSame(\file_get_contents($filename), \stream_get_contents($stream)); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @covers ::getSourceAsStream() |
|
359
|
|
|
*/ |
|
360
|
|
View Code Duplication |
public function testGetSourceAsStreamFromString() |
|
|
|
|
|
|
361
|
|
|
{ |
|
362
|
|
|
$filename = __DIR__ . '/ipsum.txt'; |
|
363
|
|
|
$stream = (new ArchiveEntry(\basename($filename)))->withSourceString(\file_get_contents($filename))->getSourceAsStream(); |
|
364
|
|
|
|
|
365
|
|
|
$this->assertTrue(\is_resource($stream)); |
|
366
|
|
|
$this->assertSame(\file_get_contents($filename), \stream_get_contents($stream)); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
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.