. */ namespace SP\Tests\Domain\Export\Services; use DOMDocument; 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 SP\Tests\Generators\ClientGenerator; use SP\Tests\UnitaryTestCase; use PHPUnit\Framework\Attributes\Group; /** * Class XmlClientExportTest * */ #[Group('unitary')] class XmlClientExportTest extends UnitaryTestCase { use XmlTrait; 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(); $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); } /** * @throws Exception * @throws ServiceException */ public function testExportWithoutCategories() { $document = new DOMDocument(); $this->clientService ->expects(self::once()) ->method('getAll') ->willReturn([]); $out = $this->xmlClientExport->export(); $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(); } protected function setUp(): void { parent::setUp(); $this->clientService = $this->createMock(ClientService::class); $this->xmlClientExport = new XmlClientExport( $this->application, $this->clientService ); } }