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 \Model \Layer \Filter \DataProvider ;
8
9
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
+
9
16
/**
10
17
* Test class for \Magento\Catalog\Model\Layer\Filter\DataProvider\Price.
11
- *
12
- * @magentoAppIsolation enabled
13
18
*/
14
- class PriceTest extends \ PHPUnit \ Framework \ TestCase
19
+ class PriceTest extends TestCase
15
20
{
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
+
16
36
/**
17
- * @var \Magento\Catalog\Model\Layer\Filter\DataProvider\Price
37
+ * @inheritdoc
18
38
*/
19
- protected $ _model ;
20
-
21
39
protected function setUp (): void
22
40
{
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 ]);
32
48
}
33
49
34
50
/**
35
51
* @magentoDataFixture Magento/Catalog/_files/categories.php
36
52
* @magentoAppIsolation enabled
37
53
* @magentoDbIsolation disabled
38
54
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation auto
55
+ * @return void
39
56
*/
40
- public function testGetPriceRangeAuto ()
57
+ public function testGetPriceRangeAuto (): void
41
58
{
42
- $ this ->assertEquals (10 , $ this ->_model ->getPriceRange ());
59
+ $ this ->layer ->setCurrentCategory (4 );
60
+ $ this ->assertEquals (10 , $ this ->model ->getPriceRange ());
43
61
}
44
62
45
63
/**
@@ -48,45 +66,108 @@ public function testGetPriceRangeAuto()
48
66
* @magentoDbIsolation disabled
49
67
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
50
68
* @magentoConfigFixture current_store catalog/layered_navigation/price_range_step 1.5
69
+ * @return void
51
70
*/
52
- public function testGetPriceRangeManual ()
71
+ public function testGetPriceRangeManual (): void
53
72
{
54
73
// 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 ());
56
76
}
57
77
58
78
/**
59
79
* @magentoDataFixture Magento/Catalog/_files/categories.php
60
80
* @magentoAppIsolation enabled
61
81
* @magentoDbIsolation disabled
82
+ * @return void
62
83
*/
63
- public function testGetMaxPriceInt ()
84
+ public function testGetMaxPriceInt (): void
64
85
{
65
- $ this ->assertEquals (45.00 , $ this ->_model ->getMaxPrice ());
86
+ $ this ->layer ->setCurrentCategory (4 );
87
+ $ this ->assertEquals (45.00 , $ this ->model ->getMaxPrice ());
66
88
}
67
89
68
90
/**
69
91
* @return array
70
92
*/
71
- public function getRangeItemCountsDataProvider ()
93
+ public function getRangeItemCountsDataProvider (): array
72
94
{
73
95
return [
74
96
// These are $inputRange, [$expectedItemCounts] values
75
97
[1 , [11 => 2 , 46 => 1 , 16 => '1 ' ]],
76
98
[10 , [2 => 3 , 5 => 1 ]],
77
99
[20 , [1 => 3 , 3 => 1 ]],
78
- [50 , [1 => 4 ]]
100
+ [50 , [1 => 4 ]],
79
101
];
80
102
}
81
103
82
104
/**
105
+ * @dataProvider getRangeItemCountsDataProvider
83
106
* @magentoDataFixture Magento/Catalog/_files/categories.php
107
+ * @magentoAppIsolation enabled
84
108
* @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
86
125
*/
87
- public function testGetRangeItemCounts ( $ inputRange , $ expectedItemCounts )
126
+ public function testGetRangeItemCountsManualCalculation (): void
88
127
{
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 );
90
131
$ this ->assertEquals ($ expectedItemCounts , $ actualItemCounts );
91
132
}
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
+ }
92
173
}
0 commit comments