Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SwatchesGeneratorTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Setup\Fixtures\ImagesGenerator\ImagesGeneratorFactory;
14 
15 class SwatchesGeneratorTest extends \PHPUnit\Framework\TestCase
16 {
20  private $swatchesGeneratorMock;
21 
25  private $imagePathFixture = [
26  'option_1' => '/<-o->',
27  'option_2' => '/>o<',
28  'option_3' => '/|o|'
29  ];
30 
31  public function setUp()
32  {
33  // Mock Swatch Media Helper
34  $swatchHelperMock = $this->getMockBuilder(Media::class)
35  ->disableOriginalConstructor()
36  ->getMock();
37 
38  $swatchHelperMock
39  ->expects($this->any())
40  ->method('moveImageFromTmp')
41  ->willReturnOnConsecutiveCalls(...array_values($this->imagePathFixture));
42 
43  // Mock image generator
44  $imageGeneratorMock = $this->getMockBuilder(ImagesGenerator::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $imageGeneratorMock
49  ->expects($this->any())
50  ->method('generate');
51 
52  // Mock image generator factory
53  $imageGeneratorFactoryMock = $this->getMockBuilder(ImagesGeneratorFactory::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['create'])
56  ->getMock();
57 
58  $imageGeneratorFactoryMock
59  ->expects($this->once())
60  ->method('create')
61  ->willReturn($imageGeneratorMock);
62 
63  $this->swatchesGeneratorMock = new SwatchesGenerator(
64  $swatchHelperMock,
65  $imageGeneratorFactoryMock
66  );
67  }
68 
69  public function testGenerateSwatchData()
70  {
71  $attributeColorType['swatch_input_type'] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
72  $attributeColorType['swatchvisual']['value'] = array_reduce(
73  range(1, 3),
74  function ($values, $index) {
75  $values['option_' . $index] = '#' . str_repeat(dechex(255 * $index / 3), 3);
76  return $values;
77  },
78  []
79  );
80 
81  $attributeColorType['optionvisual']['value'] = array_reduce(
82  range(1, 3),
83  function ($values, $index) {
84  $values['option_' . $index] = ['option ' . $index];
85  return $values;
86  },
87  []
88  );
89 
90  $attributeImageType = $attributeColorType;
91  $attributeImageType['swatchvisual']['value'] = array_map(
92  function ($item) {
93  return ltrim($item, '/');
94  },
95  $this->imagePathFixture
96  );
97 
98  $this->assertEquals(
99  $attributeColorType,
100  $this->swatchesGeneratorMock->generateSwatchData(3, 'test', 'color')
101  );
102 
103  $this->assertEquals(
104  $attributeImageType,
105  $this->swatchesGeneratorMock->generateSwatchData(3, 'test', 'image')
106  );
107  }
108 }
$values
Definition: options.phtml:88
$index
Definition: list.phtml:44