3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Test \Unit \Model \Product \Image ;
8
9
11
12
use Magento \Framework \App \Area ;
12
13
use Magento \Framework \App \Config \ScopeConfigInterface ;
13
14
use Magento \Framework \Config \View ;
15
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
16
use Magento \Framework \View \ConfigInterface ;
15
17
use Magento \Store \Model \ScopeInterface ;
18
+ use PHPUnit \Framework \TestCase ;
16
19
17
- class ParamsBuilderTest extends \PHPUnit \Framework \TestCase
20
+ /**
21
+ * Test product image params builder
22
+ */
23
+ class ParamsBuilderTest extends TestCase
18
24
{
19
25
/**
20
26
* @var ScopeConfigInterface
@@ -30,10 +36,17 @@ class ParamsBuilderTest extends \PHPUnit\Framework\TestCase
30
36
* @var ParamsBuilder
31
37
*/
32
38
private $ model ;
39
+ /**
40
+ * @var array
41
+ */
42
+ private $ scopeConfigData = [];
33
43
44
+ /**
45
+ * @inheritDoc
46
+ */
34
47
protected function setUp ()
35
48
{
36
- $ objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
49
+ $ objectManager = new ObjectManager ($ this );
37
50
$ this ->scopeConfig = $ this ->createMock (ScopeConfigInterface::class);
38
51
$ this ->viewConfig = $ this ->createMock (ConfigInterface::class);
39
52
$ this ->model = $ objectManager ->getObject (
@@ -43,28 +56,37 @@ protected function setUp()
43
56
'viewConfig ' => $ this ->viewConfig ,
44
57
]
45
58
);
59
+ $ this ->scopeConfigData = [];
60
+ $ this ->scopeConfig ->method ('getValue ' )
61
+ ->willReturnCallback (
62
+ function ($ path , $ scopeType , $ scopeCode ) {
63
+ return $ this ->scopeConfigData [$ path ][$ scopeType ][$ scopeCode ] ?? null ;
64
+ }
65
+ );
46
66
}
47
67
48
68
/**
49
- * Test watermark location.
69
+ * Test build() with different parameters and config values
70
+ *
71
+ * @param int $scopeId
72
+ * @param array $config
73
+ * @param array $imageArguments
74
+ * @param array $expected
75
+ * @dataProvider buildDataProvider
50
76
*/
51
- public function testWatermarkLocation ( )
77
+ public function testBuild ( int $ scopeId , array $ config , array $ imageArguments , array $ expected )
52
78
{
53
- $ imageArguments = [
54
- 'type ' => 'type ' ,
55
- 'height ' => 'image_height ' ,
56
- 'width ' => 'image_width ' ,
57
- 'angle ' => 'angle ' ,
58
- 'background ' => [1 , 2 , 3 ]
79
+ $ this ->scopeConfigData [Image::XML_PATH_JPEG_QUALITY ][ScopeConfigInterface::SCOPE_TYPE_DEFAULT ][null ] = 80 ;
80
+ foreach ($ config as $ path => $ value ) {
81
+ $ this ->scopeConfigData [$ path ][ScopeInterface::SCOPE_STORE ][$ scopeId ] = $ value ;
82
+ }
83
+ $ imageArguments += [
84
+ 'type ' => 'image ' ,
85
+ 'height ' => '600 ' ,
86
+ 'width ' => '400 ' ,
87
+ 'angle ' => '45 ' ,
88
+ 'background ' => [110 , 64 , 224 ]
59
89
];
60
- $ scopeId = 1 ;
61
- $ quality = 100 ;
62
- $ file = 'file ' ;
63
- $ width = 'width ' ;
64
- $ height = 'height ' ;
65
- $ size = "{$ width }x {$ height }" ;
66
- $ opacity = 'opacity ' ;
67
- $ position = 'position ' ;
68
90
69
91
$ viewMock = $ this ->createMock (View::class);
70
92
$ viewMock ->expects ($ this ->once ())
@@ -77,51 +99,16 @@ public function testWatermarkLocation()
77
99
->with (['area ' => Area::AREA_FRONTEND ])
78
100
->willReturn ($ viewMock );
79
101
80
- $ this ->scopeConfig ->expects ($ this ->exactly (5 ))->method ('getValue ' )->withConsecutive (
81
- [
82
- Image::XML_PATH_JPEG_QUALITY
83
- ],
84
- [
85
- "design/watermark/ {$ imageArguments ['type ' ]}_image " ,
86
- ScopeInterface::SCOPE_STORE ,
87
- $ scopeId ,
88
- ],
89
- [
90
- "design/watermark/ {$ imageArguments ['type ' ]}_size " ,
91
- ScopeInterface::SCOPE_STORE ],
92
- [
93
- "design/watermark/ {$ imageArguments ['type ' ]}_imageOpacity " ,
94
- ScopeInterface::SCOPE_STORE ,
95
- $ scopeId
96
- ],
97
- [
98
- "design/watermark/ {$ imageArguments ['type ' ]}_position " ,
99
- ScopeInterface::SCOPE_STORE ,
100
- $ scopeId
101
- ]
102
- )->willReturnOnConsecutiveCalls (
103
- $ quality ,
104
- $ file ,
105
- $ size ,
106
- $ opacity ,
107
- $ position
108
- );
109
-
110
102
$ actual = $ this ->model ->build ($ imageArguments , $ scopeId );
111
- $ expected = [
103
+ $ expected + = [
112
104
'image_type ' => $ imageArguments ['type ' ],
113
105
'background ' => $ imageArguments ['background ' ],
114
106
'angle ' => $ imageArguments ['angle ' ],
115
- 'quality ' => $ quality ,
107
+ 'quality ' => 80 ,
116
108
'keep_aspect_ratio ' => true ,
117
109
'keep_frame ' => true ,
118
110
'keep_transparency ' => true ,
119
111
'constrain_only ' => true ,
120
- 'watermark_file ' => $ file ,
121
- 'watermark_image_opacity ' => $ opacity ,
122
- 'watermark_position ' => $ position ,
123
- 'watermark_width ' => $ width ,
124
- 'watermark_height ' => $ height ,
125
112
'image_height ' => $ imageArguments ['height ' ],
126
113
'image_width ' => $ imageArguments ['width ' ],
127
114
];
@@ -131,4 +118,50 @@ public function testWatermarkLocation()
131
118
$ actual
132
119
);
133
120
}
121
+
122
+ /**
123
+ * Provides test scenarios for
124
+ *
125
+ * @return array
126
+ */
127
+ public function buildDataProvider ()
128
+ {
129
+ return [
130
+ 'watermark config ' => [
131
+ 1 ,
132
+ [
133
+ 'design/watermark/small_image_image ' => 'stores/1/magento-logo.png ' ,
134
+ 'design/watermark/small_image_size ' => '60x40 ' ,
135
+ 'design/watermark/small_image_imageOpacity ' => '50 ' ,
136
+ 'design/watermark/small_image_position ' => 'bottom-right ' ,
137
+ ],
138
+ [
139
+ 'type ' => 'small_image '
140
+ ],
141
+ [
142
+ 'watermark_file ' => 'stores/1/magento-logo.png ' ,
143
+ 'watermark_image_opacity ' => '50 ' ,
144
+ 'watermark_position ' => 'bottom-right ' ,
145
+ 'watermark_width ' => '60 ' ,
146
+ 'watermark_height ' => '40 ' ,
147
+ ]
148
+ ],
149
+ 'watermark config empty ' => [
150
+ 1 ,
151
+ [
152
+ 'design/watermark/small_image_image ' => 'stores/1/magento-logo.png ' ,
153
+ ],
154
+ [
155
+ 'type ' => 'small_image '
156
+ ],
157
+ [
158
+ 'watermark_file ' => 'stores/1/magento-logo.png ' ,
159
+ 'watermark_image_opacity ' => null ,
160
+ 'watermark_position ' => null ,
161
+ 'watermark_width ' => null ,
162
+ 'watermark_height ' => null ,
163
+ ]
164
+ ]
165
+ ];
166
+ }
134
167
}
0 commit comments