Skip to content

Commit

Permalink
added tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrykazak committed Feb 2, 2020
1 parent de58008 commit d66558f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
51 changes: 51 additions & 0 deletions Test/Unit/Model/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace DK\CustomOptionDefaultValue\Test\Unit\Model;

use DK\CustomOptionDefaultValue\Model\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
final class ConfigTest extends TestCase
{
/**
* @var MockObject|ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var Config
*/
private $config;

protected function setUp(): void
{
parent::setUp();

$this->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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace DK\CustomOptionDefaultValue\Test\Unit\Plugin\Catalog\Block\Product\View\Options\Type;

use DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select\CheckableFactory;
use DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select\Multiple;
use DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select\MultipleFactory;
use DK\CustomOptionDefaultValue\Model\Config;
use DK\CustomOptionDefaultValue\Plugin\Catalog\Block\Product\View\Options\Type\Select;
use Magento\Catalog\Block\Product\View\Options\Type\Select as TypeSelect;
use Magento\Catalog\Model\Product;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class SelectTest extends TestCase
{
/**
* @var MockObject|MultipleFactory
*/
private $multipleFactory;

/**
* @var CheckableFactory|MockObject
*/
private $checkableFactory;

/**
* @var Config|MockObject
*/
private $config;

/**
* @var Select
*/
private $plugin;

/**
* @var MockObject|TypeSelect
*/
private $typeSelectMock;

protected function setUp(): void
{
parent::setUp();

$this->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);
}
}

0 comments on commit d66558f

Please sign in to comment.