Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SwatchAttributesProviderTest.php
Go to the documentation of this file.
1 <?php
8 
14 
15 class SwatchAttributesProviderTest extends \PHPUnit\Framework\TestCase
16 {
20  private $swatchAttributeProvider;
21 
25  private $typeConfigurable;
26 
30  private $swatchAttributeCodes;
31 
35  private $productMock;
36 
40  private $swatchTypeChecker;
41 
42  protected function setUp()
43  {
44  $this->typeConfigurable = $this->createPartialMock(
45  Configurable::class,
46  ['getConfigurableAttributes', 'getCodes', 'getProductAttribute']
47  );
48 
49  $this->swatchAttributeCodes = $this->createMock(SwatchAttributeCodes::class);
50 
51  $this->productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId', 'getTypeId']);
52  $this->swatchTypeChecker = $this->createMock(SwatchAttributeType::class);
53 
54  $this->swatchAttributeProvider = (new ObjectManager($this))->getObject(SwatchAttributesProvider::class, [
55  'typeConfigurable' => $this->typeConfigurable,
56  'swatchAttributeCodes' => $this->swatchAttributeCodes,
57  'swatchTypeChecker' => $this->swatchTypeChecker,
58  ]);
59  }
60 
61  public function testProvide()
62  {
63  $this->productMock->method('getId')->willReturn(1);
64  $this->productMock->method('getTypeId')
65  ->willReturn(Configurable::TYPE_CODE);
66 
67  $attributeMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
68  ->disableOriginalConstructor()
69  ->setMethods(['setStoreId', 'getData', 'setData', 'getSource', 'hasData'])
70  ->getMock();
71 
72  $configAttributeMock = $this->createPartialMock(
73  Configurable\Attribute::class,
74  ['getAttributeId', 'getProductAttribute']
75  );
76  $configAttributeMock
77  ->method('getAttributeId')
78  ->willReturn(1);
79 
80  $configAttributeMock
81  ->method('getProductAttribute')
82  ->willReturn($attributeMock);
83 
84  $this->typeConfigurable
85  ->method('getConfigurableAttributes')
86  ->with($this->productMock)
87  ->willReturn([$configAttributeMock]);
88 
89  $swatchAttributes = [1 => 'text_swatch'];
90  $this->swatchAttributeCodes
91  ->method('getCodes')
92  ->willReturn($swatchAttributes);
93 
94  $this->swatchTypeChecker->expects($this->once())->method('isSwatchAttribute')->willReturn(true);
95 
96  $result = $this->swatchAttributeProvider->provide($this->productMock);
97 
98  $this->assertEquals([1 => $attributeMock], $result);
99  }
100 }