1 | <?php |
||
4 | if (!class_exists('\PHPUnit\Framework\TestCase') && |
||
5 | class_exists('\PHPUnit_Framework_TestCase')) { |
||
6 | class_alias('\PHPUnit_Framework_TestCase', 'PHPUnit\Framework\TestCase'); |
||
7 | } |
||
8 | |||
9 | |||
10 | class uploadTest extends \PHPUnit\Framework\TestCase |
||
11 | { |
||
12 | public $bulletproof, |
||
13 | $testingImage, |
||
14 | $_files = array(); |
||
15 | |||
16 | /** |
||
17 | * Initialize an array to mimic the properties $_FILES global |
||
18 | */ |
||
19 | public function __construct() |
||
20 | { |
||
21 | $files = array( |
||
22 | 'ikea' => array( |
||
23 | 'name' => $this->testingImage = __DIR__ . "/monkey.jpg", |
||
24 | 'type' => 'image/jpg', |
||
25 | 'tmp_name' => $this->testingImage = __DIR__ . "/monkey.jpg", |
||
26 | 'error' => 0, |
||
27 | 'size' => 17438, |
||
28 | ) |
||
29 | ); |
||
30 | |||
31 | $this->bulletproof = new \BulletProofTest\BulletProofOverride($files); |
||
32 | |||
33 | } |
||
34 | |||
35 | |||
36 | /** |
||
37 | * test array access offset name is created from $_FILES |
||
38 | */ |
||
39 | public function testArrayAccessReadsFileNameFromArray() |
||
40 | { |
||
41 | $this->assertEquals($this->bulletproof['ikea'], true); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * test custom image renaming |
||
46 | */ |
||
47 | public function testImageRenameReturnsNewName() |
||
48 | { |
||
49 | $this->bulletproof->setName('foo'); |
||
50 | $this->assertEquals($this->bulletproof->getName(), 'foo'); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * test storage creation |
||
55 | */ |
||
56 | public function testImageLocationReturnsAssignedValue() |
||
57 | { |
||
58 | $this->bulletproof->setLocation('family_pics'); |
||
59 | $this->assertEquals($this->bulletproof->getLocation(), 'family_pics'); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * test upload fails if image is less than the size set |
||
64 | */ |
||
65 | public function testUploadFailsIfImageSizeIsSmallerThanDefined() |
||
66 | { |
||
67 | $this->bulletproof['ikea']; |
||
68 | $this->bulletproof->setSize(100, 10000); |
||
69 | $upload = $this->bulletproof->upload(); |
||
70 | $this->assertEquals( |
||
71 | $this->bulletproof['error'], |
||
72 | "Image size should be at least more than 1 kb" |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * test image is uploaded based on the mime types set |
||
78 | */ |
||
79 | public function testImageUploadAcceptsOnlyAllowedMimeTypes() |
||
80 | { |
||
81 | $this->bulletproof['ikea']; |
||
82 | $this->bulletproof->setMime(array("png")); |
||
83 | $upload = $this->bulletproof->upload(); |
||
84 | $this->assertEquals( |
||
85 | $this->bulletproof["error"], |
||
86 | "Invalid File! Only (png) image types are allowed"); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * test image upload does not pass the defined height limit |
||
91 | */ |
||
92 | public function testImageDimensionDefinesImageHeightAndWidthLimit() |
||
93 | { |
||
94 | $this->bulletproof['ikea']; |
||
95 | $this->bulletproof->setDimension(200, 100); |
||
96 | $upload = $this->bulletproof->upload(); |
||
97 | $this->assertEquals( |
||
98 | $this->bulletproof["error"], |
||
99 | "Image height/width should be less than 100/200 pixels" |
||
100 | ); |
||
101 | |||
102 | } |
||
103 | |||
104 | /** |
||
105 | * test image name has auto-generated value if name is not provided |
||
106 | */ |
||
107 | public function testReturnValueOfImageNameAfterImageUpload() |
||
108 | { |
||
109 | $this->bulletproof['ikea']; |
||
110 | $upload = $this->bulletproof->upload(); |
||
111 | $this->assertSame(strlen($upload->getName()), 28); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * test image size return |
||
116 | */ |
||
117 | public function testReturnValueOfImageSizeAfterImageUpload() |
||
118 | { |
||
119 | $this->bulletproof['ikea']; |
||
120 | $upload = $this->bulletproof->upload(); |
||
121 | $this->assertSame($upload->getSize(), 17438); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * test image mime return |
||
126 | */ |
||
127 | public function testReturnValueOfImageMimeAfterImageUpload() |
||
128 | { |
||
129 | $this->bulletproof['ikea']; |
||
130 | $upload = $this->bulletproof->upload(); |
||
131 | $this->assertSame($upload->getMime(), 'jpeg'); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * test image width return |
||
136 | */ |
||
137 | public function testReturnValueOfImageWidthAfterImageUpload() |
||
138 | { |
||
139 | $this->bulletproof['ikea']; |
||
140 | $upload = $this->bulletproof->upload(); |
||
141 | $this->assertSame($upload->getWidth(), 384); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * test image height return |
||
146 | */ |
||
147 | public function testReturnValueOfImageHeightAfterImageUpload() |
||
148 | { |
||
149 | $this->bulletproof['ikea']; |
||
150 | $upload = $this->bulletproof->upload(); |
||
151 | $this->assertSame($upload->getHeight(), 345); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * test image location return |
||
156 | */ |
||
157 | public function testReturnValueOfImageLocationAfterImageUpload() |
||
158 | { |
||
159 | $this->bulletproof['ikea']; |
||
160 | $this->bulletproof->setLocation('images'); |
||
161 | $upload = $this->bulletproof->upload(); |
||
162 | $this->assertSame($upload->getLocation(), 'images'); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * test image full path return |
||
167 | */ |
||
168 | public function testReturnValueOfImageFullPathAfterImageUpload() |
||
169 | { |
||
170 | $this->bulletproof['ikea']; |
||
171 | $this->bulletproof->setLocation('images'); |
||
172 | $this->bulletproof->setName('2012'); |
||
173 | $upload = $this->bulletproof->upload(); |
||
174 | $getMime = $this->bulletproof->getMime(); |
||
175 | $this->assertSame($upload->getFullPath(), 'images/2012.' . $getMime); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * test image json value return |
||
180 | */ |
||
181 | public function testReturnValueOfImageJsonInfoAfterImageUpload() |
||
182 | { |
||
183 | $this->bulletproof['ikea']; |
||
184 | $upload = $this->bulletproof->setName('we_belive_in_json')->upload(); |
||
185 | $this->assertSame($upload->getJson(), |
||
186 | '{"name":"we_belive_in_json","mime":"jpeg","height":345,"width":384,"size":17438,"location":"bulletproof","fullpath":"bulletproof\/we_belive_in_json.jpeg"}'); |
||
193 |