jaxon-php /
jaxon-mono
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Jaxon\Upload\Tests\TestUpload; |
||||
| 4 | |||||
| 5 | use Jaxon\Jaxon; |
||||
| 6 | use Jaxon\Exception\RequestException; |
||||
| 7 | use Jaxon\Exception\SetupException; |
||||
| 8 | use Nyholm\Psr7\UploadedFile; |
||||
| 9 | use Nyholm\Psr7Server\ServerRequestCreator; |
||||
| 10 | use Psr\Http\Message\ServerRequestInterface; |
||||
| 11 | use PHPUnit\Framework\TestCase; |
||||
| 12 | |||||
| 13 | use function Jaxon\jaxon; |
||||
| 14 | use function filesize; |
||||
| 15 | use function Jaxon\Upload\_register as _registerUpload; |
||||
| 16 | |||||
| 17 | class UploadTest extends TestCase |
||||
| 18 | { |
||||
| 19 | /** |
||||
| 20 | * @var string |
||||
| 21 | */ |
||||
| 22 | protected $sNameWhite; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * @var string |
||||
| 26 | */ |
||||
| 27 | protected $sPathWhite; |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * @var int |
||||
| 31 | */ |
||||
| 32 | protected $sSizeWhite; |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * @var string |
||||
| 36 | */ |
||||
| 37 | protected $sNameBlue; |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * @var string |
||||
| 41 | */ |
||||
| 42 | protected $sPathBlue; |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @var int |
||||
| 46 | */ |
||||
| 47 | protected $sSizeBlue; |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * @throws SetupException |
||||
| 51 | */ |
||||
| 52 | public function setUp(): void |
||||
| 53 | { |
||||
| 54 | _registerUpload(); |
||||
| 55 | |||||
| 56 | jaxon()->setAppOption('upload.enabled', true); |
||||
| 57 | jaxon()->setAppOptions([ |
||||
| 58 | 'adapter' => 'local', |
||||
| 59 | 'dir' => __DIR__ . '/../upload/dst', |
||||
| 60 | // 'options' => [], |
||||
| 61 | ], 'storage.stores.uploads'); |
||||
| 62 | |||||
| 63 | jaxon()->setOption('core.response.send', false); |
||||
| 64 | $tmpDir = __DIR__ . '/../upload/tmp'; |
||||
| 65 | @mkdir($tmpDir); |
||||
|
0 ignored issues
–
show
|
|||||
| 66 | |||||
| 67 | $sSrcWhite = __DIR__ . '/../upload/src/white.png'; |
||||
| 68 | $this->sNameWhite = 'white.png'; |
||||
| 69 | $this->sPathWhite = "$tmpDir/{$this->sNameWhite}"; |
||||
| 70 | $this->sSizeWhite = filesize($sSrcWhite); |
||||
| 71 | // Copy the file to the temp dir. |
||||
| 72 | @copy($sSrcWhite, $this->sPathWhite); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
copy(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 73 | |||||
| 74 | $sSrcBlue = __DIR__ . '/../upload/src/blue.png'; |
||||
| 75 | $this->sNameBlue = 'blue.png'; |
||||
| 76 | $this->sPathBlue = "$tmpDir/{$this->sNameBlue}"; |
||||
| 77 | $this->sSizeBlue = filesize($sSrcBlue); |
||||
| 78 | // Copy the file to the temp dir. |
||||
| 79 | @copy($sSrcBlue, $this->sPathBlue); |
||||
| 80 | |||||
| 81 | jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
||||
| 82 | jaxon()->di()->getBootstrap()->onBoot(); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * @throws SetupException |
||||
| 87 | */ |
||||
| 88 | public function tearDown(): void |
||||
| 89 | { |
||||
| 90 | jaxon()->reset(); |
||||
| 91 | parent::tearDown(); |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | public function testAjaxUpload() |
||||
| 95 | { |
||||
| 96 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 97 | // Send a request to the registered class |
||||
| 98 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 99 | return $c->g(ServerRequestCreator::class) |
||||
| 100 | ->fromGlobals() |
||||
| 101 | ->withParsedBody([ |
||||
| 102 | 'jxncall' => json_encode([ |
||||
| 103 | 'type' => 'class', |
||||
| 104 | 'name' => 'SampleUpload', |
||||
| 105 | 'method' => 'myMethod', |
||||
| 106 | 'args' => [], |
||||
| 107 | ]), |
||||
| 108 | ]) |
||||
| 109 | ->withUploadedFiles([ |
||||
| 110 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 111 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 112 | ]) |
||||
| 113 | ->withMethod('POST'); |
||||
| 114 | }); |
||||
| 115 | |||||
| 116 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 117 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 118 | jaxon()->processRequest(); |
||||
| 119 | |||||
| 120 | // Uploaded files |
||||
| 121 | $aFiles = jaxon()->upload()->files(); |
||||
| 122 | $this->assertCount(1, $aFiles); |
||||
| 123 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 124 | $xFile = $aFiles['image'][0]; |
||||
| 125 | $this->assertEquals('white', $xFile->name()); |
||||
| 126 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 127 | $this->assertEquals('png', $xFile->type()); |
||||
| 128 | $this->assertEquals('png', $xFile->extension()); |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | public function testAjaxUploadMultipleFiles() |
||||
| 132 | { |
||||
| 133 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 134 | // Send a request to the registered class |
||||
| 135 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 136 | return $c->g(ServerRequestCreator::class) |
||||
| 137 | ->fromGlobals() |
||||
| 138 | ->withParsedBody([ |
||||
| 139 | 'jxncall' => json_encode([ |
||||
| 140 | 'type' => 'class', |
||||
| 141 | 'name' => 'SampleUpload', |
||||
| 142 | 'method' => 'myMethod', |
||||
| 143 | 'args' => [], |
||||
| 144 | ]), |
||||
| 145 | ]) |
||||
| 146 | ->withUploadedFiles([ |
||||
| 147 | 'image' => [ |
||||
| 148 | new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 149 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 150 | new UploadedFile($this->sPathBlue, $this->sSizeBlue, |
||||
| 151 | UPLOAD_ERR_OK, $this->sNameBlue, 'png'), |
||||
| 152 | ], |
||||
| 153 | ]) |
||||
| 154 | ->withMethod('POST'); |
||||
| 155 | }); |
||||
| 156 | |||||
| 157 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 158 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 159 | jaxon()->processRequest(); |
||||
| 160 | |||||
| 161 | // Uploaded files |
||||
| 162 | $aFiles = jaxon()->upload()->files(); |
||||
| 163 | $this->assertCount(1, $aFiles); |
||||
| 164 | $this->assertCount(2, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 165 | $xFile = $aFiles['image'][0]; |
||||
| 166 | $this->assertEquals('white', $xFile->name()); |
||||
| 167 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 168 | $this->assertEquals('png', $xFile->type()); |
||||
| 169 | $this->assertEquals('png', $xFile->extension()); |
||||
| 170 | $xFile = $aFiles['image'][1]; |
||||
| 171 | $this->assertEquals('blue', $xFile->name()); |
||||
| 172 | $this->assertEquals($this->sNameBlue, $xFile->filename()); |
||||
| 173 | $this->assertEquals('png', $xFile->type()); |
||||
| 174 | $this->assertEquals('png', $xFile->extension()); |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | public function testAjaxUploadMultipleNames() |
||||
| 178 | { |
||||
| 179 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 180 | // Send a request to the registered class |
||||
| 181 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 182 | return $c->g(ServerRequestCreator::class) |
||||
| 183 | ->fromGlobals() |
||||
| 184 | ->withParsedBody([ |
||||
| 185 | 'jxncall' => json_encode([ |
||||
| 186 | 'type' => 'class', |
||||
| 187 | 'name' => 'SampleUpload', |
||||
| 188 | 'method' => 'myMethod', |
||||
| 189 | 'args' => [], |
||||
| 190 | ]), |
||||
| 191 | ]) |
||||
| 192 | ->withUploadedFiles([ |
||||
| 193 | 'white' => [ |
||||
| 194 | new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 195 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 196 | ], |
||||
| 197 | 'blue' => [ |
||||
| 198 | new UploadedFile($this->sPathBlue, $this->sSizeBlue, |
||||
| 199 | UPLOAD_ERR_OK, $this->sNameBlue, 'png'), |
||||
| 200 | ], |
||||
| 201 | ]) |
||||
| 202 | ->withMethod('POST'); |
||||
| 203 | }); |
||||
| 204 | |||||
| 205 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 206 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 207 | jaxon()->processRequest(); |
||||
| 208 | |||||
| 209 | // Uploaded files |
||||
| 210 | $aFiles = jaxon()->upload()->files(); |
||||
| 211 | $this->assertCount(2, $aFiles); |
||||
| 212 | $this->assertCount(1, $aFiles['white']); |
||||
|
0 ignored issues
–
show
$aFiles['white'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 213 | $this->assertCount(1, $aFiles['blue']); |
||||
| 214 | $xFile = $aFiles['white'][0]; |
||||
| 215 | $this->assertEquals('white', $xFile->name()); |
||||
| 216 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 217 | $this->assertEquals('png', $xFile->type()); |
||||
| 218 | $this->assertEquals('png', $xFile->extension()); |
||||
| 219 | $xFile = $aFiles['blue'][0]; |
||||
| 220 | $this->assertEquals('blue', $xFile->name()); |
||||
| 221 | $this->assertEquals($this->sNameBlue, $xFile->filename()); |
||||
| 222 | $this->assertEquals('png', $xFile->type()); |
||||
| 223 | $this->assertEquals('png', $xFile->extension()); |
||||
| 224 | } |
||||
| 225 | |||||
| 226 | public function testAjaxUploadNameSanitizer() |
||||
| 227 | { |
||||
| 228 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 229 | jaxon()->upload()->sanitizer(function($sFilename, $sVarName) { |
||||
| 230 | return $sVarName === 'image' ? 'img_' . $sFilename : $sFilename; |
||||
| 231 | }); |
||||
| 232 | // Send a request to the registered class |
||||
| 233 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 234 | return $c->g(ServerRequestCreator::class) |
||||
| 235 | ->fromGlobals() |
||||
| 236 | ->withParsedBody([ |
||||
| 237 | 'jxncall' => json_encode([ |
||||
| 238 | 'type' => 'class', |
||||
| 239 | 'name' => 'SampleUpload', |
||||
| 240 | 'method' => 'myMethod', |
||||
| 241 | 'args' => [], |
||||
| 242 | ]), |
||||
| 243 | ]) |
||||
| 244 | ->withUploadedFiles([ |
||||
| 245 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 246 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 247 | ]) |
||||
| 248 | ->withMethod('POST'); |
||||
| 249 | }); |
||||
| 250 | |||||
| 251 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 252 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 253 | jaxon()->processRequest(); |
||||
| 254 | |||||
| 255 | // Uploaded files |
||||
| 256 | $aFiles = jaxon()->upload()->files(); |
||||
| 257 | $this->assertCount(1, $aFiles); |
||||
| 258 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 259 | $xFile = $aFiles['image'][0]; |
||||
| 260 | $this->assertEquals('img_white', $xFile->name()); |
||||
| 261 | $this->assertEquals('png', $xFile->type()); |
||||
| 262 | $this->assertEquals('png', $xFile->extension()); |
||||
| 263 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 264 | } |
||||
| 265 | |||||
| 266 | public function testUploadFileTypeValidationOk() |
||||
| 267 | { |
||||
| 268 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 269 | jaxon()->setAppOption('upload.default.types', ['png']); |
||||
| 270 | // Send a request to the registered class |
||||
| 271 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 272 | return $c->g(ServerRequestCreator::class) |
||||
| 273 | ->fromGlobals() |
||||
| 274 | ->withParsedBody([ |
||||
| 275 | 'jxncall' => json_encode([ |
||||
| 276 | 'type' => 'class', |
||||
| 277 | 'name' => 'SampleUpload', |
||||
| 278 | 'method' => 'myMethod', |
||||
| 279 | 'args' => [], |
||||
| 280 | ]), |
||||
| 281 | ]) |
||||
| 282 | ->withUploadedFiles([ |
||||
| 283 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 284 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 285 | ])->withMethod('POST'); |
||||
| 286 | }); |
||||
| 287 | |||||
| 288 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 289 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 290 | jaxon()->processRequest(); |
||||
| 291 | |||||
| 292 | // Return the file name for the next test |
||||
| 293 | $aFiles = jaxon()->upload()->files(); |
||||
| 294 | $this->assertCount(1, $aFiles); |
||||
| 295 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 296 | $xFile = $aFiles['image'][0]; |
||||
| 297 | $this->assertEquals('white', $xFile->name()); |
||||
| 298 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 299 | } |
||||
| 300 | |||||
| 301 | public function testUploadFileTypeValidationError() |
||||
| 302 | { |
||||
| 303 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 304 | jaxon()->setAppOption('upload.default.types', ['jpg']); |
||||
| 305 | // Send a request to the registered class |
||||
| 306 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 307 | return $c->g(ServerRequestCreator::class) |
||||
| 308 | ->fromGlobals() |
||||
| 309 | ->withParsedBody([ |
||||
| 310 | 'jxncall' => json_encode([ |
||||
| 311 | 'type' => 'class', |
||||
| 312 | 'name' => 'SampleUpload', |
||||
| 313 | 'method' => 'myMethod', |
||||
| 314 | 'args' => [], |
||||
| 315 | ]), |
||||
| 316 | ]) |
||||
| 317 | ->withUploadedFiles([ |
||||
| 318 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 319 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 320 | ]) |
||||
| 321 | ->withMethod('POST'); |
||||
| 322 | }); |
||||
| 323 | |||||
| 324 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 325 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 326 | $this->expectException(RequestException::class); |
||||
| 327 | jaxon()->processRequest(); |
||||
| 328 | } |
||||
| 329 | |||||
| 330 | public function testUploadFileExtensionValidationOk() |
||||
| 331 | { |
||||
| 332 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 333 | jaxon()->setAppOption('upload.default.extensions', ['png']); |
||||
| 334 | // Send a request to the registered class |
||||
| 335 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 336 | return $c->g(ServerRequestCreator::class) |
||||
| 337 | ->fromGlobals() |
||||
| 338 | ->withParsedBody([ |
||||
| 339 | 'jxncall' => json_encode([ |
||||
| 340 | 'type' => 'class', |
||||
| 341 | 'name' => 'SampleUpload', |
||||
| 342 | 'method' => 'myMethod', |
||||
| 343 | 'args' => [], |
||||
| 344 | ]), |
||||
| 345 | ]) |
||||
| 346 | ->withUploadedFiles([ |
||||
| 347 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 348 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 349 | ]) |
||||
| 350 | ->withMethod('POST'); |
||||
| 351 | }); |
||||
| 352 | |||||
| 353 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 354 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 355 | jaxon()->processRequest(); |
||||
| 356 | |||||
| 357 | // Return the file name for the next test |
||||
| 358 | $aFiles = jaxon()->upload()->files(); |
||||
| 359 | $this->assertCount(1, $aFiles); |
||||
| 360 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 361 | $xFile = $aFiles['image'][0]; |
||||
| 362 | $this->assertEquals('white', $xFile->name()); |
||||
| 363 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 364 | } |
||||
| 365 | |||||
| 366 | public function testUploadFileExtensionValidationError() |
||||
| 367 | { |
||||
| 368 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 369 | jaxon()->setAppOption('upload.default.extensions', ['jpg']); |
||||
| 370 | // Send a request to the registered class |
||||
| 371 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 372 | return $c->g(ServerRequestCreator::class) |
||||
| 373 | ->fromGlobals() |
||||
| 374 | ->withParsedBody([ |
||||
| 375 | 'jxncall' => json_encode([ |
||||
| 376 | 'type' => 'class', |
||||
| 377 | 'name' => 'SampleUpload', |
||||
| 378 | 'method' => 'myMethod', |
||||
| 379 | 'args' => [], |
||||
| 380 | ]), |
||||
| 381 | ]) |
||||
| 382 | ->withUploadedFiles([ |
||||
| 383 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 384 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 385 | ]) |
||||
| 386 | ->withMethod('POST'); |
||||
| 387 | }); |
||||
| 388 | |||||
| 389 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 390 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 391 | $this->expectException(RequestException::class); |
||||
| 392 | jaxon()->processRequest(); |
||||
| 393 | } |
||||
| 394 | |||||
| 395 | public function testUploadFileMaxSizeValidationOk() |
||||
| 396 | { |
||||
| 397 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 398 | jaxon()->setAppOption('upload.default.max-size', 30000); |
||||
| 399 | // Send a request to the registered class |
||||
| 400 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 401 | return $c->g(ServerRequestCreator::class) |
||||
| 402 | ->fromGlobals() |
||||
| 403 | ->withParsedBody([ |
||||
| 404 | 'jxncall' => json_encode([ |
||||
| 405 | 'type' => 'class', |
||||
| 406 | 'name' => 'SampleUpload', |
||||
| 407 | 'method' => 'myMethod', |
||||
| 408 | 'args' => [], |
||||
| 409 | ]), |
||||
| 410 | ]) |
||||
| 411 | ->withUploadedFiles([ |
||||
| 412 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 413 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 414 | ]) |
||||
| 415 | ->withMethod('POST'); |
||||
| 416 | }); |
||||
| 417 | |||||
| 418 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 419 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 420 | jaxon()->processRequest(); |
||||
| 421 | |||||
| 422 | // Return the file name for the next test |
||||
| 423 | $aFiles = jaxon()->upload()->files(); |
||||
| 424 | $this->assertCount(1, $aFiles); |
||||
| 425 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 426 | $xFile = $aFiles['image'][0]; |
||||
| 427 | $this->assertEquals('white', $xFile->name()); |
||||
| 428 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 429 | } |
||||
| 430 | |||||
| 431 | public function testUploadFileMaxSizeValidationError() |
||||
| 432 | { |
||||
| 433 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 434 | jaxon()->setAppOption('upload.default.max-size', 25000); |
||||
| 435 | // Send a request to the registered class |
||||
| 436 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 437 | return $c->g(ServerRequestCreator::class) |
||||
| 438 | ->fromGlobals() |
||||
| 439 | ->withParsedBody([ |
||||
| 440 | 'jxncall' => json_encode([ |
||||
| 441 | 'type' => 'class', |
||||
| 442 | 'name' => 'SampleUpload', |
||||
| 443 | 'method' => 'myMethod', |
||||
| 444 | 'args' => [], |
||||
| 445 | ]), |
||||
| 446 | ]) |
||||
| 447 | ->withUploadedFiles([ |
||||
| 448 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 449 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 450 | ]) |
||||
| 451 | ->withMethod('POST'); |
||||
| 452 | }); |
||||
| 453 | |||||
| 454 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 455 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 456 | $this->expectException(RequestException::class); |
||||
| 457 | jaxon()->processRequest(); |
||||
| 458 | } |
||||
| 459 | |||||
| 460 | public function testUploadFileMinSizeValidationOk() |
||||
| 461 | { |
||||
| 462 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 463 | jaxon()->setAppOption('upload.default.min-size', 25000); |
||||
| 464 | // Send a request to the registered class |
||||
| 465 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 466 | return $c->g(ServerRequestCreator::class) |
||||
| 467 | ->fromGlobals() |
||||
| 468 | ->withParsedBody([ |
||||
| 469 | 'jxncall' => json_encode([ |
||||
| 470 | 'type' => 'class', |
||||
| 471 | 'name' => 'SampleUpload', |
||||
| 472 | 'method' => 'myMethod', |
||||
| 473 | 'args' => [], |
||||
| 474 | ]), |
||||
| 475 | ]) |
||||
| 476 | ->withUploadedFiles([ |
||||
| 477 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 478 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 479 | ]) |
||||
| 480 | ->withMethod('POST'); |
||||
| 481 | }); |
||||
| 482 | |||||
| 483 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 484 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 485 | jaxon()->processRequest(); |
||||
| 486 | |||||
| 487 | // Return the file name for the next test |
||||
| 488 | $aFiles = jaxon()->upload()->files(); |
||||
| 489 | $this->assertCount(1, $aFiles); |
||||
| 490 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 491 | $xFile = $aFiles['image'][0]; |
||||
| 492 | $this->assertEquals('white', $xFile->name()); |
||||
| 493 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 494 | } |
||||
| 495 | |||||
| 496 | public function testUploadFileMinSizeValidationError() |
||||
| 497 | { |
||||
| 498 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 499 | jaxon()->setAppOption('upload.default.min-size', 30000); |
||||
| 500 | // Send a request to the registered class |
||||
| 501 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 502 | return $c->g(ServerRequestCreator::class) |
||||
| 503 | ->fromGlobals() |
||||
| 504 | ->withParsedBody([ |
||||
| 505 | 'jxncall' => json_encode([ |
||||
| 506 | 'type' => 'class', |
||||
| 507 | 'name' => 'SampleUpload', |
||||
| 508 | 'method' => 'myMethod', |
||||
| 509 | 'args' => [], |
||||
| 510 | ]), |
||||
| 511 | ]) |
||||
| 512 | ->withUploadedFiles([ |
||||
| 513 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 514 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 515 | ]) |
||||
| 516 | ->withMethod('POST'); |
||||
| 517 | }); |
||||
| 518 | |||||
| 519 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 520 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 521 | $this->expectException(RequestException::class); |
||||
| 522 | jaxon()->processRequest(); |
||||
| 523 | } |
||||
| 524 | |||||
| 525 | public function testRequestWithNoPluginNoUpload() |
||||
| 526 | { |
||||
| 527 | jaxon()->setAppOption('upload.enabled', false); |
||||
| 528 | // Send a request to the registered class |
||||
| 529 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 530 | return $c->g(ServerRequestCreator::class) |
||||
| 531 | ->fromGlobals() |
||||
| 532 | ->withParsedBody([ |
||||
| 533 | 'jxncall' => json_encode([ |
||||
| 534 | 'who' => 'Nobody', |
||||
| 535 | 'args' => [], |
||||
| 536 | ]), |
||||
| 537 | ]) |
||||
| 538 | ->withMethod('POST'); |
||||
| 539 | }); |
||||
| 540 | |||||
| 541 | $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 542 | } |
||||
| 543 | } |
||||
| 544 |
If you suppress an error, we recommend checking for the error condition explicitly: