7
7
8
8
namespace Magento \CatalogSearch \Model \Layer \Filter ;
9
9
10
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
12
+ use Magento \Catalog \Model \Layer \Category as CategoryLayer ;
13
+ use Magento \CatalogSearch \Model \Layer \Filter \Price as PriceFilter ;
10
14
use Magento \TestFramework \Helper \Bootstrap ;
11
15
12
16
/**
19
23
class PriceTest extends \PHPUnit \Framework \TestCase
20
24
{
21
25
/**
22
- * @var \Magento\CatalogSearch\Model\Layer\Filter\Price
26
+ * @var PriceFilter
23
27
*/
24
- protected $ _model ;
28
+ private $ model ;
25
29
26
30
/**
27
31
* @var \Magento\Framework\ObjectManagerInterface
@@ -31,37 +35,28 @@ class PriceTest extends \PHPUnit\Framework\TestCase
31
35
protected function setUp (): void
32
36
{
33
37
$ this ->objectManager = Bootstrap::getObjectManager ();
34
- $ category = $ this ->objectManager ->create (
35
- \Magento \Catalog \Model \Category::class
36
- );
37
- $ category ->load (4 );
38
- $ layer = $ this ->objectManager ->get (\Magento \Catalog \Model \Layer \Category::class);
39
- $ layer ->setCurrentCategory ($ category );
40
- $ this ->_model = $ this ->objectManager ->create (
41
- \Magento \CatalogSearch \Model \Layer \Filter \Price::class,
42
- ['layer ' => $ layer ]
43
- );
38
+ $ this ->initializePriceFilter ();
44
39
}
45
40
46
41
public function testApplyNothing ()
47
42
{
48
- $ this ->assertEmpty ($ this ->_model ->getData ('price_range ' ));
43
+ $ this ->assertEmpty ($ this ->model ->getData ('price_range ' ));
49
44
/** @var $request \Magento\TestFramework\Request */
50
45
$ request = $ this ->objectManager ->get (\Magento \TestFramework \Request::class);
51
- $ this ->_model ->apply ($ request );
46
+ $ this ->model ->apply ($ request );
52
47
53
- $ this ->assertEmpty ($ this ->_model ->getData ('price_range ' ));
48
+ $ this ->assertEmpty ($ this ->model ->getData ('price_range ' ));
54
49
}
55
50
56
51
public function testApplyInvalid ()
57
52
{
58
- $ this ->assertEmpty ($ this ->_model ->getData ('price_range ' ));
53
+ $ this ->assertEmpty ($ this ->model ->getData ('price_range ' ));
59
54
/** @var $request \Magento\TestFramework\Request */
60
55
$ request = $ this ->objectManager ->get (\Magento \TestFramework \Request::class);
61
56
$ request ->setParam ('price ' , 'non-numeric ' );
62
- $ this ->_model ->apply ($ request );
57
+ $ this ->model ->apply ($ request );
63
58
64
- $ this ->assertEmpty ($ this ->_model ->getData ('price_range ' ));
59
+ $ this ->assertEmpty ($ this ->model ->getData ('price_range ' ));
65
60
}
66
61
67
62
/**
@@ -72,7 +67,7 @@ public function testApplyManual()
72
67
/** @var $request \Magento\TestFramework\Request */
73
68
$ request = $ this ->objectManager ->get (\Magento \TestFramework \Request::class);
74
69
$ request ->setParam ('price ' , '10-20 ' );
75
- $ this ->_model ->apply ($ request );
70
+ $ this ->model ->apply ($ request );
76
71
}
77
72
78
73
/**
@@ -84,11 +79,11 @@ public function testApplyWithCustomCurrencyRate()
84
79
$ request = $ this ->objectManager ->get (\Magento \TestFramework \Request::class);
85
80
86
81
$ request ->setParam ('price ' , '10-20 ' );
87
- $ this ->_model ->setCurrencyRate (10 );
82
+ $ this ->model ->setCurrencyRate (10 );
88
83
89
- $ this ->_model ->apply ($ request );
84
+ $ this ->model ->apply ($ request );
90
85
91
- $ filters = $ this ->_model ->getLayer ()->getState ()->getFilters ();
86
+ $ filters = $ this ->model ->getLayer ()->getState ()->getFilters ();
92
87
$ this ->assertArrayHasKey (0 , $ filters );
93
88
$ this ->assertEquals (
94
89
'<span class="price">$100.00</span> - <span class="price">$199.99</span> ' ,
@@ -100,22 +95,64 @@ public function testGetSetCustomerGroupId()
100
95
{
101
96
$ this ->assertEquals (
102
97
\Magento \Customer \Model \GroupManagement::NOT_LOGGED_IN_ID ,
103
- $ this ->_model ->getCustomerGroupId ()
98
+ $ this ->model ->getCustomerGroupId ()
104
99
);
105
100
106
101
$ customerGroupId = 123 ;
107
- $ this ->_model ->setCustomerGroupId ($ customerGroupId );
102
+ $ this ->model ->setCustomerGroupId ($ customerGroupId );
108
103
109
- $ this ->assertEquals ($ customerGroupId , $ this ->_model ->getCustomerGroupId ());
104
+ $ this ->assertEquals ($ customerGroupId , $ this ->model ->getCustomerGroupId ());
110
105
}
111
106
112
107
public function testGetSetCurrencyRate ()
113
108
{
114
- $ this ->assertEquals (1 , $ this ->_model ->getCurrencyRate ());
109
+ $ this ->assertEquals (1 , $ this ->model ->getCurrencyRate ());
115
110
116
111
$ currencyRate = 42 ;
117
- $ this ->_model ->setCurrencyRate ($ currencyRate );
112
+ $ this ->model ->setCurrencyRate ($ currencyRate );
113
+
114
+ $ this ->assertEquals ($ currencyRate , $ this ->model ->getCurrencyRate ());
115
+ }
116
+
117
+ /**
118
+ * @magentoDbIsolation disabled
119
+ * @magentoConfigFixture current_store catalog/layered_navigation/price_range_calculation manual
120
+ * @magentoConfigFixture current_store catalog/layered_navigation/price_range_step 10
121
+ * @magentoConfigFixture current_store catalog/layered_navigation/price_range_max_intervals 2
122
+ * @magentoDataFixture Magento/CatalogSearch/_files/products_with_different_price.php
123
+ */
124
+ public function testGetItemsWithManualAlgorithm (): void
125
+ {
126
+ $ attributeRepository = $ this ->objectManager ->get (ProductAttributeRepositoryInterface::class);
127
+ $ priceAttribute = $ attributeRepository ->get ('price ' );
128
+ $ this ->model ->setAttributeModel ($ priceAttribute );
118
129
119
- $ this ->assertEquals ($ currencyRate , $ this ->_model ->getCurrencyRate ());
130
+ /** @var \Magento\Catalog\Model\Layer\Filter\Item[] $ranges */
131
+ $ ranges = $ this ->model ->getItems ();
132
+ self ::assertCount (2 , $ ranges );
133
+
134
+ $ request = $ this ->objectManager ->get (\Magento \TestFramework \Request::class);
135
+ foreach ($ ranges as $ range ) {
136
+ $ request ->setParam ('price ' , $ range ->getValueString ());
137
+ $ this ->initializePriceFilter ();
138
+ $ this ->model ->apply ($ request );
139
+ $ products = $ this ->model ->getLayer ()->getProductCollection ()->getItems ();
140
+ self ::assertCount ($ range ->getCount (), $ products );
141
+ }
142
+ }
143
+
144
+ /**
145
+ * @return void
146
+ */
147
+ private function initializePriceFilter (): void
148
+ {
149
+ $ categoryRepository = $ this ->objectManager ->get (CategoryRepositoryInterface::class);
150
+ $ category = $ categoryRepository ->get (4 );
151
+ $ layer = $ this ->objectManager ->create (CategoryLayer::class);
152
+ $ layer ->setCurrentCategory ($ category );
153
+ $ this ->model = $ this ->objectManager ->create (
154
+ PriceFilter::class,
155
+ ['layer ' => $ layer ]
156
+ );
120
157
}
121
158
}
0 commit comments