|
| 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 Carbon\Carbon; |
| 15 | +use Hyperf\Config\Config; |
| 16 | +use Hyperf\Filesystem\FilesystemFactory; |
| 17 | +use League\Flysystem\Filesystem; |
| 18 | +use OnixSystemsPHP\HyperfCore\Constants\Time; |
| 19 | +use OnixSystemsPHP\HyperfCore\Contract\CoreAuthenticatable; |
| 20 | +use OnixSystemsPHP\HyperfFileUpload\Model\File; |
| 21 | +use OnixSystemsPHP\HyperfFileUpload\Repository\FileRepository; |
| 22 | +use OnixSystemsPHP\HyperfFileUpload\Service\ClearUnusedDeletedFilesService; |
| 23 | +use OnixSystemsPHP\HyperfFileUpload\Test\Cases\AppTest; |
| 24 | +use OnixSystemsPHP\HyperfFileUpload\Test\Fixtures\FilesFixture; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use PHPUnit\Framework\MockObject\Rule\InvokedCount; |
| 27 | + |
| 28 | +/** |
| 29 | + * @internal |
| 30 | + * @coversNothing |
| 31 | + */ |
| 32 | +class ClearUnusedDeletedFilesServiceTest extends AppTest |
| 33 | +{ |
| 34 | + private array $files; |
| 35 | + |
| 36 | + private ?array $finderResult = null; |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->createContainer(); |
| 43 | + |
| 44 | + $this->user = $this->user = $this->createMock(CoreAuthenticatable::class); |
| 45 | + $this->user->method('getId')->willReturn(1); |
| 46 | + |
| 47 | + $this->files = $this->fillFiles(); |
| 48 | + } |
| 49 | + |
| 50 | + public function testMain() |
| 51 | + { |
| 52 | + $service = $this->getContainer(1, 3); |
| 53 | + $this->assertEquals(3, $service->run()); |
| 54 | + $this->assertEquals(3, count($this->files)); |
| 55 | + } |
| 56 | + |
| 57 | + protected function getContainer(int $nEvents, int $nDirectoryDelete): ClearUnusedDeletedFilesService |
| 58 | + { |
| 59 | + $fileSystemMock = $this->createMock(Filesystem::class); |
| 60 | + $fileSystemMock->expects(new InvokedCount($nDirectoryDelete))->method('deleteDirectory'); |
| 61 | + $fileSystemFactoryMock = $this->createMock(FilesystemFactory::class); |
| 62 | + $fileSystemFactoryMock->method('get')->willReturn($fileSystemMock); |
| 63 | + return new ClearUnusedDeletedFilesService( |
| 64 | + $this->getConfig(), |
| 65 | + $fileSystemFactoryMock, |
| 66 | + $this->getRepository(), |
| 67 | + $this->getEventDispatcherMock($nEvents), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private function getConfig(): Config |
| 72 | + { |
| 73 | + $config = new Config([]); |
| 74 | + $config->set('file_upload.unused_file_max_lifetime', Time::DAY); |
| 75 | + return $config; |
| 76 | + } |
| 77 | + |
| 78 | + private function getRepository(): MockObject|FileRepository |
| 79 | + { |
| 80 | + $repository = $this->getMockBuilder(FileRepository::class) |
| 81 | + ->setConstructorArgs([null]) |
| 82 | + ->setMethods(['finder', 'limit', 'get', 'all', 'forceDelete']) |
| 83 | + ->getMock(); |
| 84 | + |
| 85 | + $repository->method('limit')->willReturn($repository); |
| 86 | + $repository->method('get')->willReturn($repository); |
| 87 | + $repository->method('get')->willReturn($repository); |
| 88 | + |
| 89 | + $repository->method('all') |
| 90 | + ->will($this->returnCallback(fn () => $this->finderResult)); |
| 91 | + |
| 92 | + $repository->method('finder') |
| 93 | + ->will($this->returnCallback(fn ($arg1, $arg2 = null) => $this->finder($repository, $arg1, $arg2))); |
| 94 | + |
| 95 | + $repository->method('forceDelete')->will($this->returnCallback(fn ($arg) => $this->deleteFile($arg))); |
| 96 | + |
| 97 | + return $repository; |
| 98 | + } |
| 99 | + |
| 100 | + private function fillFiles(): array |
| 101 | + { |
| 102 | + $files['fileAssignedInLoyalPeriod'] = new File(array_merge(FilesFixture::image1(), [ |
| 103 | + 'id' => 1, |
| 104 | + 'user_id' => $this->user->getId(), |
| 105 | + 'fileable_id' => 1, |
| 106 | + 'fileable_type' => 'App\Model\SomeModel', |
| 107 | + 'field_name' => 'field', |
| 108 | + ])); |
| 109 | + |
| 110 | + $files['fileAssignedInLoyalPeriod']->setDateFormat('Y-m-d h:i:s'); |
| 111 | + $files['fileAssignedInLoyalPeriod']->created_at = Carbon::now()->subHours(20); |
| 112 | + |
| 113 | + $files['fileAssignedNotInLoyalPeriod'] = new File(array_merge(FilesFixture::image1(), [ |
| 114 | + 'id' => 2, |
| 115 | + 'user_id' => $this->user->getId(), |
| 116 | + 'fileable_id' => 1, |
| 117 | + 'fileable_type' => 'App\Model\SomeModel', |
| 118 | + 'field_name' => 'field', |
| 119 | + ])); |
| 120 | + |
| 121 | + $files['fileAssignedNotInLoyalPeriod']->setDateFormat('Y-m-d h:i:s'); |
| 122 | + $files['fileAssignedNotInLoyalPeriod']->created_at = Carbon::now()->subHours(25); |
| 123 | + |
| 124 | + $files['fileNotAssignedInLoyalPeriod'] = new File(array_merge(FilesFixture::image1(), [ |
| 125 | + 'id' => 3, |
| 126 | + 'user_id' => $this->user->getId(), |
| 127 | + ])); |
| 128 | + |
| 129 | + $files['fileNotAssignedInLoyalPeriod']->setDateFormat('Y-m-d h:i:s'); |
| 130 | + $files['fileNotAssignedInLoyalPeriod']->created_at = Carbon::now()->subHours(10); |
| 131 | + |
| 132 | + $files['fileNotAssignedNotInLoyalPeriod'] = new File(array_merge(FilesFixture::image1(), [ |
| 133 | + 'id' => 4, |
| 134 | + 'user_id' => $this->user->getId(), |
| 135 | + ])); |
| 136 | + $files['fileNotAssignedNotInLoyalPeriod']->setDateFormat('Y-m-d h:i:s'); |
| 137 | + $files['fileNotAssignedNotInLoyalPeriod']->created_at = Carbon::now()->subHours(25); |
| 138 | + |
| 139 | + $files['fileAssignedDeleted'] = new File(array_merge(FilesFixture::image1(), [ |
| 140 | + 'id' => 5, |
| 141 | + 'user_id' => $this->user->getId(), |
| 142 | + 'fileable_id' => 1, |
| 143 | + 'fileable_type' => 'App\Model\SomeModel', |
| 144 | + 'field_name' => 'field', |
| 145 | + ])); |
| 146 | + $files['fileAssignedDeleted']->setDateFormat('Y-m-d h:i:s'); |
| 147 | + $files['fileAssignedDeleted']->created_at = Carbon::now()->subHours(1); |
| 148 | + |
| 149 | + $files['fileNotAssignedDeleted'] = new File(array_merge(FilesFixture::image1(), [ |
| 150 | + 'id' => 6, |
| 151 | + 'user_id' => $this->user->getId(), |
| 152 | + 'path' => 'errorPath', |
| 153 | + ])); |
| 154 | + $files['fileNotAssignedDeleted']->setDateFormat('Y-m-d h:i:s'); |
| 155 | + $files['fileNotAssignedDeleted']->created_at = Carbon::now()->subHours(1); |
| 156 | + $files['fileNotAssignedDeleted']->deleted_at = Carbon::now()->subHours(1); |
| 157 | + |
| 158 | + return $files; |
| 159 | + } |
| 160 | + |
| 161 | + private function finder(FileRepository $repository, string $arg1, ?Carbon $arg2 = null): MockObject|FileRepository |
| 162 | + { |
| 163 | + if ($arg1 === 'olderThan' && ! empty($arg2)) { |
| 164 | + foreach ($this->files as $file) { |
| 165 | + if ($file->created_at > $arg2) { |
| 166 | + $files[] = $file; |
| 167 | + } |
| 168 | + } |
| 169 | + $this->finderResult = $files ?? []; |
| 170 | + } |
| 171 | + return $repository; |
| 172 | + } |
| 173 | + |
| 174 | + private function deleteFile(File $file): bool |
| 175 | + { |
| 176 | + $this->files = array_filter($this->files, fn (File $f) => $file->id !== $f->id); |
| 177 | + return true; |
| 178 | + } |
| 179 | +} |
0 commit comments