chore(tests): UT for ImportStrategy service

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-03-02 10:21:51 +01:00
parent 7df5384234
commit 7f84c30de8
2 changed files with 75 additions and 34 deletions

View File

@@ -79,21 +79,16 @@ final class ImportStrategy extends Service implements ImportStrategyService
private function fileTypeFactory(ImportParamsDto $importParams): ItemsImportService
{
$fileHandler = $importParams->getFile();
$fileType = $this->checkFile($fileHandler);
switch ($fileType) {
case 'text/plain':
case 'text/csv':
return new CsvImport($this->application, $this->importHelper, $this->crypt, $fileHandler);
case 'text/xml':
case 'application/xml':
return $this->xmlFactory($fileHandler);
}
throw ImportException::error(
sprintf(__('Mime type not supported ("%s")'), $fileType),
__u('Please, check the file format')
);
return match ($this->checkFile($fileHandler)) {
'text/plain', 'text/csv' => new CsvImport(
$this->application,
$this->importHelper,
$this->crypt,
$fileHandler
),
'text/xml', 'application/xml' => $this->xmlFactory($fileHandler)
};
}
/**
@@ -109,8 +104,8 @@ final class ImportStrategy extends Service implements ImportStrategyService
if (!in_array($fileType, self::ALLOWED_MIME)) {
throw ImportException::error(
__u('File type not allowed'),
sprintf(__('MIME type: %s'), $fileType)
sprintf(__('Mime type not supported ("%s")'), $fileType),
__u('Please, check the file format')
);
}
@@ -135,23 +130,21 @@ final class ImportStrategy extends Service implements ImportStrategyService
{
$xmlFile = $this->xmlFile->builder($fileHandler);
switch ($xmlFile->detectFormat()) {
case XmlFormat::Syspass:
return new SyspassImport(
$this->application,
$this->importHelper,
$this->crypt,
$xmlFile->getDocument()
);
case XmlFormat::Keepass:
return new KeepassImport(
$this->application,
$this->importHelper,
$this->crypt,
$xmlFile->getDocument()
);
}
throw ImportException::error(__u('Format not detected'));
return match ($xmlFile->detectFormat()) {
XmlFormat::Syspass =>
new SyspassImport(
$this->application,
$this->importHelper,
$this->crypt,
$xmlFile->getDocument()
),
XmlFormat::Keepass =>
new KeepassImport(
$this->application,
$this->importHelper,
$this->crypt,
$xmlFile->getDocument()
)
};
}
}

View File

@@ -176,6 +176,54 @@ class ImportStrategyTest extends UnitaryTestCase
self::assertInstanceOf(KeepassImport::class, $out);
}
/**
* @throws ImportException
* @throws Exception
* @throws FileException
*/
public function testBuildImportWithFileException()
{
$fileHandler = $this->createMock(FileHandlerInterface::class);
$fileHandler->expects(self::once())
->method('checkIsReadable')
->willThrowException(FileException::error('test'));
$importParamsDto = self::createMock(ImportParamsDto::class);
$importParamsDto->expects(self::once())
->method('getFile')
->willReturn($fileHandler);
$this->expectException(FileException::class);
$this->expectExceptionMessage('Internal error while reading the file');
$this->importStrategy->buildImport($importParamsDto);
}
/**
* @throws ImportException
* @throws Exception
* @throws FileException
*/
public function testBuildImportWithMimeTypeException()
{
$fileHandler = $this->createMock(FileHandlerInterface::class);
$fileHandler->expects(self::once())
->method('checkIsReadable');
$fileHandler->expects(self::once())
->method('getFileType')
->willReturn('a_file_type');
$importParamsDto = self::createMock(ImportParamsDto::class);
$importParamsDto->expects(self::once())
->method('getFile')
->willReturn($fileHandler);
$this->expectException(ImportException::class);
$this->expectExceptionMessage('Mime type not supported ("a_file_type")');
$this->importStrategy->buildImport($importParamsDto);
}
protected function setUp(): void
{
parent::setUp();