6
6
7
7
namespace Magento \Catalog \Helper \Product ;
8
8
9
+ use Magento \Catalog \Helper \Data ;
10
+ use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
11
+ use Magento \Store \Test \Fixture \Group as StoreGroupFixture ;
12
+ use Magento \Store \Test \Fixture \Store as StoreFixture ;
13
+ use Magento \Store \Test \Fixture \Website as WebsiteFixture ;
14
+ use Magento \TestFramework \Fixture \Config ;
15
+ use Magento \TestFramework \Fixture \DataFixture ;
16
+ use Magento \TestFramework \Fixture \DataFixtureStorage ;
17
+ use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
18
+ use Magento \Store \Model \StoreManagerInterface ;
19
+ use Magento \Customer \Model \Visitor ;
20
+
9
21
class CompareTest extends \PHPUnit \Framework \TestCase
10
22
{
11
23
/**
12
24
* @var \Magento\Catalog\Helper\Product\Compare
13
25
*/
14
26
protected $ _helper ;
15
27
28
+ /** @var StoreManagerInterface */
29
+ private $ storeManager ;
30
+
31
+ /**
32
+ * @var DataFixtureStorage
33
+ */
34
+ private $ fixtures ;
35
+
16
36
/**
17
37
* @var \Magento\Framework\ObjectManagerInterface
18
38
*/
@@ -22,6 +42,8 @@ protected function setUp(): void
22
42
{
23
43
$ this ->_objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
24
44
$ this ->_helper = $ this ->_objectManager ->get (\Magento \Catalog \Helper \Product \Compare::class);
45
+ $ this ->fixtures = $ this ->_objectManager ->get (DataFixtureStorageManager::class)->getStorage ();
46
+ $ this ->storeManager = $ this ->_objectManager ->get (StoreManagerInterface::class);
25
47
}
26
48
27
49
public function testGetListUrl ()
@@ -73,25 +95,22 @@ public function testGetClearListUrl()
73
95
);
74
96
}
75
97
76
- /**
77
- * @see testGetListUrl() for coverage of customer case
78
- */
79
- public function testGetItemCollection ()
80
- {
81
- $ this ->assertInstanceOf (
82
- \Magento \Catalog \Model \ResourceModel \Product \Compare \Item \Collection::class,
83
- $ this ->_helper ->getItemCollection ()
84
- );
85
- }
86
-
87
98
/**
88
99
* calculate()
89
100
* getItemCount()
90
101
* hasItems()
91
102
*
92
- * @magentoDataFixture Magento/Catalog/_files/multiple_products.php
93
103
* @magentoDbIsolation disabled
94
104
*/
105
+ #[
106
+ Config(Data::XML_PATH_PRICE_SCOPE , Data::PRICE_SCOPE_WEBSITE ),
107
+ DataFixture(WebsiteFixture::class, as: 'website2 ' ),
108
+ DataFixture(StoreGroupFixture::class, ['website_id ' => '$website2.id$ ' ], 'store_group2 ' ),
109
+ DataFixture(StoreFixture::class, ['store_group_id ' => '$store_group2.id$ ' ], 'store2 ' ),
110
+ DataFixture(ProductFixture::class, ['website_ids ' => [1 ]], as: 'product1 ' ),
111
+ DataFixture(ProductFixture::class, ['website_ids ' => [1 , '$website2.id$ ' ]], as: 'product2 ' ),
112
+ DataFixture(ProductFixture::class, ['website_ids ' => ['$website2.id$ ' ]], as: 'product3 ' ),
113
+ ]
95
114
public function testCalculate ()
96
115
{
97
116
/** @var \Magento\Catalog\Model\Session $session */
@@ -101,18 +120,53 @@ public function testCalculate()
101
120
$ this ->assertFalse ($ this ->_helper ->hasItems ());
102
121
$ this ->assertEquals (0 , $ session ->getCatalogCompareItemsCount ());
103
122
104
- $ this ->_populateCompareList ();
123
+ $ visitor = $ this ->_objectManager ->get (Visitor::class);
124
+ $ visitor ->setVisitorId (1 );
125
+ $ this ->_populateCompareList ('product1 ' );
126
+ $ this ->_populateCompareList ('product2 ' );
105
127
$ this ->_helper ->calculate ();
106
128
$ this ->assertEquals (2 , $ session ->getCatalogCompareItemsCount ());
107
129
$ this ->assertTrue ($ this ->_helper ->hasItems ());
108
130
131
+ $ secondStore = $ this ->fixtures ->get ('store2 ' )->getCode ();
132
+ $ this ->storeManager ->setCurrentStore ($ secondStore );
133
+ $ this ->_helper ->calculate ();
134
+ $ this ->assertEquals (0 , $ session ->getCatalogCompareItemsCount ());
135
+ $ this ->_populateCompareList ('product3 ' );
136
+ $ this ->_helper ->calculate ();
137
+ $ this ->assertEquals (1 , $ session ->getCatalogCompareItemsCount ());
138
+ $ this ->assertTrue ($ this ->_helper ->hasItems ());
139
+ $ this ->_populateCompareList ('product2 ' );
140
+ $ this ->_helper ->calculate ();
141
+ $ this ->assertEquals (2 , $ session ->getCatalogCompareItemsCount ());
142
+ $ this ->assertTrue ($ this ->_helper ->hasItems ());
143
+ $ compareItems = $ this ->_helper ->getItemCollection ();
144
+ $ compareItems ->clear ();
145
+ $ session ->unsCatalogCompareItemsCountPerWebsite ();
146
+ $ this ->assertFalse ($ this ->_helper ->hasItems ());
147
+ $ this ->assertEquals (0 , $ session ->getCatalogCompareItemsCount ());
148
+ $ this ->storeManager ->setCurrentStore (1 );
149
+ $ this ->_helper ->calculate ();
150
+ $ this ->assertEquals (2 , $ session ->getCatalogCompareItemsCount ());
151
+ $ this ->assertTrue ($ this ->_helper ->hasItems ());
109
152
$ session ->unsCatalogCompareItemsCount ();
110
153
} catch (\Exception $ e ) {
111
154
$ session ->unsCatalogCompareItemsCount ();
112
155
throw $ e ;
113
156
}
114
157
}
115
158
159
+ /**
160
+ * @see testGetListUrl() for coverage of customer case
161
+ */
162
+ public function testGetItemCollection ()
163
+ {
164
+ $ this ->assertInstanceOf (
165
+ \Magento \Catalog \Model \ResourceModel \Product \Compare \Item \Collection::class,
166
+ $ this ->_helper ->getItemCollection ()
167
+ );
168
+ }
169
+
116
170
public function testSetGetAllowUsedFlat ()
117
171
{
118
172
$ this ->assertTrue ($ this ->_helper ->getAllowUsedFlat ());
@@ -130,14 +184,14 @@ protected function _testGetProductUrl($method, $expectedFullAction)
130
184
131
185
/**
132
186
* Add products from fixture to compare list
187
+ *
188
+ * @param string $sku
133
189
*/
134
- protected function _populateCompareList ()
190
+ protected function _populateCompareList (string $ sku )
135
191
{
136
- $ productRepository = $ this ->_objectManager ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
137
- $ productOne = $ productRepository ->get ('simple1 ' );
138
- $ productTwo = $ productRepository ->get ('simple2 ' );
192
+ $ product = $ this ->fixtures ->get ($ sku );
139
193
/** @var $compareList \Magento\Catalog\Model\Product\Compare\ListCompare */
140
194
$ compareList = $ this ->_objectManager ->create (\Magento \Catalog \Model \Product \Compare \ListCompare::class);
141
- $ compareList ->addProduct ($ productOne )-> addProduct ( $ productTwo );
195
+ $ compareList ->addProduct ($ product );
142
196
}
143
197
}
0 commit comments