66
77namespace Magento \Catalog \Helper \Product ;
88
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+
921class CompareTest extends \PHPUnit \Framework \TestCase
1022{
1123 /**
1224 * @var \Magento\Catalog\Helper\Product\Compare
1325 */
1426 protected $ _helper ;
1527
28+ /** @var StoreManagerInterface */
29+ private $ storeManager ;
30+
31+ /**
32+ * @var DataFixtureStorage
33+ */
34+ private $ fixtures ;
35+
1636 /**
1737 * @var \Magento\Framework\ObjectManagerInterface
1838 */
@@ -22,6 +42,8 @@ protected function setUp(): void
2242 {
2343 $ this ->_objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
2444 $ 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);
2547 }
2648
2749 public function testGetListUrl ()
@@ -73,25 +95,22 @@ public function testGetClearListUrl()
7395 );
7496 }
7597
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-
8798 /**
8899 * calculate()
89100 * getItemCount()
90101 * hasItems()
91102 *
92- * @magentoDataFixture Magento/Catalog/_files/multiple_products.php
93103 * @magentoDbIsolation disabled
94104 */
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+ ]
95114 public function testCalculate ()
96115 {
97116 /** @var \Magento\Catalog\Model\Session $session */
@@ -101,18 +120,53 @@ public function testCalculate()
101120 $ this ->assertFalse ($ this ->_helper ->hasItems ());
102121 $ this ->assertEquals (0 , $ session ->getCatalogCompareItemsCount ());
103122
104- $ this ->_populateCompareList ();
123+ $ visitor = $ this ->_objectManager ->get (Visitor::class);
124+ $ visitor ->setVisitorId (1 );
125+ $ this ->_populateCompareList ('product1 ' );
126+ $ this ->_populateCompareList ('product2 ' );
105127 $ this ->_helper ->calculate ();
106128 $ this ->assertEquals (2 , $ session ->getCatalogCompareItemsCount ());
107129 $ this ->assertTrue ($ this ->_helper ->hasItems ());
108130
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 ());
109152 $ session ->unsCatalogCompareItemsCount ();
110153 } catch (\Exception $ e ) {
111154 $ session ->unsCatalogCompareItemsCount ();
112155 throw $ e ;
113156 }
114157 }
115158
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+
116170 public function testSetGetAllowUsedFlat ()
117171 {
118172 $ this ->assertTrue ($ this ->_helper ->getAllowUsedFlat ());
@@ -130,14 +184,14 @@ protected function _testGetProductUrl($method, $expectedFullAction)
130184
131185 /**
132186 * Add products from fixture to compare list
187+ *
188+ * @param string $sku
133189 */
134- protected function _populateCompareList ()
190+ protected function _populateCompareList (string $ sku )
135191 {
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 );
139193 /** @var $compareList \Magento\Catalog\Model\Product\Compare\ListCompare */
140194 $ compareList = $ this ->_objectManager ->create (\Magento \Catalog \Model \Product \Compare \ListCompare::class);
141- $ compareList ->addProduct ($ productOne )-> addProduct ( $ productTwo );
195+ $ compareList ->addProduct ($ product );
142196 }
143197}
0 commit comments