Skip to content

Commit 5d0cfc2

Browse files
authored
Merge pull request #2 from onix-systems-php/feature/PHPSB-71
feat(test): add service tests
2 parents d241e9a + a9abf5b commit 5d0cfc2

9 files changed

+668
-0
lines changed

tests/Cases/AppTest.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
13+
namespace OnixSystemsPHP\HyperfFileUpload\Test\Cases;
14+
15+
use Hyperf\Contract\ContainerInterface;
16+
use Hyperf\Event\EventDispatcher;
17+
use Hyperf\Utils\ApplicationContext;
18+
use OnixSystemsPHP\HyperfFileUpload\Test\Mocks\TestContainer;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* @internal
25+
* @coversNothing
26+
*/
27+
class AppTest extends TestCase
28+
{
29+
protected ContainerInterface $container;
30+
31+
public function tearDown(): void
32+
{
33+
\Mockery::close();
34+
}
35+
36+
public function testMain()
37+
{
38+
$this->assertTrue(true);
39+
}
40+
41+
protected function getServiceMock(string $className, int $nRuns): MockObject
42+
{
43+
$mock = $this->createMock($className);
44+
$mock->expects(new InvokedCount($nRuns))->method('run');
45+
return $mock;
46+
}
47+
48+
protected function getEventDispatcherMock(int $nEvents): MockObject|EventDispatcher
49+
{
50+
$mock = $this->createMock(EventDispatcher::class);
51+
$mock->expects(new InvokedCount($nEvents))->method('dispatch');
52+
return $mock;
53+
}
54+
55+
protected function createContainer(array $methods = []): void
56+
{
57+
$this->container = new TestContainer();
58+
59+
foreach ($methods as $key => $value) {
60+
$this->container->set($key, $value);
61+
}
62+
63+
ApplicationContext::setContainer($this->container);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
13+
namespace OnixSystemsPHP\HyperfFileUpload\Test\Cases\Service;
14+
15+
use Exception;
16+
use OnixSystemsPHP\HyperfFileUpload\Service\AddExternalFileService;
17+
use OnixSystemsPHP\HyperfFileUpload\Service\AddFileService;
18+
use OnixSystemsPHP\HyperfFileUpload\Service\DownloadFileService;
19+
use OnixSystemsPHP\HyperfFileUpload\Test\Cases\AppTest;
20+
21+
/**
22+
* @internal
23+
* @coversNothing
24+
*/
25+
class AddExternalFileServiceTest extends AppTest
26+
{
27+
protected function setUp(): void
28+
{
29+
$this->createContainer();
30+
parent::setUp();
31+
}
32+
33+
public function testMain()
34+
{
35+
$fileName = $this->createFile();
36+
$service = $this->getService($fileName);
37+
$service->run('fakeUrl', null);
38+
$this->assertTrue(true);
39+
unlink($fileName);
40+
}
41+
42+
public function testIfException()
43+
{
44+
$fileName = $this->createFile();
45+
$service = $this->getService($fileName, true);
46+
$this->expectException(Exception::class);
47+
$service->run('fakeUrl', null);
48+
$this->assertFileDoesNotExist($fileName);
49+
}
50+
51+
protected function getService(string $fileName, bool $expectException = false): AddExternalFileService
52+
{
53+
$downloadFileService = $this->createMock(DownloadFileService::class);
54+
$downloadFileService->expects($this->once())->method('run')->willReturn($fileName);
55+
$addFileService = $this->createMock(AddFileService::class);
56+
if ($expectException) {
57+
$addFileService->expects($this->once())->method('run')->willThrowException(new Exception());
58+
}
59+
return new AddExternalFileService(
60+
$downloadFileService,
61+
$addFileService,
62+
);
63+
}
64+
65+
private function createFile(): string
66+
{
67+
$path = __DIR__ . '/../../Mocks/TestFile.txt';
68+
file_put_contents($path, 'Test');
69+
return $path;
70+
}
71+
}
+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+
}

0 commit comments

Comments
 (0)