diff --git a/Application/Model/Registries/registryOrdermanagerActions.php b/Application/Model/Registries/registryOrdermanagerActions.php index 4fa3caf..6eb0256 100644 --- a/Application/Model/Registries/registryOrdermanagerActions.php +++ b/Application/Model/Registries/registryOrdermanagerActions.php @@ -45,6 +45,9 @@ class registryOrdermanagerActions extends registryAbstract implements registryOr $this->addGenerator(deliverynotewithoutlogoPdf::class); } + /** + * @codeCoverageIgnore + */ public function getRequiredGeneratorInterfaceClassName(): string { return pdfdocumentsOrderInterface::class; diff --git a/Application/Model/Registries/registryOrderoverview.php b/Application/Model/Registries/registryOrderoverview.php index 6f90753..510738d 100644 --- a/Application/Model/Registries/registryOrderoverview.php +++ b/Application/Model/Registries/registryOrderoverview.php @@ -25,10 +25,14 @@ use D3\PdfDocuments\Application\Model\Interfaces\pdfdocumentsOrderInterface; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService; use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class registryOrderoverview extends registryAbstract implements registryOrderoverviewInterface { /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface * @throws wrongPdfGeneratorInterface */ public function __construct() @@ -36,7 +40,7 @@ class registryOrderoverview extends registryAbstract implements registryOrderove /** @var ModuleSettingService $settingsService */ $settingsService = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class); if ($settingsService->getBoolean('d3PdfDocumentsDocInvoice', Constants::OXID_MODULE_ID)) - $this->addGenerator( invoicePdf::class ); + $this->addGenerator(invoicePdf::class); if ($settingsService->getBoolean('d3PdfDocumentsDocDeliveryNote', Constants::OXID_MODULE_ID)) $this->addGenerator(deliverynotePdf::class); if ($settingsService->getBoolean('d3PdfDocumentsDocInvoiceNoLogo', Constants::OXID_MODULE_ID)) @@ -45,6 +49,9 @@ class registryOrderoverview extends registryAbstract implements registryOrderove $this->addGenerator(deliverynotewithoutlogoPdf::class); } + /** + * @codeCoverageIgnore + */ public function getRequiredGeneratorInterfaceClassName(): string { return pdfdocumentsOrderInterface::class; diff --git a/Application/views/de/translations.php b/Application/views/de/translations.php index 406cabd..b9696ce 100644 --- a/Application/views/de/translations.php +++ b/Application/views/de/translations.php @@ -23,9 +23,9 @@ $basicAuthHelp = << 'utf-8', - 'SHOP_MODULE_GROUP_'. Constants::OXID_MODULE_ID.'main' => 'Grundeinstellungen', - 'SHOP_MODULE_'. Constants::OXID_MODULE_ID.'bDev' => 'Entwicklermodus', - 'HELP_SHOP_MODULE_'. Constants::OXID_MODULE_ID.'bDev' => 'Mit aktiviertem Entwicklermodus kann das Dokument im '. + 'SHOP_MODULE_GROUP_'. Constants::OXID_MODULE_ID.'main' => 'Grundeinstellungen', + 'SHOP_MODULE_'. Constants::OXID_MODULE_ID.'bDev' => 'Entwicklermodus', + 'HELP_SHOP_MODULE_'. Constants::OXID_MODULE_ID.'bDev' => 'Mit aktiviertem Entwicklermodus kann das Dokument im '. 'SGML-Format ausgegeben werden. Inhaltliche Fehler können so besser nachvollzogen werden.', 'SHOP_MODULE_'. Constants::OXID_MODULE_ID.'basicAuthUserName' => 'BasicAuth des Shops - Benutzername (optional)', 'HELP_SHOP_MODULE_'. Constants::OXID_MODULE_ID.'basicAuthUserName' => $basicAuthHelp, @@ -53,7 +53,7 @@ return [ 'D3_PDFDOCUMENTS_PDF_TYPE' => 'Dokument', 'D3_PDFDOCUMENTS_LANGUAGE' => 'Sprache', - 'D3_PDFDOCUMENTS_SGML_GENERATE' => 'SGML erstellen', + 'D3_PDFDOCUMENTS_SGML_GENERATE' => 'Markup erstellen', '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 392ca3a..52d2012 100644 --- a/Application/views/en/translations.php +++ b/Application/views/en/translations.php @@ -53,7 +53,7 @@ return [ 'D3_PDFDOCUMENTS_PDF_TYPE' => 'Document', 'D3_PDFDOCUMENTS_LANGUAGE' => 'Language', - 'D3_PDFDOCUMENTS_SGML_GENERATE' => 'Create SGML', + 'D3_PDFDOCUMENTS_SGML_GENERATE' => 'Create markup', 'D3_PDFDOCUMENTS_PDF_GENERATE' => 'Create Document', ]; // @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/Tests/Unit/Application/Model/Registries/registryAbstract.php b/Tests/Unit/Application/Model/Registries/registryAbstract.php new file mode 100644 index 0000000..aea95c4 --- /dev/null +++ b/Tests/Unit/Application/Model/Registries/registryAbstract.php @@ -0,0 +1,195 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\PdfDocuments\Tests\Unit\Application\Model\Registries; + +use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; +use D3\PdfDocuments\Application\Model\Registries\registryOrderoverview; +use D3\PdfDocuments\Tests\Unit\Helpers\nonOrderDocument; +use D3\PdfDocuments\Tests\Unit\Helpers\orderDocument; +use D3\TestingTools\Development\CanAccessRestricted; +use Exception; +use Generator; +use PHPUnit\Framework\MockObject\Rule\InvocationOrder; +use PHPUnit\Framework\TestCase; +use ReflectionException; + +abstract class registryAbstract extends TestCase +{ + use CanAccessRestricted; + + /** + * @test + * @throws ReflectionException + * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::addGenerator + * @dataProvider addGeneratorDataProvider + */ + public function testAddGenerator(bool $exist, InvocationOrder $addInvocation, bool $expectException): void + { + $sut = $this->getMockBuilder(registryOrderoverview::class) + ->onlyMethods(['hasGenerator', 'addItem']) + ->getMock(); + $sut->method('hasGenerator')->willReturn($exist); + $sut->expects($addInvocation)->method('addItem'); + + if ($expectException) { + $this->expectException(Exception::class); + } + + $this->callMethod( + $sut, + 'addGenerator', + [orderDocument::class] + ); + } + + public static function addGeneratorDataProvider(): Generator + { + yield 'not added' => [false, self::once(), false]; + yield 'already added' => [true, self::never(), true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::addItem + * @dataProvider addItemDataProvider + */ + public function testAddItem(string $fqcn, bool $expectException): void + { + $sut = $this->getMockBuilder(registryOrderoverview::class) + ->disableOriginalConstructor() + ->onlyMethods(['addGenerator']) + ->getMock(); + + $this->assertArrayNotHasKey($fqcn, $this->getValue($sut, 'registry')); + + if ($expectException) { + $this->expectException(Exception::class); + } + + $this->callMethod( + $sut, + 'addItem', + [oxNew($fqcn)] + ); + + if ($expectException) { + $this->assertArrayNotHasKey($fqcn, $this->getValue($sut, 'registry')); + } else { + $this->assertArrayHasKey($fqcn, $this->getValue($sut, 'registry')); + } + } + + public static function addItemDataProvider(): Generator + { + yield 'right interface passed' => [orderDocument::class, false]; + yield 'wrong interface' => [nonOrderDocument::class, true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::removeGenerator + * @dataProvider removeGeneratorDataProvider + */ + public function testRemoveGenerator(bool $exist, bool $removed): void + { + $sut = $this->getMockBuilder(registryOrderoverview::class) + ->disableOriginalConstructor() + ->onlyMethods(['hasGenerator']) + ->getMock(); + $sut->method('hasGenerator')->willReturn($exist); + + $this->setValue( + $sut, + 'registry', + array_merge( + $this->getValue( + $sut, + 'registry', + ), + [orderDocument::class => oxNew(orderDocument::class)] + ) + ); + + $this->assertArrayHasKey(orderDocument::class, $this->getValue($sut, 'registry')); + + $this->callMethod( + $sut, + 'removeGenerator', + [orderDocument::class] + ); + + if ($removed) { + $this->assertArrayNotHasKey( orderDocument::class, $this->getValue( $sut, 'registry' ) ); + } else { + $this->assertArrayHasKey( orderDocument::class, $this->getValue( $sut, 'registry' ) ); + } + } + + public static function removeGeneratorDataProvider(): Generator + { + yield 'generator exists' => [true, true]; + yield 'generator does not exist' => [false, false]; + } + + /** + * @test + * @throws wrongPdfGeneratorInterface + * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::hasGenerator + */ + public function testHasGenerator(): void + { + $sut = oxNew(registryOrderoverview::class); + + $this->assertFalse($sut->hasGenerator(orderDocument::class)); + $sut->addGenerator(orderDocument::class); + $this->assertTrue($sut->hasGenerator(orderDocument::class)); + $sut->removeGenerator(orderDocument::class); + $this->assertFalse($sut->hasGenerator(orderDocument::class)); + } + + /** + * @test + * @throws wrongPdfGeneratorInterface + * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::getList + */ + public function testGetList(): void + { + $sut = oxNew(registryOrderoverview::class); + $list = $sut->getList(); + + $this->assertIsIterable($list); + $startCount = count($list); + + $sut->addGenerator(orderDocument::class); + $this->assertCount($startCount + 1, $sut->getList()); + + $sut->removeGenerator(orderDocument::class); + $this->assertCount($startCount, $sut->getList()); + } + + /** + * @test + * @throws wrongPdfGeneratorInterface + * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::clearList + */ + public function testClearList(): void + { + $sut = oxNew(registryOrderoverview::class); + $sut->addGenerator(orderDocument::class); + $sut->clearList(); + $this->assertCount(0, $sut->getList()); + } +} \ No newline at end of file diff --git a/Tests/Unit/Application/Model/Registries/registryOrderOverviewTest.php b/Tests/Unit/Application/Model/Registries/registryOrderOverviewTest.php index d01c137..1df1177 100644 --- a/Tests/Unit/Application/Model/Registries/registryOrderOverviewTest.php +++ b/Tests/Unit/Application/Model/Registries/registryOrderOverviewTest.php @@ -13,89 +13,89 @@ namespace D3\PdfDocuments\Tests\Unit\Application\Model\Registries; +use D3\PdfDocuments\Application\Model\Constants; use D3\PdfDocuments\Application\Model\Documents\invoicePdf; +use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; use D3\PdfDocuments\Application\Model\Registries\registryOrderoverview; use D3\PdfDocuments\Tests\Unit\Helpers\nonOrderDocument; +use D3\PdfDocuments\Tests\Unit\Helpers\orderDocument; use D3\TestingTools\Development\CanAccessRestricted; use Exception; use Generator; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; use PHPUnit\Framework\MockObject\Rule\InvocationOrder; use PHPUnit\Framework\TestCase; use ReflectionException; -class registryOrderOverviewTest extends TestCase +class registryOrderOverviewTest extends registryAbstract { - use CanAccessRestricted; - /** * @test * @throws ReflectionException - * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::addGenerator - * @dataProvider addGeneratorDataProvider + * @covers \D3\PdfDocuments\Application\Model\Registries\registryOrderoverview::__construct + * @dataProvider constructDataProvider */ - public function testAddGenerator(bool $exist, InvocationOrder $addInvocation, bool $expectException): void + public function testConstructor(bool $inv, bool $deln, bool $invNL, bool $delnNL, int $expectedCount): void { - $sut = $this->getMockBuilder(registryOrderoverview::class) - ->onlyMethods(['hasGenerator', 'addItem']) - ->getMock(); - $sut->method('hasGenerator')->willReturn($exist); - $sut->expects($addInvocation)->method('addItem'); - - if ($expectException) { - $this->expectException(Exception::class); - } - - $this->callMethod( - $sut, - 'addGenerator', - [invoicePdf::class] - ); - } - - public static function addGeneratorDataProvider(): Generator - { - yield 'not added' => [false, self::once(), false]; - yield 'already added' => [true, self::never(), true]; - } - - /** - * @test - * @throws ReflectionException - * @covers \D3\PdfDocuments\Application\Model\Registries\registryAbstract::addItem - * @dataProvider addItemDataProvider - */ - public function testAddItem(string $fqcn, bool $expectException): void - { - $sut = $this->getMockBuilder(registryOrderoverview::class) + $settingService = $this->getMockBuilder(ModuleSettingService::class) + ->onlyMethods(['getBoolean']) ->disableOriginalConstructor() - ->onlyMethods(['addGenerator']) ->getMock(); + $settingService->method('getBoolean')->willReturnMap([ + ['d3PdfDocumentsDocInvoice', Constants::OXID_MODULE_ID, $inv], + ['d3PdfDocumentsDocDeliveryNote', Constants::OXID_MODULE_ID, $deln], + ['d3PdfDocumentsDocInvoiceNoLogo', Constants::OXID_MODULE_ID, $invNL], + ['d3PdfDocumentsDocDeliveryNoteNoLogo', Constants::OXID_MODULE_ID, $delnNL] + ]); - $this->assertArrayNotHasKey($fqcn, $this->getValue($sut, 'registry')); + try { + $this->addServiceMocks([ModuleSettingServiceInterface::class => $settingService]); - if ($expectException) { - $this->expectException(Exception::class); - } + $sut = $this->getMockBuilder(registryOrderoverview::class) + ->disableOriginalConstructor() + ->onlyMethods(['addGenerator']) + ->getMock(); + $sut->expects($this->exactly($expectedCount))->method('addGenerator'); - $this->callMethod( - $sut, - 'addItem', - [oxNew($fqcn)] - ); - - if ($expectException) { - $this->assertArrayNotHasKey($fqcn, $this->getValue($sut, 'registry')); - } else { - $this->assertArrayHasKey($fqcn, $this->getValue($sut, 'registry')); + $this->callMethod( + $sut, + '__construct' + ); + } finally { + ContainerFactory::resetContainer(); } } - public static function addItemDataProvider(): Generator + public static function constructDataProvider(): Generator { - dumpvar(__LINE__); - yield 'right interface passed' => [invoicePdf::class, false]; - dumpvar(__LINE__); - yield 'wrong interface' => [nonOrderDocument::class, true]; - dumpvar(__LINE__); + yield 'nothing' => [false, false, false, false, 0]; + yield 'invoice only' => [true, false, false, false, 1]; + yield 'invoice + delnote' => [true, true, false, false, 2]; + yield 'invoice + NoLogo + delnote' => [true, true, true, false, 3]; + yield 'invoice + NoLogo + delnote + NoLogo' => [true, true, true, true, 4]; + yield 'invNoLogo + delnote + NoLogo' => [false, true, true, true, 3]; + } + + /** + * @test + * @throws ReflectionException + * @throws wrongPdfGeneratorInterface + * @covers \D3\PdfDocuments\Application\Model\Registries\registryOrderoverview::getGenerator + */ + public function testGetGenerator(): void + { + $sut = oxNew(registryOrderoverview::class); + $sut->addGenerator(orderDocument::class); + + $this->assertInstanceOf( + orderDocument::class, + $this->callMethod( + $sut, + 'getGenerator', + [orderDocument::class] + ) + ); } } \ No newline at end of file diff --git a/Tests/Unit/Application/Model/Registries/registryOrdermanagerActionsTest.php b/Tests/Unit/Application/Model/Registries/registryOrdermanagerActionsTest.php new file mode 100644 index 0000000..3ffd050 --- /dev/null +++ b/Tests/Unit/Application/Model/Registries/registryOrdermanagerActionsTest.php @@ -0,0 +1,95 @@ + + * @link https://www.oxidmodule.com + */ + +namespace D3\PdfDocuments\Tests\Unit\Application\Model\Registries; + +use D3\PdfDocuments\Application\Model\Constants; +use D3\PdfDocuments\Application\Model\Exceptions\wrongPdfGeneratorInterface; +use D3\PdfDocuments\Application\Model\Registries\registryOrdermanagerActions; +use D3\PdfDocuments\Tests\Unit\Helpers\orderDocument; +use Generator; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingService; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; +use ReflectionException; + +class registryOrdermanagerActionsTest extends registryAbstract +{ + /** + * @test + * @throws ReflectionException + * @covers \D3\PdfDocuments\Application\Model\Registries\registryOrdermanagerActions::__construct + * @dataProvider constructDataProvider + */ + public function testConstructor(bool $inv, bool $deln, bool $invNL, bool $delnNL, int $expectedCount): void + { + $settingService = $this->getMockBuilder(ModuleSettingService::class) + ->onlyMethods(['getBoolean']) + ->disableOriginalConstructor() + ->getMock(); + $settingService->method('getBoolean')->willReturnMap([ + ['d3PdfDocumentsDocInvoice', Constants::OXID_MODULE_ID, $inv], + ['d3PdfDocumentsDocDeliveryNote', Constants::OXID_MODULE_ID, $deln], + ['d3PdfDocumentsDocInvoiceNoLogo', Constants::OXID_MODULE_ID, $invNL], + ['d3PdfDocumentsDocDeliveryNoteNoLogo', Constants::OXID_MODULE_ID, $delnNL] + ]); + + $this->addServiceMocks([ModuleSettingServiceInterface::class => $settingService]); + + try { + $sut = $this->getMockBuilder(registryOrdermanagerActions::class) + ->disableOriginalConstructor() + ->onlyMethods(['addGenerator']) + ->getMock(); + $sut->expects($this->exactly($expectedCount))->method('addGenerator'); + + $this->callMethod( + $sut, + '__construct' + ); + } finally { + ContainerFactory::resetContainer(); + } + } + + public static function constructDataProvider(): Generator + { + yield 'nothing' => [false, false, false, false, 0]; + yield 'invoice only' => [true, false, false, false, 1]; + yield 'invoice + delnote' => [true, true, false, false, 2]; + yield 'invoice + NoLogo + delnote' => [true, true, true, false, 3]; + yield 'invoice + NoLogo + delnote + NoLogo' => [true, true, true, true, 4]; + yield 'invNoLogo + delnote + NoLogo' => [false, true, true, true, 3]; + } + + /** + * @test + * @throws ReflectionException + * @throws wrongPdfGeneratorInterface + * @covers \D3\PdfDocuments\Application\Model\Registries\registryOrdermanagerActions::getGenerator + */ + public function testGetGenerator(): void + { + $sut = oxNew(registryOrdermanagerActions::class); + $sut->addGenerator(orderDocument::class); + + $this->assertInstanceOf( + orderDocument::class, + $this->callMethod( + $sut, + 'getGenerator', + [orderDocument::class] + ) + ); + } +} \ No newline at end of file diff --git a/Tests/Unit/Helpers/nonOrderDocument.php b/Tests/Unit/Helpers/nonOrderDocument.php index 618d867..75befb3 100644 --- a/Tests/Unit/Helpers/nonOrderDocument.php +++ b/Tests/Unit/Helpers/nonOrderDocument.php @@ -58,4 +58,9 @@ class nonOrderDocument implements pdfdocumentsGenericInterface { return 'filenameExtension'; } + + public function setDevelopmentMode( bool $devMode ) + { + return false; + } } \ No newline at end of file diff --git a/Tests/Unit/Helpers/orderDocument.php b/Tests/Unit/Helpers/orderDocument.php new file mode 100644 index 0000000..4c10b6d --- /dev/null +++ b/Tests/Unit/Helpers/orderDocument.php @@ -0,0 +1,81 @@ +