33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Catalog \Model \ResourceModel \Product ;
79
10+ use Magento \Catalog \Model \Product \Visibility ;
11+ use Magento \Framework \App \Area ;
12+ use Magento \Framework \App \State ;
13+ use Magento \Store \Model \Store ;
14+ use Magento \TestFramework \Helper \Bootstrap ;
15+
816/**
917 * Collection test
18+ *
19+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1020 */
1121class CollectionTest extends \PHPUnit \Framework \TestCase
1222{
@@ -31,15 +41,15 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
3141 */
3242 protected function setUp ()
3343 {
34- $ this ->collection = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
44+ $ this ->collection = Bootstrap::getObjectManager ()->create (
3545 \Magento \Catalog \Model \ResourceModel \Product \Collection::class
3646 );
3747
38- $ this ->processor = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
48+ $ this ->processor = Bootstrap::getObjectManager ()->create (
3949 \Magento \Catalog \Model \Indexer \Product \Price \Processor::class
4050 );
4151
42- $ this ->productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
52+ $ this ->productRepository = Bootstrap::getObjectManager ()->create (
4353 \Magento \Catalog \Api \ProductRepositoryInterface::class
4454 );
4555 }
@@ -54,7 +64,7 @@ public function testAddPriceDataOnSchedule()
5464 $ this ->processor ->getIndexer ()->setScheduled (true );
5565 $ this ->assertTrue ($ this ->processor ->getIndexer ()->isScheduled ());
5666
57- $ productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
67+ $ productRepository = Bootstrap::getObjectManager ()
5868 ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
5969 /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
6070 $ product = $ productRepository ->get ('simple ' );
@@ -73,7 +83,7 @@ public function testAddPriceDataOnSchedule()
7383 //reindexing
7484 $ this ->processor ->getIndexer ()->reindexList ([1 ]);
7585
76- $ this ->collection = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
86+ $ this ->collection = Bootstrap::getObjectManager ()->create (
7787 \Magento \Catalog \Model \ResourceModel \Product \Collection::class
7888 );
7989 $ this ->collection ->addPriceData (0 , 1 );
@@ -89,6 +99,69 @@ public function testAddPriceDataOnSchedule()
8999 $ this ->processor ->getIndexer ()->setScheduled (false );
90100 }
91101
102+ /**
103+ * @magentoDataFixture Magento/Catalog/_files/products.php
104+ * @magentoAppIsolation enabled
105+ * @magentoDbIsolation disabled
106+ */
107+ public function testSetVisibility ()
108+ {
109+ $ appState = Bootstrap::getObjectManager ()
110+ ->create (State::class);
111+ $ appState ->setAreaCode (Area::AREA_CRONTAB );
112+ $ this ->collection ->setStoreId (Store::DEFAULT_STORE_ID );
113+ $ this ->collection ->setVisibility ([Visibility::VISIBILITY_BOTH ]);
114+ $ this ->collection ->load ();
115+ /** @var \Magento\Catalog\Api\Data\ProductInterface[] $product */
116+ $ items = $ this ->collection ->getItems ();
117+ $ this ->assertCount (2 , $ items );
118+ }
119+
120+ /**
121+ * @magentoDataFixture Magento/Catalog/_files/category_product.php
122+ * @magentoAppIsolation enabled
123+ * @magentoDbIsolation disabled
124+ */
125+ public function testSetCategoryWithStoreFilter ()
126+ {
127+ $ appState = Bootstrap::getObjectManager ()
128+ ->create (State::class);
129+ $ appState ->setAreaCode (Area::AREA_CRONTAB );
130+
131+ $ category = \Magento \Framework \App \ObjectManager::getInstance ()->get (
132+ \Magento \Catalog \Model \Category::class
133+ )->load (333 );
134+ $ this ->collection ->addCategoryFilter ($ category )->addStoreFilter (1 );
135+ $ this ->collection ->load ();
136+
137+ $ collectionStoreFilterAfter = Bootstrap::getObjectManager ()->create (
138+ \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory::class
139+ )->create ();
140+ $ collectionStoreFilterAfter ->addStoreFilter (1 )->addCategoryFilter ($ category );
141+ $ collectionStoreFilterAfter ->load ();
142+ $ this ->assertEquals ($ this ->collection ->getItems (), $ collectionStoreFilterAfter ->getItems ());
143+ $ this ->assertCount (1 , $ collectionStoreFilterAfter ->getItems ());
144+ }
145+
146+ /**
147+ * @magentoDataFixture Magento/Catalog/_files/categories.php
148+ * @magentoAppIsolation enabled
149+ * @magentoDbIsolation disabled
150+ */
151+ public function testSetCategoryFilter ()
152+ {
153+ $ appState = Bootstrap::getObjectManager ()
154+ ->create (State::class);
155+ $ appState ->setAreaCode (Area::AREA_CRONTAB );
156+
157+ $ category = \Magento \Framework \App \ObjectManager::getInstance ()->get (
158+ \Magento \Catalog \Model \Category::class
159+ )->load (3 );
160+ $ this ->collection ->addCategoryFilter ($ category );
161+ $ this ->collection ->load ();
162+ $ this ->assertEquals ($ this ->collection ->getSize (), 3 );
163+ }
164+
92165 /**
93166 * @magentoDataFixture Magento/Catalog/_files/products.php
94167 * @magentoAppIsolation enabled
@@ -98,7 +171,7 @@ public function testAddPriceDataOnSave()
98171 {
99172 $ this ->processor ->getIndexer ()->setScheduled (false );
100173 $ this ->assertFalse ($ this ->processor ->getIndexer ()->isScheduled ());
101- $ productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
174+ $ productRepository = Bootstrap::getObjectManager ()
102175 ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
103176 /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
104177 $ product = $ productRepository ->get ('simple ' );
@@ -184,7 +257,7 @@ public function testJoinTable()
184257 $ productTable = $ this ->collection ->getTable ('catalog_product_entity ' );
185258 $ urlRewriteTable = $ this ->collection ->getTable ('url_rewrite ' );
186259
187- // phpcs:ignore
260+ // phpcs:ignore Magento2.SQL.RawQuery
188261 $ expected = 'SELECT `e`.*, `alias`.`request_path` FROM ` ' . $ productTable . '` AS `e` '
189262 . ' LEFT JOIN ` ' . $ urlRewriteTable . '` AS `alias` ON (alias.entity_id =e.entity_id) '
190263 . ' AND (alias.entity_type = \'product \') ' ;
0 commit comments