1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Test\Unit\Fixtures\AttributeSet;
use Magento\Setup\Fixtures\AttributeSet\SwatchesGenerator;
use Magento\Setup\Fixtures\ImagesGenerator\ImagesGenerator;
use Magento\Setup\Fixtures\ImagesGenerator\ImagesGeneratorFactory;
use Magento\Swatches\Helper\Media;
use Magento\Swatches\Model\Swatch;
class SwatchesGeneratorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var SwatchesGenerator
*/
private $swatchesGeneratorMock;
/**
* @var array
*/
private $imagePathFixture = [
'option_1' => '/<-o->',
'option_2' => '/>o<',
'option_3' => '/|o|'
];
public function setUp()
{
// Mock Swatch Media Helper
$swatchHelperMock = $this->getMockBuilder(Media::class)
->disableOriginalConstructor()
->getMock();
$swatchHelperMock
->expects($this->any())
->method('moveImageFromTmp')
->willReturnOnConsecutiveCalls(...array_values($this->imagePathFixture));
// Mock image generator
$imageGeneratorMock = $this->getMockBuilder(ImagesGenerator::class)
->disableOriginalConstructor()
->getMock();
$imageGeneratorMock
->expects($this->any())
->method('generate');
// Mock image generator factory
$imageGeneratorFactoryMock = $this->getMockBuilder(ImagesGeneratorFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$imageGeneratorFactoryMock
->expects($this->once())
->method('create')
->willReturn($imageGeneratorMock);
$this->swatchesGeneratorMock = new SwatchesGenerator(
$swatchHelperMock,
$imageGeneratorFactoryMock
);
}
public function testGenerateSwatchData()
{
$attributeColorType['swatch_input_type'] = Swatch::SWATCH_INPUT_TYPE_VISUAL;
$attributeColorType['swatchvisual']['value'] = array_reduce(
range(1, 3),
function ($values, $index) {
$values['option_' . $index] = '#' . str_repeat(dechex(255 * $index / 3), 3);
return $values;
},
[]
);
$attributeColorType['optionvisual']['value'] = array_reduce(
range(1, 3),
function ($values, $index) {
$values['option_' . $index] = ['option ' . $index];
return $values;
},
[]
);
$attributeImageType = $attributeColorType;
$attributeImageType['swatchvisual']['value'] = array_map(
function ($item) {
return ltrim($item, '/');
},
$this->imagePathFixture
);
$this->assertEquals(
$attributeColorType,
$this->swatchesGeneratorMock->generateSwatchData(3, 'test', 'color')
);
$this->assertEquals(
$attributeImageType,
$this->swatchesGeneratorMock->generateSwatchData(3, 'test', 'image')
);
}
}