From f8220a30584b2a669aedc6fe4d3c2cd633ede121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 10 Feb 2024 18:29:49 +0100 Subject: [PATCH] chore(tests): UT for XmlClientExport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Export/Services/XmlCategoryExportTest.php | 2 - .../Export/Services/XmlClientExportTest.php | 138 ++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 tests/SPT/Domain/Export/Services/XmlClientExportTest.php diff --git a/tests/SPT/Domain/Export/Services/XmlCategoryExportTest.php b/tests/SPT/Domain/Export/Services/XmlCategoryExportTest.php index 97436f48..6844ea11 100644 --- a/tests/SPT/Domain/Export/Services/XmlCategoryExportTest.php +++ b/tests/SPT/Domain/Export/Services/XmlCategoryExportTest.php @@ -29,7 +29,6 @@ use DOMNodeList; use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use RuntimeException; -use SP\DataModel\ItemData; use SP\Domain\Category\Ports\CategoryService; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Export\Services\XmlCategoryExport; @@ -54,7 +53,6 @@ class XmlCategoryExportTest extends UnitaryTestCase public function testExport() { $category = CategoryGenerator::factory()->buildCategory(); - $tag = new ItemData(['id' => self::$faker->randomNumber(3)]); $document = new DOMDocument(); diff --git a/tests/SPT/Domain/Export/Services/XmlClientExportTest.php b/tests/SPT/Domain/Export/Services/XmlClientExportTest.php new file mode 100644 index 00000000..3d474399 --- /dev/null +++ b/tests/SPT/Domain/Export/Services/XmlClientExportTest.php @@ -0,0 +1,138 @@ +. + */ + +namespace SPT\Domain\Export\Services; + +use DOMDocument; +use DOMNodeList; +use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; +use RuntimeException; +use SP\Domain\Client\Ports\ClientService; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\Export\Services\XmlClientExport; +use SPT\Generators\ClientGenerator; +use SPT\UnitaryTestCase; + +/** + * Class XmlClientExportTest + * + * @group unitary + */ +class XmlClientExportTest extends UnitaryTestCase +{ + + private ClientService|MockObject $clientService; + private XmlClientExport $xmlClientExport; + + /** + * @throws Exception + * @throws ServiceException + */ + public function testExport() + { + $client = ClientGenerator::factory()->buildClient(); + + $document = new DOMDocument(); + + $this->clientService + ->expects(self::once()) + ->method('getAll') + ->willReturn([$client]); + + $out = $this->xmlClientExport->export($document); + + $this->assertEquals('Clients', $out->nodeName); + $this->assertEquals('Client', $out->firstChild->nodeName); + $this->assertEquals($client->getId(), $out->firstChild->attributes->getNamedItem('id')->nodeValue); + $this->assertEquals(2, $out->firstChild->childNodes->count()); + + $nodes = [ + 'name' => $client->getName(), + 'description' => $client->getDescription() + ]; + + $this->checkNodes($out->firstChild->childNodes, $nodes); + } + + 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 testExportWithoutCategories() + { + $document = new DOMDocument(); + + $this->clientService + ->expects(self::once()) + ->method('getAll') + ->willReturn([]); + + $out = $this->xmlClientExport->export($document); + + $this->assertEquals('Clients', $out->nodeName); + $this->assertEquals(0, $out->childNodes->count()); + } + + /** + * @throws Exception + * @throws ServiceException + */ + public function testExportWithException() + { + $document = new DOMDocument(); + + $this->clientService + ->expects(self::once()) + ->method('getAll') + ->willThrowException(new RuntimeException('test')); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('test'); + $this->xmlClientExport->export($document); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->clientService = $this->createMock(ClientService::class); + + $this->xmlClientExport = new XmlClientExport( + $this->application, + $this->clientService + ); + } +}