33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Catalog \Model \Layer \Filter \DataProvider ;
89
10+ use Magento \Catalog \Model \Layer \Category ;
11+ use Magento \Catalog \Model \Layer \Resolver ;
12+ use Magento \Framework \ObjectManagerInterface ;
13+ use Magento \TestFramework \Helper \Bootstrap ;
14+ use PHPUnit \Framework \TestCase ;
15+
916/**
1017 * Test class for \Magento\Catalog\Model\Layer\Filter\DataProvider\Price.
11- *
12- * @magentoAppIsolation enabled
1318 */
14- class PriceTest extends \ PHPUnit \ Framework \ TestCase
19+ class PriceTest extends TestCase
1520{
21+ /** @var ObjectManagerInterface */
22+ private $ objectManager ;
23+
24+ /** @var Price */
25+ private $ model ;
26+
27+ /** @var Resolver */
28+ private $ layerResolver ;
29+
30+ /** @var Category */
31+ private $ layer ;
32+
33+ /** @var PriceFactory */
34+ private $ dataProviderPriceFactory ;
35+
1636 /**
17- * @var \Magento\Catalog\Model\Layer\Filter\DataProvider\Price
37+ * @inheritdoc
1838 */
19- protected $ _model ;
20-
2139 protected function setUp (): void
2240 {
23- $ category = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
24- \Magento \Catalog \Model \Category::class
25- );
26- $ category ->load (4 );
27- $ layer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
28- ->get (\Magento \Catalog \Model \Layer \Category::class);
29- $ layer ->setCurrentCategory ($ category );
30- $ this ->_model = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()
31- ->create (\Magento \Catalog \Model \Layer \Filter \DataProvider \Price::class, ['layer ' => $ layer ]);
41+ parent ::setUp ();
42+
43+ $ this ->objectManager = Bootstrap::getObjectManager ();
44+ $ this ->layerResolver = $ this ->objectManager ->get (Resolver::class);
45+ $ this ->layer = $ this ->layerResolver ->get ();
46+ $ this ->dataProviderPriceFactory = $ this ->objectManager ->get (PriceFactory::class);
47+ $ this ->model = $ this ->dataProviderPriceFactory ->create (['layer ' => $ this ->layer ]);
3248 }
3349
3450 /**
3551 * @magentoDataFixture Magento/Catalog/_files/categories.php
3652 * @magentoAppIsolation enabled
3753 * @magentoDbIsolation disabled
3854 * @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation auto
55+ * @return void
3956 */
40- public function testGetPriceRangeAuto ()
57+ public function testGetPriceRangeAuto (): void
4158 {
42- $ this ->assertEquals (10 , $ this ->_model ->getPriceRange ());
59+ $ this ->layer ->setCurrentCategory (4 );
60+ $ this ->assertEquals (10 , $ this ->model ->getPriceRange ());
4361 }
4462
4563 /**
@@ -48,45 +66,108 @@ public function testGetPriceRangeAuto()
4866 * @magentoDbIsolation disabled
4967 * @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
5068 * @magentoConfigFixture current_store catalog/layered_navigation/price_range_step 1.5
69+ * @return void
5170 */
52- public function testGetPriceRangeManual ()
71+ public function testGetPriceRangeManual (): void
5372 {
5473 // what you set is what you get
55- $ this ->assertEquals (1.5 , $ this ->_model ->getPriceRange ());
74+ $ this ->layer ->setCurrentCategory (4 );
75+ $ this ->assertEquals (1.5 , $ this ->model ->getPriceRange ());
5676 }
5777
5878 /**
5979 * @magentoDataFixture Magento/Catalog/_files/categories.php
6080 * @magentoAppIsolation enabled
6181 * @magentoDbIsolation disabled
82+ * @return void
6283 */
63- public function testGetMaxPriceInt ()
84+ public function testGetMaxPriceInt (): void
6485 {
65- $ this ->assertEquals (45.00 , $ this ->_model ->getMaxPrice ());
86+ $ this ->layer ->setCurrentCategory (4 );
87+ $ this ->assertEquals (45.00 , $ this ->model ->getMaxPrice ());
6688 }
6789
6890 /**
6991 * @return array
7092 */
71- public function getRangeItemCountsDataProvider ()
93+ public function getRangeItemCountsDataProvider (): array
7294 {
7395 return [
7496 // These are $inputRange, [$expectedItemCounts] values
7597 [1 , [11 => 2 , 46 => 1 , 16 => '1 ' ]],
7698 [10 , [2 => 3 , 5 => 1 ]],
7799 [20 , [1 => 3 , 3 => 1 ]],
78- [50 , [1 => 4 ]]
100+ [50 , [1 => 4 ]],
79101 ];
80102 }
81103
82104 /**
105+ * @dataProvider getRangeItemCountsDataProvider
83106 * @magentoDataFixture Magento/Catalog/_files/categories.php
107+ * @magentoAppIsolation enabled
84108 * @magentoDbIsolation disabled
85- * @dataProvider getRangeItemCountsDataProvider
109+ * @param int $inputRange
110+ * @param array $expectedItemCounts
111+ * @return void
112+ */
113+ public function testGetRangeItemCounts (int $ inputRange , array $ expectedItemCounts ): void
114+ {
115+ $ this ->layer ->setCurrentCategory (4 );
116+ $ actualItemCounts = $ this ->model ->getRangeItemCounts ($ inputRange );
117+ $ this ->assertEquals ($ expectedItemCounts , $ actualItemCounts );
118+ }
119+
120+ /**
121+ * @magentoConfigFixture current_store catalog/layered_navigation/price_range_max_intervals 3
122+ * @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
123+ * @magentoDataFixture Magento/Catalog/_files/products_for_search.php
124+ * @return void
86125 */
87- public function testGetRangeItemCounts ( $ inputRange , $ expectedItemCounts )
126+ public function testGetRangeItemCountsManualCalculation (): void
88127 {
89- $ actualItemCounts = $ this ->_model ->getRangeItemCounts ($ inputRange );
128+ $ expectedItemCounts = [11 => '2 ' , 21 => '1 ' , 31 => 2 ];
129+ $ this ->layer ->setCurrentCategory (333 );
130+ $ actualItemCounts = $ this ->model ->getRangeItemCounts (1 );
90131 $ this ->assertEquals ($ expectedItemCounts , $ actualItemCounts );
91132 }
133+
134+ /**
135+ * @dataProvider getAdditionalRequestDataDataProvider
136+ * @param array $priceFilters
137+ * @param string $expectedRequest
138+ * @return void
139+ */
140+ public function testGetAdditionalRequestData (array $ priceFilters , string $ expectedRequest ): void
141+ {
142+ $ filter = explode ('- ' , $ priceFilters [0 ]);
143+ $ this ->model ->setInterval ($ filter );
144+ $ priorFilters = $ this ->model ->getPriorFilters ($ priceFilters );
145+ if (!empty ($ priorFilters )) {
146+ $ this ->model ->setPriorIntervals ($ priorFilters );
147+ }
148+
149+ $ actualRequest = $ this ->model ->getAdditionalRequestData ();
150+ $ this ->assertEquals ($ expectedRequest , $ actualRequest );
151+ }
152+
153+ /**
154+ * @return array
155+ */
156+ public function getAdditionalRequestDataDataProvider (): array
157+ {
158+ return [
159+ 'with_prior_filters ' => [
160+ 'price_filters ' => ['10-11 ' , '20-21 ' , '30-31 ' ],
161+ 'expected_request ' => ',10-11,20-21,30-31 ' ,
162+ ],
163+ 'without_prior_filters ' => [
164+ 'price_filters ' => ['10-11 ' ],
165+ 'expected_request ' => ',10-11 ' ,
166+ ],
167+ 'not_valid_prior_filters ' => [
168+ 'price_filters ' => ['10-11 ' , '20-21 ' , '31 ' , '40-41 ' ],
169+ 'expected_request ' => ',10-11 ' ,
170+ ],
171+ ];
172+ }
92173}
0 commit comments