From 7f84c30de8cdf4662678df36cd38e563aaee45b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 2 Mar 2024 10:21:51 +0100 Subject: [PATCH] chore(tests): UT for ImportStrategy service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Domain/Import/Services/ImportStrategy.php | 61 ++++++++----------- .../Import/Services/ImportStrategyTest.php | 48 +++++++++++++++ 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/lib/SP/Domain/Import/Services/ImportStrategy.php b/lib/SP/Domain/Import/Services/ImportStrategy.php index f8f2bf5d..3d74f6d6 100644 --- a/lib/SP/Domain/Import/Services/ImportStrategy.php +++ b/lib/SP/Domain/Import/Services/ImportStrategy.php @@ -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() + ) + }; } } diff --git a/tests/SPT/Domain/Import/Services/ImportStrategyTest.php b/tests/SPT/Domain/Import/Services/ImportStrategyTest.php index 1ebbedb3..c8ac886a 100644 --- a/tests/SPT/Domain/Import/Services/ImportStrategyTest.php +++ b/tests/SPT/Domain/Import/Services/ImportStrategyTest.php @@ -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();