UploadTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace iBrand\Upload\Test;
  3. use iBrand\Upload\UploadFiles;
  4. use Illuminate\Http\UploadedFile;
  5. use Illuminate\Support\Facades\Storage;
  6. class UploadTest extends BaseTest
  7. {
  8. /** @test */
  9. public function TestConfig()
  10. {
  11. $config = config('filesystems.disks');
  12. $this->assertArrayHasKey('qiniu', $config);
  13. }
  14. /** @test */
  15. public function testUploadImage()
  16. {
  17. $client_id = 1;
  18. $response = $this->post('cdn/upload', [
  19. 'upload_file' => UploadedFile::fake()->image('no_head.jpg'),
  20. 'client_id' => $client_id,
  21. ]);
  22. $res = $response->getContent();
  23. $result = json_decode($res, true);
  24. $this->assertTrue($result['status']);
  25. $this->assertArrayHasKey('data', $result);
  26. $this->assertTrue(Storage::disk('qiniu')->has($result['data']['path']));
  27. $file = UploadFiles::where('client_id', $client_id)->where('path', $result['data']['path'])->first();
  28. $this->assertTrue(Storage::disk('qiniu')->has($file->path));
  29. $this->assertSame($result['data']['url'], $file->url);
  30. }
  31. /** @test */
  32. public function testUploadInvalidFileType()
  33. {
  34. $client_id = 1;
  35. $response = $this->post('cdn/upload', [
  36. 'upload_file' => '12123123',
  37. 'client_id' => $client_id,
  38. ]);
  39. $res = $response->getContent();
  40. $result = json_decode($res, true);
  41. $this->assertFalse($result['status']);
  42. $response = $this->post('cdn/upload', [
  43. 'upload_file' => UploadedFile::fake()->create('document.pdf'),
  44. 'client_id' => $client_id,
  45. ]);
  46. $res = $response->getContent();
  47. $result = json_decode($res, true);
  48. $this->assertFalse($result['status']);
  49. $response = $this->post('cdn/upload', [
  50. 'upload_file' => UploadedFile::fake()->create('test.bmp'),
  51. 'client_id' => $client_id,
  52. ]);
  53. $res = $response->getContent();
  54. $result = json_decode($res, true);
  55. $this->assertFalse($result['status']);
  56. config(['ibrand.uploader.upload_image.supportedExtensions' => ['png']]);
  57. $response = $this->post('cdn/upload', [
  58. 'upload_file' => UploadedFile::fake()->create('test.jpeg'),
  59. 'client_id' => $client_id,
  60. ]);
  61. $res = $response->getContent();
  62. $result = json_decode($res, true);
  63. $this->assertFalse($result['status']);
  64. }
  65. /** @test */
  66. /*public function testAllowUploadFileSize()
  67. {
  68. $client_id = 1;
  69. $response = $this->post('cdn/upload', [
  70. 'upload_file' => UploadedFile::fake()->image('test.jpg')->size(3 * 1024),
  71. 'client_id' => $client_id,
  72. ]);
  73. $res = $response->getContent();
  74. $result = json_decode($res, true);
  75. $this->assertFalse($result['status']);
  76. }*/
  77. }