3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Catalog \Test \Unit \Model \Product ;
7
8
8
9
use Magento \Catalog \Api \Data \ProductExtension ;
9
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
+ use Magento \Catalog \Model \Attribute \ScopeOverriddenValue ;
10
12
use Magento \Catalog \Model \Product ;
13
+ use Magento \Catalog \Model \Product \Attribute \Source \Status ;
11
14
use Magento \Catalog \Model \Product \Copier ;
15
+ use Magento \Catalog \Model \Product \CopyConstructorInterface ;
16
+ use Magento \Catalog \Model \Product \Option \Repository ;
17
+ use Magento \Catalog \Model \ProductFactory ;
18
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResourceModel ;
12
19
use Magento \CatalogInventory \Api \Data \StockItemInterface ;
20
+ use Magento \Eav \Model \Entity \AbstractEntity ;
21
+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
22
+ use Magento \Framework \EntityManager \EntityMetadata ;
23
+ use Magento \Framework \EntityManager \MetadataPool ;
24
+ use PHPUnit \Framework \MockObject \MockObject ;
25
+ use PHPUnit \Framework \TestCase ;
13
26
14
27
/**
15
28
* Test for Magento\Catalog\Model\Product\Copier class.
16
29
*
17
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
31
*/
19
- class CopierTest extends \ PHPUnit \ Framework \ TestCase
32
+ class CopierTest extends TestCase
20
33
{
21
34
/**
22
- * @var \PHPUnit_Framework_MockObject_MockObject
35
+ * @var Copier
23
36
*/
24
- private $ optionRepositoryMock ;
37
+ private $ _model ;
25
38
26
39
/**
27
- * @var Copier
40
+ * @var Repository|MockObject
28
41
*/
29
- private $ _model ;
42
+ private $ optionRepositoryMock ;
30
43
31
44
/**
32
- * @var \PHPUnit_Framework_MockObject_MockObject
45
+ * @var CopyConstructorInterface|MockObject
33
46
*/
34
47
private $ copyConstructorMock ;
35
48
36
49
/**
37
- * @var \PHPUnit_Framework_MockObject_MockObject
50
+ * @var ProductFactory|MockObject
38
51
*/
39
52
private $ productFactoryMock ;
40
53
41
54
/**
42
- * @var \PHPUnit_Framework_MockObject_MockObject
55
+ * @var ScopeOverriddenValue|MockObject
43
56
*/
44
57
private $ scopeOverriddenValueMock ;
45
58
46
59
/**
47
- * @var \PHPUnit_Framework_MockObject_MockObject
60
+ * @var Product|MockObject
48
61
*/
49
62
private $ productMock ;
50
63
51
64
/**
52
- * @var \PHPUnit_Framework_MockObject_MockObject
65
+ * @var EntityMetadata|MockObject
53
66
*/
54
67
private $ metadata ;
55
68
@@ -58,27 +71,23 @@ class CopierTest extends \PHPUnit\Framework\TestCase
58
71
*/
59
72
protected function setUp ()
60
73
{
61
- $ this ->copyConstructorMock = $ this ->createMock (\Magento \Catalog \Model \Product \CopyConstructorInterface::class);
62
- $ this ->productFactoryMock = $ this ->createPartialMock (
63
- \Magento \Catalog \Model \ProductFactory::class,
64
- ['create ' ]
65
- );
66
- $ this ->scopeOverriddenValueMock = $ this ->createMock (
67
- \Magento \Catalog \Model \Attribute \ScopeOverriddenValue::class
68
- );
69
- $ this ->optionRepositoryMock = $ this ->createMock (
70
- \Magento \Catalog \Model \Product \Option \Repository::class
71
- );
74
+ $ this ->copyConstructorMock = $ this ->createMock (CopyConstructorInterface::class);
75
+ $ this ->productFactoryMock = $ this ->createPartialMock (ProductFactory::class, ['create ' ]);
76
+ $ this ->scopeOverriddenValueMock = $ this ->createMock (ScopeOverriddenValue::class);
77
+ $ this ->optionRepositoryMock = $ this ->createMock (Repository::class);
72
78
$ this ->productMock = $ this ->createMock (Product::class);
73
- $ this ->productMock ->expects ($ this ->any ())->method ('getEntityId ' )->willReturn (1 );
74
79
75
- $ this ->metadata = $ this ->getMockBuilder (\ Magento \ Framework \ EntityManager \ EntityMetadata::class)
80
+ $ this ->metadata = $ this ->getMockBuilder (EntityMetadata::class)
76
81
->disableOriginalConstructor ()
77
82
->getMock ();
78
- $ metadataPool = $ this ->getMockBuilder (\Magento \Framework \EntityManager \MetadataPool::class)
83
+
84
+ /** @var MetadataPool|MockObject $metadataPool */
85
+ $ metadataPool = $ this ->getMockBuilder (MetadataPool::class)
79
86
->disableOriginalConstructor ()
80
87
->getMock ();
81
- $ metadataPool ->expects ($ this ->any ())->method ('getMetadata ' )->willReturn ($ this ->metadata );
88
+ $ metadataPool ->expects ($ this ->once ())
89
+ ->method ('getMetadata ' )
90
+ ->willReturn ($ this ->metadata );
82
91
$ this ->_model = new Copier (
83
92
$ this ->copyConstructorMock ,
84
93
$ this ->productFactoryMock ,
@@ -89,9 +98,12 @@ protected function setUp()
89
98
}
90
99
91
100
/**
101
+ * Test duplicate product
102
+ *
103
+ * @return void
92
104
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
93
105
*/
94
- public function testCopy ()
106
+ public function testCopy (): void
95
107
{
96
108
$ stockItem = $ this ->createMock (StockItemInterface::class);
97
109
$ extensionAttributes = $ this ->getMockBuilder (ProductExtension::class)
@@ -110,51 +122,57 @@ public function testCopy()
110
122
'product data ' => ['product data ' ],
111
123
ProductInterface::EXTENSION_ATTRIBUTES_KEY => $ extensionAttributes ,
112
124
];
113
- $ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getWebsiteIds ' );
114
- $ this ->productMock ->expects ($ this ->atLeastOnce ())->method ('getCategoryIds ' );
115
- $ this ->productMock ->expects ($ this ->any ())->method ('getData ' )->willReturnMap ([
116
- ['' , null , $ productData ],
117
- ['linkField ' , null , '1 ' ],
118
- ]);
125
+ $ this ->productMock ->expects ($ this ->atLeastOnce ())
126
+ ->method ('getWebsiteIds ' );
127
+ $ this ->productMock ->expects ($ this ->atLeastOnce ())
128
+ ->method ('getCategoryIds ' );
129
+ $ this ->productMock ->expects ($ this ->exactly (2 ))
130
+ ->method ('getData ' )
131
+ ->willReturnMap ([
132
+ ['' , null , $ productData ],
133
+ ['linkField ' , null , '1 ' ],
134
+ ]);
119
135
120
136
$ entityMock = $ this ->getMockForAbstractClass (
121
- \ Magento \ Eav \ Model \ Entity \ AbstractEntity::class,
137
+ AbstractEntity::class,
122
138
[],
123
139
'' ,
124
140
false ,
125
141
true ,
126
142
true ,
127
143
['checkAttributeUniqueValue ' ]
128
144
);
129
- $ entityMock ->expects ($ this ->any ())
145
+ $ entityMock ->expects ($ this ->once ())
130
146
->method ('checkAttributeUniqueValue ' )
131
147
->willReturn (true );
132
148
133
149
$ attributeMock = $ this ->getMockForAbstractClass (
134
- \ Magento \ Eav \ Model \ Entity \ Attribute \ AbstractAttribute::class,
150
+ AbstractAttribute::class,
135
151
[],
136
152
'' ,
137
153
false ,
138
154
true ,
139
155
true ,
140
156
['getEntity ' ]
141
157
);
142
- $ attributeMock ->expects ($ this ->any ())
158
+ $ attributeMock ->expects ($ this ->once ())
143
159
->method ('getEntity ' )
144
160
->willReturn ($ entityMock );
145
161
146
- $ resourceMock = $ this ->getMockBuilder (\ Magento \ Catalog \ Model \ ResourceModel \Product ::class)
162
+ $ resourceMock = $ this ->getMockBuilder (ProductResourceModel ::class)
147
163
->disableOriginalConstructor ()
148
164
->setMethods (['getAttributeRawValue ' , 'duplicate ' , 'getAttribute ' ])
149
165
->getMock ();
150
- $ resourceMock ->expects ($ this ->any ())
166
+ $ resourceMock ->expects ($ this ->once ())
151
167
->method ('getAttributeRawValue ' )
152
168
->willReturn ('urk-key-1 ' );
153
- $ resourceMock ->expects ($ this ->any ( ))
169
+ $ resourceMock ->expects ($ this ->exactly ( 2 ))
154
170
->method ('getAttribute ' )
155
171
->willReturn ($ attributeMock );
156
172
157
- $ this ->productMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ resourceMock ));
173
+ $ this ->productMock ->expects ($ this ->exactly (2 ))
174
+ ->method ('getResource ' )
175
+ ->willReturn ($ resourceMock );
158
176
159
177
$ duplicateMock = $ this ->createPartialMock (
160
178
Product::class,
@@ -176,51 +194,71 @@ public function testCopy()
176
194
'getStoreIds ' ,
177
195
'setMetaTitle ' ,
178
196
'setMetaKeyword ' ,
179
- 'setMetaDescription '
197
+ 'setMetaDescription ' ,
180
198
]
181
199
);
182
- $ this ->productFactoryMock ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ duplicateMock ));
200
+ $ this ->productFactoryMock ->expects ($ this ->once ())
201
+ ->method ('create ' )
202
+ ->willReturn ($ duplicateMock );
183
203
184
204
$ duplicateMock ->expects ($ this ->once ())->method ('setOptions ' )->with ([]);
185
205
$ duplicateMock ->expects ($ this ->once ())->method ('setIsDuplicate ' )->with (true );
186
206
$ duplicateMock ->expects ($ this ->once ())->method ('setOriginalLinkId ' )->with (1 );
187
- $ duplicateMock ->expects (
188
- $ this ->once ()
189
- )->method (
190
- 'setStatus '
191
- )->with (
192
- \Magento \Catalog \Model \Product \Attribute \Source \Status::STATUS_DISABLED
193
- );
207
+ $ duplicateMock ->expects ($ this ->once ())
208
+ ->method ('setStatus ' )
209
+ ->with (Status::STATUS_DISABLED );
194
210
$ duplicateMock ->expects ($ this ->atLeastOnce ())->method ('setStoreId ' );
195
- $ duplicateMock ->expects ($ this ->once ())->method ('setCreatedAt ' )->with (null );
196
- $ duplicateMock ->expects ($ this ->once ())->method ('setUpdatedAt ' )->with (null );
197
- $ duplicateMock ->expects ($ this ->once ())->method ('setId ' )->with (null );
198
- $ duplicateMock ->expects ($ this ->once ())->method ('setMetaTitle ' )->with (null );
199
- $ duplicateMock ->expects ($ this ->once ())->method ('setMetaKeyword ' )->with (null );
200
- $ duplicateMock ->expects ($ this ->once ())->method ('setMetaDescription ' )->with (null );
201
- $ duplicateMock ->expects ($ this ->atLeastOnce ())->method ('getStoreIds ' )->willReturn ([]);
202
- $ duplicateMock ->expects ($ this ->atLeastOnce ())->method ('setData ' )->willReturn ($ duplicateMock );
203
- $ this ->copyConstructorMock ->expects ($ this ->once ())->method ('build ' )->with ($ this ->productMock , $ duplicateMock );
204
- $ duplicateMock ->expects ($ this ->once ())->method ('setUrlKey ' )->with ('urk-key-2 ' )->willReturn ($ duplicateMock );
205
- $ duplicateMock ->expects ($ this ->once ())->method ('save ' );
206
-
207
- $ this ->metadata ->expects ($ this ->any ())->method ('getLinkField ' )->willReturn ('linkField ' );
208
-
209
- $ duplicateMock ->expects ($ this ->any ())->method ('getData ' )->willReturnMap ([
210
- ['linkField ' , null , '2 ' ],
211
- ]);
211
+ $ duplicateMock ->expects ($ this ->once ())
212
+ ->method ('setCreatedAt ' )
213
+ ->with (null );
214
+ $ duplicateMock ->expects ($ this ->once ())
215
+ ->method ('setUpdatedAt ' )
216
+ ->with (null );
217
+ $ duplicateMock ->expects ($ this ->once ())
218
+ ->method ('setId ' )
219
+ ->with (null );
220
+ $ duplicateMock ->expects ($ this ->once ())
221
+ ->method ('setMetaTitle ' )
222
+ ->with (null );
223
+ $ duplicateMock ->expects ($ this ->once ())
224
+ ->method ('setMetaKeyword ' )
225
+ ->with (null );
226
+ $ duplicateMock ->expects ($ this ->once ())
227
+ ->method ('setMetaDescription ' )
228
+ ->with (null );
229
+ $ duplicateMock ->expects ($ this ->atLeastOnce ())
230
+ ->method ('getStoreIds ' )->willReturn ([]);
231
+ $ duplicateMock ->expects ($ this ->atLeastOnce ())
232
+ ->method ('setData ' )
233
+ ->willReturn ($ duplicateMock );
234
+ $ this ->copyConstructorMock ->expects ($ this ->once ())
235
+ ->method ('build ' )
236
+ ->with ($ this ->productMock , $ duplicateMock );
237
+ $ duplicateMock ->expects ($ this ->once ())
238
+ ->method ('setUrlKey ' )
239
+ ->with ('urk-key-2 ' )
240
+ ->willReturn ($ duplicateMock );
241
+ $ duplicateMock ->expects ($ this ->once ())
242
+ ->method ('save ' );
243
+ $ this ->metadata ->expects ($ this ->once ())
244
+ ->method ('getLinkField ' )
245
+ ->willReturn ('linkField ' );
246
+ $ duplicateMock ->expects ($ this ->never ())
247
+ ->method ('getData ' );
212
248
$ this ->optionRepositoryMock ->expects ($ this ->once ())
213
249
->method ('duplicate ' )
214
250
->with ($ this ->productMock , $ duplicateMock );
215
- $ resourceMock ->expects ($ this ->once ())->method ('duplicate ' )->with (1 , 2 );
216
251
217
252
$ this ->assertEquals ($ duplicateMock , $ this ->_model ->copy ($ this ->productMock ));
218
253
}
219
254
220
255
/**
256
+ * Test duplicate product with `UrlAlreadyExistsException` while copy stores url
257
+ *
258
+ * @return void
221
259
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
222
260
*/
223
- public function testUrlAlreadyExistsExceptionWhileCopyStoresUrl ()
261
+ public function testUrlAlreadyExistsExceptionWhileCopyStoresUrl (): void
224
262
{
225
263
$ stockItem = $ this ->getMockBuilder (StockItemInterface::class)
226
264
->getMock ();
0 commit comments