|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Files; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Files\Download; |
|
6
|
|
|
use org\bovigo\vfs\vfsStream; |
|
7
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
|
8
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
|
9
|
|
|
|
|
10
|
|
|
class DownloadTest extends \PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Download |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $object; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
19
|
|
|
* This method is called before a test is executed. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
include_once __DIR__ . '/../../../framework/Koch/Files/Download.php'; |
|
24
|
|
|
include_once __DIR__ . '/../../../vendor/autoload.php'; |
|
25
|
|
|
|
|
26
|
|
|
$this->object = new Download(); |
|
27
|
|
|
|
|
28
|
|
|
$this->media_files = [ |
|
|
|
|
|
|
29
|
|
|
// type, mime, binarypacks |
|
30
|
|
|
['jpg', 'image/jpeg', [0xffd8, 0xffe0, 0x0010, 0x4a46, 0x4946, 0x0001, 0x0101, 0x0048, 0x0048]], |
|
31
|
|
|
['png', 'image/png', [0x8950, 0x4e47, 0x0d0a, 0x1a0a, 0x0000, 0x000d, 0x4948, 0x4452, 0x0000]], |
|
32
|
|
|
['mp4', 'video/mp4', [0x0000, 0x001c, 0x6674, 0x7970, 0x6d70, 0x3432, 0x0000, 0x0000, 0x6973]], |
|
33
|
|
|
['mp3', 'audio/mpeg', [0x4944, 0x3303, 0x0000, 0x0000, 0x1064, 0x5441, 0x4c42, 0x0000, 0x0017]], |
|
34
|
|
|
['avi', 'video/x-msvideo', [0x5249, 0x4646, 0x6a42, 0x0100, 0x4156, 0x4920, 0x4c49, 0x5354, 0x8c05]], |
|
35
|
|
|
['ogg', 'application/ogg', [0x4f67, 0x6753, 0x0002, 0x0000, 0x0000, 0x0000, 0x0000, 0x5d28, 0xf95e]], |
|
36
|
|
|
#array("crap", "application/octet-stream", array(0xff09, 0xff08, 0xff07, 0xff06, 0xff05, 0xff04, 0xff03, 0xff02, 0xff01)), |
|
|
|
|
|
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
vfsStreamWrapper::register(); |
|
40
|
|
|
$this->root = new vfsStreamDirectory('root'); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
// write virtual files |
|
43
|
|
|
foreach ($this->media_files as $mime) { |
|
44
|
|
|
// extract inner array structure into variables |
|
45
|
|
|
list($type, $mimetype, $binary_pack) = $mime; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$file = 'file.' . $type; |
|
48
|
|
|
$file = vfsStream::newFile($file, 0777)->withContent( |
|
49
|
|
|
// write binarypacks to file |
|
50
|
|
|
call_user_func_array('pack', array_merge(['n*'], (array) $binary_pack)) |
|
|
|
|
|
|
51
|
|
|
); |
|
52
|
|
|
$this->root->addChild($file); |
|
53
|
|
|
} |
|
54
|
|
|
vfsStreamWrapper::setRoot($this->root); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
59
|
|
|
* This method is called after a test is executed. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function tearDown() |
|
62
|
|
|
{ |
|
63
|
|
|
unset($this->object); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @covers Koch\Files\Download::getMimeType |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testGetMimeType() |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($this->media_files as $mime) { |
|
72
|
|
|
// extract inner array structure into variables |
|
73
|
|
|
list($type, $mimetype, $binary_pack) = $mime; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
// skip mp3, finfo detects them as "application/octet-stream" |
|
76
|
|
|
// instead of "audio/mpeg" |
|
77
|
|
|
if ($type === 'mp3') { |
|
78
|
|
|
// skip |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$vfsFile = vfsStream::url('root/file.' . $type); |
|
83
|
|
|
$fetched_mimetype = $this->object->getMimeType($vfsFile); |
|
|
|
|
|
|
84
|
|
|
$this->assertEquals($mimetype, $fetched_mimetype); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// fallback: unknown mimetype is always "application/octet-stream" |
|
88
|
|
|
#$file = vfsStream::url('root/file.crap'); |
|
|
|
|
|
|
89
|
|
|
#$this->assertEquals('application/octet-stream', $this->object->getMimeType($file)); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @runInSeparateProcess |
|
94
|
|
|
* -preserveGlobalState disabled |
|
95
|
|
|
* @covers Koch\Files\Download::sendFile |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testSendFile() |
|
98
|
|
|
{ |
|
99
|
|
|
$file = __DIR__ . DIRECTORY_SEPARATOR . 'DownloadTest.php'; |
|
100
|
|
|
$this->object->sendFile($file); |
|
101
|
|
|
|
|
102
|
|
|
$this->expectOutputString(file_get_contents($file)); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: