mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-14 04:16:54 +01:00
chore(tests): UT for XmlFile service
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
127
tests/SPT/Domain/Import/Services/XmlFileTest.php
Normal file
127
tests/SPT/Domain/Import/Services/XmlFileTest.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/*
|
||||
* sysPass
|
||||
*
|
||||
* @author nuxsmin
|
||||
* @link https://syspass.org
|
||||
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
|
||||
*
|
||||
* This file is part of sysPass.
|
||||
*
|
||||
* sysPass is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* sysPass is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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('<?xml version="1.0" encoding="utf-8" standalone="yes"?><Test></Test>');
|
||||
|
||||
$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('<?xml version="1.0" encoding="utf-8" standalone="yes"?><Test></Test>');
|
||||
|
||||
$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('<?xml version="1.0" encoding="utf-8" standalone="yes"?><Test>');
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user