diff --git a/lib/SP/Domain/Import/Services/XmlFile.php b/lib/SP/Domain/Import/Services/XmlFile.php
index f6679bf5..8132bf4e 100644
--- a/lib/SP/Domain/Import/Services/XmlFile.php
+++ b/lib/SP/Domain/Import/Services/XmlFile.php
@@ -40,7 +40,7 @@ final class XmlFile implements XmlFileService
{
private readonly DOMDocument $document;
- private function __construct()
+ public function __construct()
{
$this->createDocument();
}
@@ -78,8 +78,6 @@ final class XmlFile implements XmlFileService
{
libxml_use_internal_errors(true);
- $this->createDocument();
-
if ($this->document->load($file, LIBXML_PARSEHUGE) === false) {
foreach (libxml_get_errors() as $error) {
logger(__METHOD__ . ' - ' . $error->message);
@@ -104,8 +102,7 @@ final class XmlFile implements XmlFileService
throw ImportException::error(
__u('XML file not supported'),
__u('Unable to guess the application which data was exported from'),
- $e->getCode(),
- $e
+ $e->getCode()
);
}
}
diff --git a/tests/SPT/Domain/Import/Services/XmlFileTest.php b/tests/SPT/Domain/Import/Services/XmlFileTest.php
new file mode 100644
index 00000000..14275829
--- /dev/null
+++ b/tests/SPT/Domain/Import/Services/XmlFileTest.php
@@ -0,0 +1,127 @@
+.
+ */
+
+namespace SPT\Domain\Import\Services;
+
+use SP\Domain\Import\Services\ImportException;
+use SP\Domain\Import\Services\XmlFile;
+use SP\Domain\Import\Services\XmlFormat;
+use SP\Infrastructure\File\FileException;
+use SP\Infrastructure\File\FileHandler;
+use SPT\UnitaryTestCase;
+
+/**
+ * Class XmlFileTest
+ *
+ * @group unitary
+ */
+class XmlFileTest extends UnitaryTestCase
+{
+ private const KEEPASS_FILE = RESOURCE_PATH . DIRECTORY_SEPARATOR . 'import' . DIRECTORY_SEPARATOR .
+ 'data_keepass.xml';
+ private const SYSPASS_FILE = RESOURCE_PATH . DIRECTORY_SEPARATOR . 'import' . DIRECTORY_SEPARATOR .
+ 'data_syspass.xml';
+
+ public static function fileFormatProvider(): array
+ {
+ return [
+ [self::KEEPASS_FILE, XmlFormat::Keepass],
+ [self::SYSPASS_FILE, XmlFormat::Syspass],
+ ];
+ }
+
+ /**
+ * @dataProvider fileFormatProvider
+ *
+ * @throws ImportException
+ * @throws FileException
+ */
+ public function testDetectFormat(string $file, XmlFormat $format)
+ {
+ $fileHandler = new FileHandler($file);
+
+ $xmlFile = new XmlFile();
+ $out = $xmlFile->builder($fileHandler)->detectFormat();
+
+ $this->assertEquals($format, $out);
+ }
+
+ /**
+ * @throws ImportException
+ * @throws FileException
+ */
+ public function testDetectFormatWithException()
+ {
+ $fileHandler = new FileHandler(self::$faker->filePath(), 'w');
+ $fileHandler->write('');
+
+ $xmlFile = new XmlFile();
+
+ $this->expectException(ImportException::class);
+ $this->expectExceptionMessage('XML file not supported');
+
+ $xmlFile->builder($fileHandler)->detectFormat();
+ }
+
+ /**
+ * @throws ImportException
+ * @throws FileException
+ */
+ public function testBuilder()
+ {
+ $fileHandler = new FileHandler(self::$faker->filePath(), 'w');
+ $fileHandler->write('');
+
+ $xmlFile = new XmlFile();
+ $out = $xmlFile->builder($fileHandler);
+
+ $this->assertFalse(spl_object_id($xmlFile) === spl_object_id($out));
+ }
+
+ /**
+ * @throws ImportException
+ * @throws FileException
+ */
+ public function testBuilderWithException()
+ {
+ $fileHandler = new FileHandler(self::$faker->filePath(), 'w');
+ $fileHandler->write('');
+
+ $xmlFile = new XmlFile();
+
+ $this->expectException(ImportException::class);
+ $this->expectExceptionMessage('Internal error');
+
+ $xmlFile->builder($fileHandler);
+ }
+
+ public function testGetDocument()
+ {
+ $xmlFile = new XmlFile();
+ $out = $xmlFile->getDocument();
+
+ $this->assertFalse($out->formatOutput);
+ $this->assertFalse($out->preserveWhiteSpace);
+ }
+}