Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SwatchAttributeTypeTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
18 class SwatchAttributeTypeTest extends \PHPUnit\Framework\TestCase
19 {
23  private $swatchType;
24 
28  protected function setUp()
29  {
30  parent::setUp();
31  $this->swatchType = new SwatchAttributeType(new Json());
32  }
33 
40  public function testIsSwatchAttribute(string $dataValue, bool $expected) : void
41  {
42  $this->assertEquals(
43  $expected,
44  $this->swatchType->isSwatchAttribute(
45  $this->createAttributeMock($dataValue)
46  )
47  );
48  }
49 
55  public function provideIsSwatchAttributeTestData() : array
56  {
57  return [
60  ['fake', false],
61  ];
62  }
63 
70  public function testIsTextSwatch(string $dataValue, bool $expected) : void
71  {
72  $this->assertEquals(
73  $expected,
74  $this->swatchType->isTextSwatch(
75  $this->createAttributeMock($dataValue)
76  )
77  );
78  }
79 
85  public function provideIsTextSwatchAttributeTestData() : array
86  {
87  return [
90  ['fake', false],
91  ];
92  }
93 
100  public function testIsVisualSwatch(string $dataValue, bool $expected) : void
101  {
102  $this->assertEquals(
103  $expected,
104  $this->swatchType->isVisualSwatch(
105  $this->createAttributeMock($dataValue)
106  )
107  );
108  }
109 
115  public function provideIsVisualSwatchAttributeTestData() : array
116  {
117  return [
120  ['fake', false],
121  ];
122  }
123 
127  public function testIfAttributeHasNotAdditionData() : void
128  {
130  $json = new Json();
131  $encodedAdditionData = $json->serialize([Swatch::SWATCH_INPUT_TYPE_KEY => Swatch::SWATCH_INPUT_TYPE_TEXT]);
132 
134  $attributeMock = $this->getMockBuilder(AttributeInterface::class)
135  ->disableOriginalConstructor()
136  ->setMethods(['hasData', 'getData', 'setData'])
137  ->getMockForAbstractClass();
138 
139  $attributeMock->expects($this->any())->method('hasData')->willReturn(false);
140 
141  $attributeMock->expects($this->any())
142  ->method('getData')
143  ->willReturnMap(
144  [
145  ['additional_data', $encodedAdditionData],
147  ]
148  );
149 
150  $this->assertEquals(true, $this->swatchType->isTextSwatch($attributeMock));
151  $this->assertEquals(false, $this->swatchType->isVisualSwatch($attributeMock));
152  }
153 
159  protected function createAttributeMock($getDataReturns, bool $hasDataReturns = true)
160  {
161  $attributeMock = $this->getMockBuilder(AttributeInterface::class)
162  ->disableOriginalConstructor()
163  ->setMethods(['hasData', 'getData', 'setData'])
164  ->getMockForAbstractClass();
165 
166  $attributeMock->expects($this->any())->method('hasData')->willReturn($hasDataReturns);
167  $attributeMock->expects($this->any())->method('getData')->willReturn($getDataReturns);
168 
169  return $attributeMock;
170  }
171 }
createAttributeMock($getDataReturns, bool $hasDataReturns=true)