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