diff --git a/tests/SPT/Domain/Export/Services/XmlAccountExportTest.php b/tests/SPT/Domain/Export/Services/XmlAccountExportTest.php new file mode 100644 index 00000000..6a088dc4 --- /dev/null +++ b/tests/SPT/Domain/Export/Services/XmlAccountExportTest.php @@ -0,0 +1,171 @@ +. + */ + +namespace SPT\Domain\Export\Services; + +use DOMDocument; +use DOMNodeList; +use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; +use RuntimeException; +use SP\DataModel\ItemData; +use SP\Domain\Account\Ports\AccountService; +use SP\Domain\Account\Ports\AccountToTagService; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\Export\Services\XmlAccountExport; +use SPT\Generators\AccountDataGenerator; +use SPT\UnitaryTestCase; + +/** + * Class XmlAccountExportTest + * + * @group unitary + */ +class XmlAccountExportTest extends UnitaryTestCase +{ + + private AccountService|MockObject $accountService; + private AccountToTagService|MockObject $accountToTagService; + private XmlAccountExport $xmlAccountExport; + + /** + * @throws Exception + * @throws ServiceException + */ + public function testExport() + { + $account = AccountDataGenerator::factory()->buildAccount(); + $tag = new ItemData(['id' => self::$faker->randomNumber(3)]); + + $document = new DOMDocument(); + + $this->accountService + ->expects(self::once()) + ->method('getAllBasic') + ->willReturn([$account]); + + $this->accountToTagService + ->expects(self::once()) + ->method('getTagsByAccountId') + ->with($account->getId()) + ->willReturn([$tag]); + + $out = $this->xmlAccountExport->export($document); + + $this->assertEquals('Accounts', $out->nodeName); + $this->assertEquals(9, $out->firstChild->childNodes->count()); + + $nodes = [ + 'name' => $account->getName(), + 'clientId' => $account->getClientId(), + 'categoryId' => $account->getCategoryId(), + 'login' => $account->getLogin(), + 'url' => $account->getUrl(), + 'notes' => $account->getNotes(), + 'pass' => $account->getPass(), + 'key' => $account->getKey(), + 'tags' => '', + ]; + + $this->checkNodes($out->firstChild->childNodes, $nodes); + + $tagsNode = $out->firstChild->childNodes->item(8); + + $this->assertEquals('tag', $tagsNode->firstChild->nodeName); + $this->assertEquals( + $tag->getId(), + $tagsNode->firstChild->attributes->getNamedItem('id')->nodeValue + ); + } + + private function checkNodes(DOMNodeList $nodeList, array $nodes): void + { + $names = array_keys($nodes); + $values = array_values($nodes); + + foreach ($names as $key => $nodeName) { + $this->assertEquals($nodeName, $nodeList->item($key)->nodeName); + $this->assertEquals($values[$key], $nodeList->item($key)->nodeValue); + } + } + + /** + * @throws Exception + * @throws ServiceException + */ + public function testExportWithoutAccounts() + { + $document = new DOMDocument(); + + $this->accountService + ->expects(self::once()) + ->method('getAllBasic') + ->willReturn([]); + + $this->accountToTagService + ->expects(self::never()) + ->method('getTagsByAccountId'); + + $out = $this->xmlAccountExport->export($document); + + $this->assertEquals('Accounts', $out->nodeName); + $this->assertEquals(0, $out->childNodes->count()); + } + + /** + * @throws Exception + * @throws ServiceException + */ + public function testExportWithException() + { + $document = new DOMDocument(); + + $this->accountService + ->expects(self::once()) + ->method('getAllBasic') + ->willThrowException(new RuntimeException('test')); + + $this->accountToTagService + ->expects(self::never()) + ->method('getTagsByAccountId'); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('test'); + $this->xmlAccountExport->export($document); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->accountService = $this->createMock(AccountService::class); + $this->accountToTagService = $this->createMock(AccountToTagService::class); + + $this->xmlAccountExport = new XmlAccountExport( + $this->application, + $this->accountService, + $this->accountToTagService + ); + } +}