1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2024 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
14
14
use Magento \Catalog \Model \Product \Price \Validation \TierPriceValidator ;
15
15
use Magento \Catalog \Model \Product \Type ;
16
16
use Magento \Catalog \Model \ProductIdLocatorInterface ;
17
- use Magento \Customer \Api \Data \GroupInterface ;
18
- use Magento \Customer \Api \Data \GroupSearchResultsInterface ;
19
- use Magento \Customer \Api \GroupRepositoryInterface ;
20
- use Magento \Framework \Api \AbstractSimpleObject ;
21
- use Magento \Framework \Api \FilterBuilder ;
22
- use Magento \Framework \Api \Search \SearchCriteriaInterface ;
23
- use Magento \Framework \Api \SearchCriteriaBuilder ;
17
+ use Magento \Framework \App \ResourceConnection ;
18
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
19
+ use Magento \Framework \DB \Select ;
24
20
use Magento \Framework \Exception \NoSuchEntityException ;
25
21
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
26
22
use Magento \Store \Api \Data \WebsiteInterface ;
@@ -45,21 +41,6 @@ class TierPriceValidatorTest extends TestCase
45
41
*/
46
42
private $ productIdLocator ;
47
43
48
- /**
49
- * @var SearchCriteriaBuilder|MockObject
50
- */
51
- private $ searchCriteriaBuilder ;
52
-
53
- /**
54
- * @var FilterBuilder|MockObject
55
- */
56
- private $ filterBuilder ;
57
-
58
- /**
59
- * @var GroupRepositoryInterface|MockObject
60
- */
61
- private $ customerGroupRepository ;
62
-
63
44
/**
64
45
* @var WebsiteRepositoryInterface|MockObject
65
46
*/
@@ -85,6 +66,16 @@ class TierPriceValidatorTest extends TestCase
85
66
*/
86
67
private $ productRepository ;
87
68
69
+ /**
70
+ * @var ResourceConnection|MockObject
71
+ */
72
+ private $ resourceConnectionMock ;
73
+
74
+ /**
75
+ * @var AdapterInterface|MockObject
76
+ */
77
+ private $ adapterInterface ;
78
+
88
79
/**
89
80
* {@inheritdoc}
90
81
*/
@@ -93,15 +84,6 @@ protected function setUp(): void
93
84
$ this ->productIdLocator = $ this ->getMockBuilder (ProductIdLocatorInterface::class)
94
85
->disableOriginalConstructor ()
95
86
->getMockForAbstractClass ();
96
- $ this ->searchCriteriaBuilder = $ this ->getMockBuilder (SearchCriteriaBuilder::class)
97
- ->disableOriginalConstructor ()
98
- ->getMock ();
99
- $ this ->filterBuilder = $ this ->getMockBuilder (FilterBuilder::class)
100
- ->disableOriginalConstructor ()
101
- ->getMock ();
102
- $ this ->customerGroupRepository = $ this ->getMockBuilder (GroupRepositoryInterface::class)
103
- ->disableOriginalConstructor ()
104
- ->getMockForAbstractClass ();
105
87
$ this ->websiteRepository = $ this ->getMockBuilder (WebsiteRepositoryInterface::class)
106
88
->disableOriginalConstructor ()
107
89
->getMockForAbstractClass ();
@@ -118,51 +100,54 @@ protected function setUp(): void
118
100
$ this ->productRepository = $ this ->getMockBuilder (ProductRepositoryInterface::class)
119
101
->disableOriginalConstructor ()
120
102
->getMockForAbstractClass ();
103
+ $ this ->resourceConnectionMock = $ this ->getMockBuilder (ResourceConnection::class)
104
+ ->disableOriginalConstructor ()
105
+ ->getMock ();
106
+
107
+ $ this ->adapterInterface = $ this ->getMockBuilder (AdapterInterface::class)
108
+ ->disableOriginalConstructor ()
109
+ ->getMockForAbstractClass ();
121
110
122
111
$ objectManagerHelper = new ObjectManager ($ this );
123
112
$ this ->tierPriceValidator = $ objectManagerHelper ->getObject (
124
113
TierPriceValidator::class,
125
114
[
126
115
'productIdLocator ' => $ this ->productIdLocator ,
127
- 'searchCriteriaBuilder ' => $ this ->searchCriteriaBuilder ,
128
- 'filterBuilder ' => $ this ->filterBuilder ,
129
- 'customerGroupRepository ' => $ this ->customerGroupRepository ,
130
116
'websiteRepository ' => $ this ->websiteRepository ,
131
117
'validationResult ' => $ this ->validationResult ,
132
118
'invalidSkuProcessor ' => $ this ->invalidSkuProcessor ,
133
- 'productRepository ' => $ this ->productRepository
119
+ 'productRepository ' => $ this ->productRepository ,
120
+ 'resourceConnection ' => $ this ->resourceConnectionMock
134
121
]
135
122
);
136
123
}
137
124
138
125
/**
139
126
* Prepare CustomerGroupRepository mock.
140
127
*
141
- * @param array $returned
142
128
* @return void
143
129
*/
144
- private function prepareCustomerGroupRepositoryMock (array $ returned )
130
+ private function prepareCustomerGroupRepositoryMock ()
145
131
{
146
- $ searchCriteria = $ this
147
- ->getMockBuilder (SearchCriteriaInterface::class)
148
- ->disableOriginalConstructor ()
149
- ->getMockForAbstractClass ();
150
- $ filter = $ this ->getMockBuilder (AbstractSimpleObject::class)
151
- ->disableOriginalConstructor ()
152
- ->getMockForAbstractClass ();
153
- $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('setField ' )->willReturnSelf ();
154
- $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('setValue ' )->willReturnSelf ();
155
- $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturn ($ filter );
156
- $ this ->searchCriteriaBuilder ->expects ($ this ->atLeastOnce ())->method ('addFilters ' )->willReturnSelf ();
157
- $ this ->searchCriteriaBuilder ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturn ($ searchCriteria );
158
- $ customerGroupSearchResults = $ this
159
- ->getMockBuilder (GroupSearchResultsInterface::class)
160
- ->disableOriginalConstructor ()
161
- ->getMockForAbstractClass ();
162
- $ customerGroupSearchResults ->expects ($ this ->once ())->method ('getItems ' )
163
- ->willReturn ($ returned ['customerGroupSearchResults_getItems ' ]);
164
- $ this ->customerGroupRepository ->expects ($ this ->atLeastOnce ())->method ('getList ' )
165
- ->willReturn ($ customerGroupSearchResults );
132
+ $ select = $ this ->createMock (Select::class);
133
+ $ select ->expects ($ this ->once ())
134
+ ->method ('from ' )
135
+ ->with ('customer_group ' , 'customer_group_id ' )
136
+ ->willReturnSelf ();
137
+ $ select ->expects ($ this ->once ())
138
+ ->method ('where ' )
139
+ ->with ('customer_group_code = ? ' , 'test_group ' )
140
+ ->willReturnSelf ();
141
+ $ this ->adapterInterface ->expects ($ this ->once ())
142
+ ->method ('select ' )
143
+ ->willReturn ($ select );
144
+
145
+ $ this ->resourceConnectionMock ->expects ($ this ->once ())
146
+ ->method ('getConnection ' )
147
+ ->willReturn ($ this ->adapterInterface );
148
+ $ this ->resourceConnectionMock ->expects ($ this ->once ())
149
+ ->method ('getTableName ' )
150
+ ->willReturnArgument (0 );
166
151
}
167
152
168
153
/**
@@ -251,11 +236,6 @@ public function testValidateSkus()
251
236
*/
252
237
public function testRetrieveValidationResult (array $ returned )
253
238
{
254
- if (!empty ($ returned ['customerGroupSearchResults_getItems ' ])) {
255
- $ groupSearchResult = $ returned ['customerGroupSearchResults_getItems ' ][0 ];
256
- $ returned ['customerGroupSearchResults_getItems ' ][0 ] = $ groupSearchResult ($ this );
257
- }
258
-
259
239
$ sku = 'ASDF234234 ' ;
260
240
$ prices = [$ this ->tierPrice ];
261
241
$ existingPrices = [$ this ->tierPrice ];
@@ -264,26 +244,14 @@ public function testRetrieveValidationResult(array $returned)
264
244
->disableOriginalConstructor ()
265
245
->getMockForAbstractClass ();
266
246
$ this ->websiteRepository ->expects ($ this ->atLeastOnce ())->method ('getById ' )->willReturn ($ website );
267
- $ this ->prepareCustomerGroupRepositoryMock ($ returned );
247
+ $ this ->prepareCustomerGroupRepositoryMock ();
268
248
269
249
$ this ->assertEquals (
270
250
$ this ->validationResult ,
271
251
$ this ->tierPriceValidator ->retrieveValidationResult ($ prices , $ existingPrices )
272
252
);
273
253
}
274
254
275
- protected function getMockForCustomerGroup ($ customerGroupName )
276
- {
277
- $ customerGroup = $ this ->getMockBuilder (GroupInterface::class)
278
- ->onlyMethods (['getCode ' , 'getId ' ])
279
- ->disableOriginalConstructor ()
280
- ->getMockForAbstractClass ();
281
- $ customerGroup ->expects ($ this ->atLeastOnce ())->method ('getCode ' )->willReturn ($ customerGroupName );
282
- $ customerGroupId = 23 ;
283
- $ customerGroup ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn ($ customerGroupId );
284
- return $ customerGroup ;
285
- }
286
-
287
255
/**
288
256
* Data provider for retrieveValidationResult() test.
289
257
*
@@ -292,21 +260,17 @@ protected function getMockForCustomerGroup($customerGroupName)
292
260
public static function retrieveValidationResultDataProvider ()
293
261
{
294
262
$ customerGroupName = 'test_Group ' ;
295
- $ customerGroup = static fn (self $ testCase ) => $ testCase ->getMockForCustomerGroup ($ customerGroupName );
296
-
297
263
return [
298
264
[
299
265
[
300
266
'tierPrice_getCustomerGroup ' => $ customerGroupName ,
301
- 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_DISCOUNT ,
302
- 'customerGroupSearchResults_getItems ' => [$ customerGroup ]
267
+ 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_DISCOUNT
303
268
]
304
269
],
305
270
[
306
271
[
307
272
'tierPrice_getCustomerGroup ' => $ customerGroupName ,
308
- 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_FIXED ,
309
- 'customerGroupSearchResults_getItems ' => []
273
+ 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_FIXED
310
274
]
311
275
]
312
276
];
@@ -325,13 +289,12 @@ public function testRetrieveValidationResultWithException()
325
289
$ existingPrices = [$ this ->tierPrice ];
326
290
$ returned = [
327
291
'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_DISCOUNT ,
328
- 'customerGroupSearchResults_getItems ' => [],
329
292
'tierPrice_getCustomerGroup ' => $ customerGroupName ,
330
293
];
331
294
$ this ->prepareRetrieveValidationResultMethod ($ sku , $ returned );
332
295
$ exception = new NoSuchEntityException ();
333
296
$ this ->websiteRepository ->expects ($ this ->atLeastOnce ())->method ('getById ' )->willThrowException ($ exception );
334
- $ this ->prepareCustomerGroupRepositoryMock ($ returned );
297
+ $ this ->prepareCustomerGroupRepositoryMock ();
335
298
336
299
$ this ->assertEquals (
337
300
$ this ->validationResult ,
0 commit comments