3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Model \ResourceModel \Product ;
7
9
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
+
8
16
/**
9
17
* Collection test
18
+ *
19
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10
20
*/
11
21
class CollectionTest extends \PHPUnit \Framework \TestCase
12
22
{
@@ -31,15 +41,15 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
31
41
*/
32
42
protected function setUp ()
33
43
{
34
- $ this ->collection = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
44
+ $ this ->collection = Bootstrap::getObjectManager ()->create (
35
45
\Magento \Catalog \Model \ResourceModel \Product \Collection::class
36
46
);
37
47
38
- $ this ->processor = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
48
+ $ this ->processor = Bootstrap::getObjectManager ()->create (
39
49
\Magento \Catalog \Model \Indexer \Product \Price \Processor::class
40
50
);
41
51
42
- $ this ->productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
52
+ $ this ->productRepository = Bootstrap::getObjectManager ()->create (
43
53
\Magento \Catalog \Api \ProductRepositoryInterface::class
44
54
);
45
55
}
@@ -54,7 +64,7 @@ public function testAddPriceDataOnSchedule()
54
64
$ this ->processor ->getIndexer ()->setScheduled (true );
55
65
$ this ->assertTrue ($ this ->processor ->getIndexer ()->isScheduled ());
56
66
57
- $ productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
67
+ $ productRepository = Bootstrap::getObjectManager ()
58
68
->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
59
69
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
60
70
$ product = $ productRepository ->get ('simple ' );
@@ -73,7 +83,7 @@ public function testAddPriceDataOnSchedule()
73
83
//reindexing
74
84
$ this ->processor ->getIndexer ()->reindexList ([1 ]);
75
85
76
- $ this ->collection = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
86
+ $ this ->collection = Bootstrap::getObjectManager ()->create (
77
87
\Magento \Catalog \Model \ResourceModel \Product \Collection::class
78
88
);
79
89
$ this ->collection ->addPriceData (0 , 1 );
@@ -89,6 +99,69 @@ public function testAddPriceDataOnSchedule()
89
99
$ this ->processor ->getIndexer ()->setScheduled (false );
90
100
}
91
101
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
+
92
165
/**
93
166
* @magentoDataFixture Magento/Catalog/_files/products.php
94
167
* @magentoAppIsolation enabled
@@ -98,7 +171,7 @@ public function testAddPriceDataOnSave()
98
171
{
99
172
$ this ->processor ->getIndexer ()->setScheduled (false );
100
173
$ this ->assertFalse ($ this ->processor ->getIndexer ()->isScheduled ());
101
- $ productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
174
+ $ productRepository = Bootstrap::getObjectManager ()
102
175
->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
103
176
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
104
177
$ product = $ productRepository ->get ('simple ' );
@@ -184,7 +257,7 @@ public function testJoinTable()
184
257
$ productTable = $ this ->collection ->getTable ('catalog_product_entity ' );
185
258
$ urlRewriteTable = $ this ->collection ->getTable ('url_rewrite ' );
186
259
187
- // phpcs:ignore
260
+ // phpcs:ignore Magento2.SQL.RawQuery
188
261
$ expected = 'SELECT `e`.*, `alias`.`request_path` FROM ` ' . $ productTable . '` AS `e` '
189
262
. ' LEFT JOIN ` ' . $ urlRewriteTable . '` AS `alias` ON (alias.entity_id =e.entity_id) '
190
263
. ' AND (alias.entity_type = \'product \') ' ;
0 commit comments