From 3675d501f51ec80df4cc5f078abbe1ee05c210d7 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 16 May 2025 10:15:05 +0200 Subject: [PATCH] add tests --- .gitignore | 3 + .../Controller/orderOverviewPdfGenerator.php | 12 +- .../de/pdfdocuments_translations.php | 2 + .../en/pdfdocuments_translations.php | 2 + Application/views/de/translations.php | 1 + Application/views/en/translations.php | 1 + .../orderOverviewPdfGeneratorTest.php | 116 ++++++++++++++++++ metadata.php | 2 + services.yaml | 10 ++ 9 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 Tests/Unit/Application/Controller/orderOverviewPdfGeneratorTest.php create mode 100644 services.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6bbea72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.phpunit.result.cache +Tests/.phpunit.result.cache +Tests/result \ No newline at end of file diff --git a/Application/Controller/orderOverviewPdfGenerator.php b/Application/Controller/orderOverviewPdfGenerator.php index bd860f0..fcb2bc9 100644 --- a/Application/Controller/orderOverviewPdfGenerator.php +++ b/Application/Controller/orderOverviewPdfGenerator.php @@ -19,8 +19,12 @@ use D3\PdfDocuments\Application\Model\Exceptions\noPdfHandlerFoundException; use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface; use D3\PdfDocuments\Application\Model\Registries\registryOrderoverview; +use D3\PdfDocuments\Application\Model\Registries\registryOrderoverviewInterface; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class orderOverviewPdfGenerator { @@ -37,15 +41,17 @@ class orderOverviewPdfGenerator } /** - * @return pdfdocumentsOrderInterface - * @throws noPdfHandlerFoundException * @throws wrongPdfGeneratorInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws noPdfHandlerFoundException */ public function getPdfClass(): pdfdocumentsOrderInterface { $requestedType = Registry::getRequest()->getRequestParameter('pdftype'); - $generatorList = oxNew(registryOrderoverview::class); + /** @var registryOrderoverview $generatorList */ + $generatorList = ContainerFactory::getInstance()->getContainer()->get(registryOrderoverviewInterface::class); /** @var pdfdocumentsOrderInterface $generator */ foreach ($generatorList->getList() as $generator) { if ($generator->getRequestId() == $requestedType) { diff --git a/Application/translations/de/pdfdocuments_translations.php b/Application/translations/de/pdfdocuments_translations.php index fbab755..2dd981b 100644 --- a/Application/translations/de/pdfdocuments_translations.php +++ b/Application/translations/de/pdfdocuments_translations.php @@ -11,6 +11,7 @@ * @link https://www.oxidmodule.com */ +// @codeCoverageIgnoreStart $sLangName = "Deutsch"; return [ 'charset' => 'utf-8', @@ -78,3 +79,4 @@ return [ 'D3_PDFDOCUMENTS_BANK_BANKCODE' => 'BIC/SWIFT-Code', ]; +// @codeCoverageIgnoreEnd diff --git a/Application/translations/en/pdfdocuments_translations.php b/Application/translations/en/pdfdocuments_translations.php index 7ea9282..2111142 100644 --- a/Application/translations/en/pdfdocuments_translations.php +++ b/Application/translations/en/pdfdocuments_translations.php @@ -11,6 +11,7 @@ * @link https://www.oxidmodule.com */ +// @codeCoverageIgnoreStart return [ 'charset' => 'utf-8', @@ -77,3 +78,4 @@ return [ 'D3_PDFDOCUMENTS_BANK_BANKCODE' => 'BIC/SWIFT Code', ]; +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/Application/views/de/translations.php b/Application/views/de/translations.php index 10ffe22..711658a 100644 --- a/Application/views/de/translations.php +++ b/Application/views/de/translations.php @@ -42,3 +42,4 @@ return [ 'D3_PDFDOCUMENTS_LANGUAGE' => 'Sprache', 'D3_PDFDOCUMENTS_PDF_GENERATE' => 'Dokument erstellen', ]; +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/Application/views/en/translations.php b/Application/views/en/translations.php index 464034e..32650be 100644 --- a/Application/views/en/translations.php +++ b/Application/views/en/translations.php @@ -42,3 +42,4 @@ return [ 'D3_PDFDOCUMENTS_LANGUAGE' => 'Language', 'D3_PDFDOCUMENTS_PDF_GENERATE' => 'Create Document', ]; +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/Tests/Unit/Application/Controller/orderOverviewPdfGeneratorTest.php b/Tests/Unit/Application/Controller/orderOverviewPdfGeneratorTest.php new file mode 100644 index 0000000..8281343 --- /dev/null +++ b/Tests/Unit/Application/Controller/orderOverviewPdfGeneratorTest.php @@ -0,0 +1,116 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\PdfDocuments\Tests\Unit\Application\Controller; + +use Assert\InvalidArgumentException; +use D3\PdfDocuments\Application\Controller\orderOverviewPdfGenerator; +use D3\PdfDocuments\Application\Model\AbstractClasses\pdfdocumentsGeneric; +use D3\PdfDocuments\Application\Model\Constants; +use D3\PdfDocuments\Application\Model\Documents\invoicePdf; +use D3\PdfDocuments\Application\Model\Exceptions\noPdfHandlerFoundException; +use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsGenericInterface as genericInterface; +use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface; +use D3\PdfDocuments\Application\Model\Registries\registryOrderoverview; +use D3\PdfDocuments\Application\Model\Registries\registryOrderoverviewInterface; +use D3\TestingTools\Development\CanAccessRestricted; +use Generator; +use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Core\Base; +use OxidEsales\Eshop\Core\Exception\StandardException; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\UtilsView; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface; +use OxidEsales\Twig\Resolver\TemplateChain\TemplateNotInChainException; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use ReflectionException; +use Spipu\Html2Pdf\Exception\Html2PdfException; +use Spipu\Html2Pdf\Html2Pdf; +use Symfony\Component\String\UnicodeString; +use Twig\Error\Error; + +class orderOverviewPdfGeneratorTest extends TestCase +{ + use CanAccessRestricted; + + /** + * @test + * @covers \D3\PdfDocuments\Application\Controller\orderOverviewPdfGenerator::generatePdf + * @throws ReflectionException + */ + public function testGeneratePdf(): void + { + $document = $this->getMockBuilder(invoicePdf::class) + ->onlyMethods(['downloadPdf']) + ->getMock(); + $document->expects($this->once())->method('downloadPdf'); + + $sut = $this->getMockBuilder(orderOverviewPdfGenerator::class) + ->onlyMethods(['getPdfClass']) + ->getMock(); + $sut->expects($this->once())->method('getPdfClass')->willReturn($document); + + $this->callMethod( + $sut, + 'generatePdf', + [oxNew(Order::class)] + ); + } + + /** + * @test + * @covers \D3\PdfDocuments\Application\Controller\orderOverviewPdfGenerator::getPdfClass + * @throws ReflectionException + * @dataProvider getPdfClassDataProvider + */ + public function testGetPdfClass(array $generatorList, string $request, bool $expectException): void + { + $_GET['pdftype'] = $request; + + $orderOverviewRegistry = $this->getMockBuilder(registryOrderoverview::class) + ->onlyMethods(['getList']) + ->getMock(); + $orderOverviewRegistry->method('getList')->willReturn($generatorList); + + $this->addServiceMocks([ + registryOrderoverviewInterface::class => $orderOverviewRegistry, + ]); + + $sut = oxNew(orderOverviewPdfGenerator::class); + + if ($expectException) { + $this->expectException(noPdfHandlerFoundException::class); + } + + $this->assertInstanceOf( + pdfdocumentsOrderInterface::class, + $this->callMethod( + $sut, + 'getPdfClass', + ) + ); + } + + public static function getPdfClassDataProvider(): Generator + { + yield 'no generator set' => [[], 'foo', true]; + yield 'unknown generator set' => [[new invoicePdf()], 'foo', true]; + yield 'valid generator set' => [[new invoicePdf()], 'invoice', false]; + } +} \ No newline at end of file diff --git a/metadata.php b/metadata.php index 5b01dfb..649340b 100644 --- a/metadata.php +++ b/metadata.php @@ -15,6 +15,7 @@ use D3\PdfDocuments\Application\Model\Constants as Constants; use D3\PdfDocuments\Modules\Application\Controller\d3_overview_controller_pdfdocuments; use OxidEsales\Eshop\Application\Controller\Admin\OrderOverview; +// @codeCoverageIgnoreStart $sMetadataVersion = '2.1'; $aModule = [ @@ -100,3 +101,4 @@ $aModule = [ ], ], ]; +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/services.yaml b/services.yaml new file mode 100644 index 0000000..1910701 --- /dev/null +++ b/services.yaml @@ -0,0 +1,10 @@ +services: + _defaults: + autowire: false + public: true + + D3\PdfDocuments\Application\Model\Registries\registryOrderoverviewInterface: + factory: 'oxNew' + shared: true + arguments: + - D3\PdfDocuments\Application\Model\Registries\registryOrderoverview \ No newline at end of file