|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace OnixSystemsPHP\HyperfFileUpload\Test\Cases\Service; |
| 13 | + |
| 14 | +use Hyperf\Config\Config; |
| 15 | +use Hyperf\Contract\TranslatorInterface; |
| 16 | +use Hyperf\Filesystem\FilesystemFactory; |
| 17 | +use Hyperf\HttpMessage\Upload\UploadedFile; |
| 18 | +use League\Flysystem\Filesystem; |
| 19 | +use OnixSystemsPHP\HyperfCore\Contract\CoreAuthenticatable; |
| 20 | +use OnixSystemsPHP\HyperfCore\Exception\BusinessException; |
| 21 | +use OnixSystemsPHP\HyperfFileUpload\Repository\FileRepository; |
| 22 | +use OnixSystemsPHP\HyperfFileUpload\Service\AddFileService; |
| 23 | +use OnixSystemsPHP\HyperfFileUpload\Test\Cases\AppTest; |
| 24 | +use PHPUnit\Framework\MockObject\MockObject; |
| 25 | +use PHPUnit\Framework\MockObject\Rule\InvokedCount; |
| 26 | + |
| 27 | +/** |
| 28 | + * @internal |
| 29 | + * @coversNothing |
| 30 | + */ |
| 31 | +class AddFileServiceTest extends AppTest |
| 32 | +{ |
| 33 | + public CoreAuthenticatable $user; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + $trans = $this->createMock(TranslatorInterface::class); |
| 40 | + $this->createContainer([TranslatorInterface::class => $trans]); |
| 41 | + $this->user = $this->createMock(CoreAuthenticatable::class); |
| 42 | + $this->user->method('getId')->willReturn(1); |
| 43 | + } |
| 44 | + |
| 45 | + public function testMain() |
| 46 | + { |
| 47 | + $service = $this->getContainer(1, 1, 1, 1); |
| 48 | + $file = $service->run( |
| 49 | + new UploadedFile(BASE_PATH . '/tests/test-image.png', 70, UPLOAD_ERR_OK, 'test-image.png', 'image/png'), |
| 50 | + $this->user |
| 51 | + ); |
| 52 | + $this->assertEquals($this->user->getId(), $file->user_id); |
| 53 | + $this->assertNull($file->fileable_id); |
| 54 | + $this->assertNull($file->fileable_type); |
| 55 | + $this->assertNull($file->field_name); |
| 56 | + $this->assertNotNull($file->storage); |
| 57 | + $this->assertNotNull($file->path); |
| 58 | + $this->assertNotNull($file->name); |
| 59 | + $this->assertNotNull($file->full_path); |
| 60 | + $this->assertNotNull($file->domain); |
| 61 | + $this->assertNotNull($file->url); |
| 62 | + $this->assertEquals('test-image.png', $file->original_name); |
| 63 | + $this->assertEquals(70, $file->size); |
| 64 | + $this->assertEquals('image/png', $file->mime); |
| 65 | + $this->assertEquals([], $file->presets); |
| 66 | + } |
| 67 | + |
| 68 | + public function testNotLoggedIn() |
| 69 | + { |
| 70 | + $service = $this->getContainer(1, 1, 1, 1); |
| 71 | + $file = $service->run( |
| 72 | + new UploadedFile(BASE_PATH . '/tests/test-image.png', 70, UPLOAD_ERR_OK, 'test-image.png', 'image/png'), |
| 73 | + null |
| 74 | + ); |
| 75 | + $this->assertEquals(null, $file->user_id); |
| 76 | + } |
| 77 | + |
| 78 | + public function testUploadError() |
| 79 | + { |
| 80 | + /** @var AddFileService $service */ |
| 81 | + $service = $this->getContainer(0, 0, 0, 0); |
| 82 | + $this->expectException(BusinessException::class); |
| 83 | + $service->run( |
| 84 | + new UploadedFile(BASE_PATH . '/tests/test-image.png', 70, UPLOAD_ERR_NO_TMP_DIR, 'test-image.png', 'image/png'), |
| 85 | + $this->user |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public function testWrongMimeType() |
| 90 | + { |
| 91 | + $service = $this->getContainer(0, 0, 0, 0); |
| 92 | + $this->expectException(BusinessException::class); |
| 93 | + $service->run( |
| 94 | + new UploadedFile(BASE_PATH . '/tests/bootstrap.php', 1073, UPLOAD_ERR_OK, 'test-image.png', 'image/png'), |
| 95 | + $this->user |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + protected function getContainer(int $nEvents, int $writesCount, int $savesCount, $createsCount): AddFileService |
| 100 | + { |
| 101 | + $fileSystemMock = $this->createMock(Filesystem::class); |
| 102 | + $fileSystemMock->expects(new InvokedCount($writesCount))->method('writeStream'); |
| 103 | + |
| 104 | + $fileSystemFactoryMock = $this->createMock(FilesystemFactory::class); |
| 105 | + $fileSystemFactoryMock->method('get')->willReturn($fileSystemMock); |
| 106 | + |
| 107 | + return new AddFileService( |
| 108 | + $this->getConfig(), |
| 109 | + $fileSystemFactoryMock, |
| 110 | + $this->getRepository($savesCount, $createsCount), |
| 111 | + $this->getEventDispatcherMock($nEvents), |
| 112 | + null, |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + private function getConfig(): Config |
| 117 | + { |
| 118 | + $config = new Config([]); |
| 119 | + $config->set('file_upload.mime_types', ['image/png', 'image/jpg', 'image/jpeg', 'image/bmp', 'application/pdf']); |
| 120 | + $config->set('file.default', 'local'); |
| 121 | + $config->set('file_upload.storage.local.domain', 'fake'); |
| 122 | + return $config; |
| 123 | + } |
| 124 | + |
| 125 | + private function getRepository(int $savesCount, int $createsCount): MockObject|FileRepository |
| 126 | + { |
| 127 | + $originalRepository = new FileRepository(null); |
| 128 | + $repository = $this->createMock(FileRepository::class); |
| 129 | + $repository->expects(new InvokedCount($savesCount))->method('save'); |
| 130 | + $repository->expects(new InvokedCount($createsCount)) |
| 131 | + ->method('create') |
| 132 | + ->will($this->returnCallback(fn ($arg) => $originalRepository->create($arg))); |
| 133 | + return $repository; |
| 134 | + } |
| 135 | +} |
0 commit comments