123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\ConfigurableProduct\Test\Unit\Model;
- use Magento\ConfigurableProduct\Model\AttributeOptionProvider;
- use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
- use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
- use Magento\Framework\App\ScopeInterface;
- use Magento\Framework\App\ScopeResolverInterface;
- use Magento\Framework\DB\Select;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- use Magento\Framework\DB\Adapter\AdapterInterface;
- use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
- use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AttributeOptionProviderTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var AttributeOptionProvider
- */
- private $model;
- /**
- * @var ObjectManagerHelper
- */
- private $objectManagerHelper;
- /**
- * @var ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $scopeResolver;
- /**
- * @var Select|\PHPUnit_Framework_MockObject_MockObject
- */
- private $select;
- /**
- * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $connectionMock;
- /**
- * @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject
- */
- private $abstractAttribute;
- /**
- * @var ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $scope;
- /**
- * @var Attribute|\PHPUnit_Framework_MockObject_MockObject
- */
- private $attributeResource;
- /**
- * @var OptionSelectBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $optionSelectBuilder;
- protected function setUp()
- {
- $this->select = $this->getMockBuilder(Select::class)
- ->setMethods([])
- ->disableOriginalConstructor()
- ->getMock();
- $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->scope = $this->getMockBuilder(ScopeInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->attributeResource = $this->getMockBuilder(Attribute::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->optionSelectBuilder = $this->getMockBuilder(OptionSelectBuilderInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->abstractAttribute = $this->getMockBuilder(AbstractAttribute::class)
- ->setMethods(['getSourceModel', 'getSource'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->objectManagerHelper = new ObjectManagerHelper($this);
- $this->model = $this->objectManagerHelper->getObject(
- AttributeOptionProvider::class,
- [
- 'attributeResource' => $this->attributeResource,
- 'scopeResolver' => $this->scopeResolver,
- 'optionSelectBuilder' => $this->optionSelectBuilder,
- ]
- );
- }
- /**
- * @param array $options
- * @dataProvider getAttributeOptionsDataProvider
- */
- public function testGetAttributeOptions(array $options)
- {
- $this->scopeResolver->expects($this->any())
- ->method('getScope')
- ->willReturn($this->scope);
-
- $this->optionSelectBuilder->expects($this->any())
- ->method('getSelect')
- ->with($this->abstractAttribute, 4, $this->scope)
- ->willReturn($this->select);
-
- $this->attributeResource->expects($this->once())
- ->method('getConnection')
- ->willReturn($this->connectionMock);
- $this->connectionMock->expects($this->once())
- ->method('fetchAll')
- ->with($this->select)
- ->willReturn($options);
- $this->assertEquals(
- $options,
- $this->model->getAttributeOptions($this->abstractAttribute, 4)
- );
- }
- /**
- * @param array $options
- * @dataProvider optionsWithBackendModelDataProvider
- */
- public function testGetAttributeOptionsWithBackendModel(array $options)
- {
- $this->scopeResolver->expects($this->any())
- ->method('getScope')
- ->willReturn($this->scope);
- $source = $this->getMockBuilder(AbstractSource::class)
- ->disableOriginalConstructor()
- ->setMethods(['getAllOptions'])
- ->getMockForAbstractClass();
- $source->expects($this->once())
- ->method('getAllOptions')
- ->willReturn([
- ['value' => 13, 'label' => 'Option Value for index 13'],
- ['value' => 14, 'label' => 'Option Value for index 14'],
- ['value' => 15, 'label' => 'Option Value for index 15']
- ]);
-
- $this->abstractAttribute->expects($this->any())
- ->method('getSource')
- ->willReturn($source);
- $this->abstractAttribute->expects($this->atLeastOnce())
- ->method('getSourceModel')
- ->willReturn('getSourceModel value');
- $this->optionSelectBuilder->expects($this->any())
- ->method('getSelect')
- ->with($this->abstractAttribute, 1, $this->scope)
- ->willReturn($this->select);
- $this->attributeResource->expects($this->once())
- ->method('getConnection')
- ->willReturn($this->connectionMock);
- $this->connectionMock->expects($this->once())
- ->method('fetchAll')
- ->with($this->select)
- ->willReturn($options);
- $this->assertEquals(
- $options,
- $this->model->getAttributeOptions($this->abstractAttribute, 1)
- );
- }
- /**
- * @return array
- */
- public function getAttributeOptionsDataProvider()
- {
- return [
- [
- [
- [
- 'sku' => 'Configurable1-Black',
- 'product_id' => 4,
- 'attribute_code' => 'color',
- 'value_index' => '13',
- 'option_title' => 'Black',
- ],
- [
- 'sku' => 'Configurable1-White',
- 'product_id' => 4,
- 'attribute_code' => 'color',
- 'value_index' => '14',
- 'option_title' => 'White',
- ],
- [
- 'sku' => 'Configurable1-Red',
- 'product_id' => 4,
- 'attribute_code' => 'color',
- 'value_index' => '15',
- 'option_title' => 'Red',
- ],
- ],
- ],
- ];
- }
- /**
- * @return array
- */
- public function optionsWithBackendModelDataProvider()
- {
- return [
- [
- [
- [
- 'sku' => 'Configurable1-Black',
- 'product_id' => 4,
- 'attribute_code' => 'color',
- 'value_index' => '13',
- 'option_title' => 'Option Value for index 13',
- 'default_title' => 'Option Value for index 13',
- ],
- [
- 'sku' => 'Configurable1-White',
- 'product_id' => 4,
- 'attribute_code' => 'color',
- 'value_index' => '14',
- 'option_title' => 'Option Value for index 14',
- 'default_title' => 'Option Value for index 14',
- ],
- [
- 'sku' => 'Configurable1-Red',
- 'product_id' => 4,
- 'attribute_code' => 'color',
- 'value_index' => '15',
- 'option_title' => 'Option Value for index 15',
- 'default_title' => 'Option Value for index 15',
- ],
- ],
- ],
- ];
- }
- }
|