. */ namespace SP\Tests\Domain\Import\Services; use PHPUnit\Framework\Attributes\DataProvider; 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 SP\Tests\UnitaryTestCase; use PHPUnit\Framework\Attributes\Group; /** * 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], ]; } /** * @throws ImportException * @throws FileException */ #[DataProvider('fileFormatProvider')] 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); } }