. */ namespace SP\Tests\Services\Export; use Defuse\Crypto\Exception\CryptoException; use DI\DependencyException; use DI\NotFoundException; use SP\Core\Context\ContextException; use SP\Services\Export\VerifyResult; use SP\Services\Export\XmlExportService; use SP\Services\Export\XmlVerifyService; use SP\Services\ServiceException; use SP\Storage\File\FileException; use SP\Tests\DatabaseTestCase; use SP\Util\PasswordUtil; use function SP\Tests\setupContext; /** * Class XmlExportServiceTest * * @package SP\Tests\Services\Export */ class XmlExportServiceTest extends DatabaseTestCase { /** * @var XmlExportService */ private static $xmlExportService; /** * @var XmlVerifyService */ private static $xmlVerifyService; /** * @throws DependencyException * @throws NotFoundException * @throws ContextException */ public static function setUpBeforeClass(): void { array_map('unlink', glob(TMP_PATH . DIRECTORY_SEPARATOR . '*.xml')); $dic = setupContext(); self::$loadFixtures = true; self::$xmlExportService = $dic->get(XmlExportService::class); self::$xmlVerifyService = $dic->get(XmlVerifyService::class); } /** * @throws ServiceException * @throws FileException */ public function testDoExportWithoutPassword() { self::$xmlExportService->doExport(TMP_PATH); $this->assertFileExists(self::$xmlExportService->getExportFile()); $this->verifyExportWithoutPassword(self::$xmlExportService->getExportFile()); } /** * @depends testDoExportWithoutPassword * * @param $file * * @throws ServiceException * @throws FileException */ private function verifyExportWithoutPassword($file) { $result = self::$xmlVerifyService->verify($file); $this->assertInstanceOf(VerifyResult::class, $result); $this->checkVerifyResult($result); } /** * @param VerifyResult $verifyResult */ private function checkVerifyResult(VerifyResult $verifyResult) { $nodes = $verifyResult->getNodes(); $this->assertCount(4, $nodes); $this->assertArrayHasKey('Account', $nodes); $this->assertArrayHasKey('Category', $nodes); $this->assertArrayHasKey('Client', $nodes); $this->assertArrayHasKey('Tag', $nodes); $this->assertEquals(4, $nodes['Account']); $this->assertEquals(3, $nodes['Category']); $this->assertEquals(4, $nodes['Client']); $this->assertEquals(3, $nodes['Tag']); } /** * @throws CryptoException * @throws ServiceException * @throws FileException */ public function testDoExportWithPassword() { $password = PasswordUtil::randomPassword(); self::$xmlExportService->doExport(TMP_PATH, $password); $this->assertFileExists(self::$xmlExportService->getExportFile()); $this->verifyExportWithPassword(self::$xmlExportService->getExportFile(), $password); } /** * @param $file * @param $password * * @throws CryptoException * @throws ServiceException * @throws FileException */ private function verifyExportWithPassword($file, $password) { $result = self::$xmlVerifyService->verifyEncrypted($file, $password); $this->assertInstanceOf(VerifyResult::class, $result); $this->assertTrue($result->isEncrypted()); $this->checkVerifyResult($result); $this->expectException(ServiceException::class); self::$xmlVerifyService->verifyEncrypted($file, 'test123'); } }