From d66558f28c96a13595e5edf8aab2239d348dced7 Mon Sep 17 00:00:00 2001 From: dmitrykazak Date: Sun, 2 Feb 2020 14:28:24 +0300 Subject: [PATCH] added tests and readme --- README.md | 6 ++ Test/Unit/Model/ConfigTest.php | 51 +++++++++ .../Product/View/Options/Type/SelectTest.php | 102 ++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 Test/Unit/Model/ConfigTest.php create mode 100644 Test/Unit/Plugin/Catalog/Block/Product/View/Options/Type/SelectTest.php diff --git a/README.md b/README.md index 5eb334e..1a96224 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,12 @@ Magento 2 Custom Option Default Value extension use to set custom option default * Radio Box * Checkbox +![Configuration](https://user-images.githubusercontent.com/5670207/73607519-005b9880-45c8-11ea-8b6c-eb7251a8d985.png) + +![Setting Custom Option of Product](https://user-images.githubusercontent.com/5670207/73607472-5976fc80-45c7-11ea-8398-b75a3fb593f8.png) + +![Product Page](https://user-images.githubusercontent.com/5670207/73607439-1157da00-45c7-11ea-9f4e-4e00763d3e6e.png) + #### Support If you encounter any problems or bugs, please open an [issue](https://github.com/dmitrykazak/magento2-custom-option-default-value/issues) on GitHub. diff --git a/Test/Unit/Model/ConfigTest.php b/Test/Unit/Model/ConfigTest.php new file mode 100644 index 0000000..bf2a910 --- /dev/null +++ b/Test/Unit/Model/ConfigTest.php @@ -0,0 +1,51 @@ +scopeConfig = $this->createMock(ScopeConfigInterface::class); + $this->config = new Config($this->scopeConfig); + } + + public function testIsActiveModule(): void + { + $this->scopeConfig + ->expects($this->once()) + ->method('isSetFlag') + ->with($this->getConstValue('XML_PATH_GENERAL_ACTIVE')->getValue()) + ->willReturn(true); + + $this->assertTrue($this->config->isActiveModule()); + } + + private function getConstValue(string $const): \ReflectionClassConstant + { + return new \ReflectionClassConstant(Config::class, $const); + } +} diff --git a/Test/Unit/Plugin/Catalog/Block/Product/View/Options/Type/SelectTest.php b/Test/Unit/Plugin/Catalog/Block/Product/View/Options/Type/SelectTest.php new file mode 100644 index 0000000..5a18e2d --- /dev/null +++ b/Test/Unit/Plugin/Catalog/Block/Product/View/Options/Type/SelectTest.php @@ -0,0 +1,102 @@ +multipleFactory = $this->createPartialMock(MultipleFactory::class, ['create']); + $this->checkableFactory = $this->createPartialMock(CheckableFactory::class, ['create']); + $this->config = $this->createMock(Config::class); + + $this->typeSelectMock = $this->createMock(TypeSelect::class); + + $this->plugin = new Select($this->multipleFactory, $this->checkableFactory, $this->config); + } + + public function testAroundGetValuesHtml(): void + { + $proceed = function () { + return null; + }; + + $this->config->expects(self::once())->method('isActiveModule')->willReturn(true); + + /** @var \Magento\Catalog\Model\Product\Option|MockObject $option */ + $option = $this->createMock(\Magento\Catalog\Model\Product\Option::class); + $option->expects(self::once())->method('getType')->willReturn(\Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN); + + /** @var MockObject|Product $product */ + $product = $this->createMock(Product::class); + + $this->typeSelectMock->expects(self::once())->method('getOption')->willReturn($option); + $this->typeSelectMock->expects(self::once())->method('getProduct')->willReturn($product); + + $multipleMock = $this->createPartialMock(Multiple::class, [ + 'setOption', 'setProduct', 'setSkipJsReloadPrice', 'toHtml', + ]); + $multipleMock->method('setOption')->willReturn($multipleMock); + $multipleMock->method('setProduct')->willReturn($multipleMock); + $multipleMock->method('setSkipJsReloadPrice')->willReturn($multipleMock); + + $this->multipleFactory->expects(self::once())->method('create')->willReturn($multipleMock); + + $this->plugin->aroundGetValuesHtml($this->typeSelectMock, $proceed); + } + + public function testAroundGetValuesHtmlModuleDisabled(): void + { + $proceed = function () { + return null; + }; + + $this->config->expects(self::once())->method('isActiveModule')->willReturn(false); + $this->typeSelectMock->expects(self::never())->method('getOption'); + + $this->plugin->aroundGetValuesHtml($this->typeSelectMock, $proceed); + } +}