11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2024 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
1414use Magento \Catalog \Model \Product \Price \Validation \TierPriceValidator ;
1515use Magento \Catalog \Model \Product \Type ;
1616use 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 ;
2420use Magento \Framework \Exception \NoSuchEntityException ;
2521use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2622use Magento \Store \Api \Data \WebsiteInterface ;
@@ -45,21 +41,6 @@ class TierPriceValidatorTest extends TestCase
4541 */
4642 private $ productIdLocator ;
4743
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-
6344 /**
6445 * @var WebsiteRepositoryInterface|MockObject
6546 */
@@ -85,6 +66,16 @@ class TierPriceValidatorTest extends TestCase
8566 */
8667 private $ productRepository ;
8768
69+ /**
70+ * @var ResourceConnection|MockObject
71+ */
72+ private $ resourceConnectionMock ;
73+
74+ /**
75+ * @var AdapterInterface|MockObject
76+ */
77+ private $ adapterInterface ;
78+
8879 /**
8980 * {@inheritdoc}
9081 */
@@ -93,15 +84,6 @@ protected function setUp(): void
9384 $ this ->productIdLocator = $ this ->getMockBuilder (ProductIdLocatorInterface::class)
9485 ->disableOriginalConstructor ()
9586 ->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 ();
10587 $ this ->websiteRepository = $ this ->getMockBuilder (WebsiteRepositoryInterface::class)
10688 ->disableOriginalConstructor ()
10789 ->getMockForAbstractClass ();
@@ -118,51 +100,54 @@ protected function setUp(): void
118100 $ this ->productRepository = $ this ->getMockBuilder (ProductRepositoryInterface::class)
119101 ->disableOriginalConstructor ()
120102 ->getMockForAbstractClass ();
103+ $ this ->resourceConnectionMock = $ this ->getMockBuilder (ResourceConnection::class)
104+ ->disableOriginalConstructor ()
105+ ->getMock ();
106+
107+ $ this ->adapterInterface = $ this ->getMockBuilder (AdapterInterface::class)
108+ ->disableOriginalConstructor ()
109+ ->getMockForAbstractClass ();
121110
122111 $ objectManagerHelper = new ObjectManager ($ this );
123112 $ this ->tierPriceValidator = $ objectManagerHelper ->getObject (
124113 TierPriceValidator::class,
125114 [
126115 'productIdLocator ' => $ this ->productIdLocator ,
127- 'searchCriteriaBuilder ' => $ this ->searchCriteriaBuilder ,
128- 'filterBuilder ' => $ this ->filterBuilder ,
129- 'customerGroupRepository ' => $ this ->customerGroupRepository ,
130116 'websiteRepository ' => $ this ->websiteRepository ,
131117 'validationResult ' => $ this ->validationResult ,
132118 'invalidSkuProcessor ' => $ this ->invalidSkuProcessor ,
133- 'productRepository ' => $ this ->productRepository
119+ 'productRepository ' => $ this ->productRepository ,
120+ 'resourceConnection ' => $ this ->resourceConnectionMock
134121 ]
135122 );
136123 }
137124
138125 /**
139126 * Prepare CustomerGroupRepository mock.
140127 *
141- * @param array $returned
142128 * @return void
143129 */
144- private function prepareCustomerGroupRepositoryMock (array $ returned )
130+ private function prepareCustomerGroupRepositoryMock ()
145131 {
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 );
166151 }
167152
168153 /**
@@ -251,11 +236,6 @@ public function testValidateSkus()
251236 */
252237 public function testRetrieveValidationResult (array $ returned )
253238 {
254- if (!empty ($ returned ['customerGroupSearchResults_getItems ' ])) {
255- $ groupSearchResult = $ returned ['customerGroupSearchResults_getItems ' ][0 ];
256- $ returned ['customerGroupSearchResults_getItems ' ][0 ] = $ groupSearchResult ($ this );
257- }
258-
259239 $ sku = 'ASDF234234 ' ;
260240 $ prices = [$ this ->tierPrice ];
261241 $ existingPrices = [$ this ->tierPrice ];
@@ -264,26 +244,14 @@ public function testRetrieveValidationResult(array $returned)
264244 ->disableOriginalConstructor ()
265245 ->getMockForAbstractClass ();
266246 $ this ->websiteRepository ->expects ($ this ->atLeastOnce ())->method ('getById ' )->willReturn ($ website );
267- $ this ->prepareCustomerGroupRepositoryMock ($ returned );
247+ $ this ->prepareCustomerGroupRepositoryMock ();
268248
269249 $ this ->assertEquals (
270250 $ this ->validationResult ,
271251 $ this ->tierPriceValidator ->retrieveValidationResult ($ prices , $ existingPrices )
272252 );
273253 }
274254
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-
287255 /**
288256 * Data provider for retrieveValidationResult() test.
289257 *
@@ -292,21 +260,17 @@ protected function getMockForCustomerGroup($customerGroupName)
292260 public static function retrieveValidationResultDataProvider ()
293261 {
294262 $ customerGroupName = 'test_Group ' ;
295- $ customerGroup = static fn (self $ testCase ) => $ testCase ->getMockForCustomerGroup ($ customerGroupName );
296-
297263 return [
298264 [
299265 [
300266 'tierPrice_getCustomerGroup ' => $ customerGroupName ,
301- 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_DISCOUNT ,
302- 'customerGroupSearchResults_getItems ' => [$ customerGroup ]
267+ 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_DISCOUNT
303268 ]
304269 ],
305270 [
306271 [
307272 'tierPrice_getCustomerGroup ' => $ customerGroupName ,
308- 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_FIXED ,
309- 'customerGroupSearchResults_getItems ' => []
273+ 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_FIXED
310274 ]
311275 ]
312276 ];
@@ -325,13 +289,12 @@ public function testRetrieveValidationResultWithException()
325289 $ existingPrices = [$ this ->tierPrice ];
326290 $ returned = [
327291 'tierPrice_getPriceType ' => TierPriceInterface::PRICE_TYPE_DISCOUNT ,
328- 'customerGroupSearchResults_getItems ' => [],
329292 'tierPrice_getCustomerGroup ' => $ customerGroupName ,
330293 ];
331294 $ this ->prepareRetrieveValidationResultMethod ($ sku , $ returned );
332295 $ exception = new NoSuchEntityException ();
333296 $ this ->websiteRepository ->expects ($ this ->atLeastOnce ())->method ('getById ' )->willThrowException ($ exception );
334- $ this ->prepareCustomerGroupRepositoryMock ($ returned );
297+ $ this ->prepareCustomerGroupRepositoryMock ();
335298
336299 $ this ->assertEquals (
337300 $ this ->validationResult ,
0 commit comments