Skip to content

Commit a9abf5b

Browse files
committed
feat(test): move and adapt tests from the main repository
1 parent 7b9d2a9 commit a9abf5b

File tree

4 files changed

+389
-0
lines changed

4 files changed

+389
-0
lines changed
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
* @contact [email protected]
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
* @contact [email protected]
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+
}

tests/Fixtures/FilesFixture.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace OnixSystemsPHP\HyperfFileUpload\Test\Fixtures;
13+
14+
class FilesFixture
15+
{
16+
public static function image1(): array
17+
{
18+
return array_merge(self::schema(), [
19+
'mime' => 'image/jpeg',
20+
]);
21+
}
22+
23+
public static function document1(): array
24+
{
25+
return array_merge(self::schema(), [
26+
'name' => 'file1.pdf',
27+
'full_path' => '/path/to/file1.pdf',
28+
'url' => 'https://domain.com/path/to/file1.pdf',
29+
'original_name' => 'file1.pdf',
30+
'mime' => 'application/pdf',
31+
]);
32+
}
33+
34+
public static function document2(): array
35+
{
36+
return array_merge(self::schema(), [
37+
'name' => 'file2.pdf',
38+
'full_path' => '/path/to/file2.pdf',
39+
'url' => 'https://domain.com/path/to/file2.pdf',
40+
'original_name' => 'file2.pdf',
41+
'mime' => 'application/pdf',
42+
]);
43+
}
44+
45+
public static function document3(): array
46+
{
47+
return array_merge(self::schema(), [
48+
'name' => 'file3.pdf',
49+
'full_path' => '/path/to/file3.pdf',
50+
'url' => 'https://domain.com/path/to/file3.pdf',
51+
'original_name' => 'file3.pdf',
52+
'mime' => 'application/pdf',
53+
]);
54+
}
55+
56+
private static function schema()
57+
{
58+
return [
59+
'user_id' => null,
60+
'fileable_id' => null,
61+
'fileable_type' => null,
62+
'field_name' => null,
63+
'storage' => 'local',
64+
'path' => '/path/to',
65+
'name' => 'file.png',
66+
'full_path' => '/path/to/file.png',
67+
'domain' => 'https://domain.com',
68+
'url' => 'https://domain.com/path/to/file.png',
69+
'original_name' => 'file.png',
70+
'size' => '9999',
71+
'mime' => 'image/png',
72+
'presets' => [],
73+
];
74+
}
75+
}

tests/test-image.png

70 Bytes
Loading

0 commit comments

Comments
 (0)