Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerBuilderTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class IndexerBuilderTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $indexerBuilder;
16 
20  protected $resourceRule;
21 
25  protected $product;
26 
30  protected $productSecond;
31 
35  protected $productThird;
36 
37  protected function setUp()
38  {
39  $this->indexerBuilder = Bootstrap::getObjectManager()->get(
40  \Magento\CatalogRule\Model\Indexer\IndexBuilder::class
41  );
42  $this->resourceRule = Bootstrap::getObjectManager()->get(\Magento\CatalogRule\Model\ResourceModel\Rule::class);
43  $this->product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Product::class);
44  }
45 
46  protected function tearDown()
47  {
50  ->get(\Magento\Framework\Registry::class);
51 
52  $registry->unregister('isSecureArea');
53  $registry->register('isSecureArea', true);
54 
57  \Magento\Catalog\Model\ResourceModel\Product\Collection::class
58  );
59  $productCollection->delete();
60 
61  $registry->unregister('isSecureArea');
62  $registry->register('isSecureArea', false);
63 
64  parent::tearDown();
65  }
66 
74  public function testReindexById()
75  {
76  $product = $this->product->loadByAttribute('sku', 'simple');
77  $product->load($product->getId());
78  $product->setData('test_attribute', 'test_attribute_value')->save();
79 
80  $this->indexerBuilder->reindexById($product->getId());
81 
82  $this->assertEquals(9.8, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $product->getId()));
83  }
84 
92  public function testReindexByIds()
93  {
94  $this->prepareProducts();
95 
96  $this->indexerBuilder->reindexByIds(
97  [
98  $this->product->getId(),
99  $this->productSecond->getId(),
100  $this->productThird->getId(),
101  ]
102  );
103 
104  $this->assertEquals(9.8, $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->product->getId()));
105  $this->assertEquals(
106  9.8,
107  $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productSecond->getId())
108  );
109  $this->assertFalse($this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productThird->getId()));
110  }
111 
119  public function testReindexFull()
120  {
121  $this->prepareProducts();
122 
123  $this->indexerBuilder->reindexFull();
124 
125  $rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->product->getId());
126  $this->assertEquals(9.8, $rulePrice);
127  $rulePrice = $this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productSecond->getId());
128  $this->assertEquals(9.8, $rulePrice);
129  $this->assertFalse($this->resourceRule->getRulePrice(new \DateTime(), 1, 1, $this->productThird->getId()));
130  }
131 
132  protected function prepareProducts()
133  {
134  $product = $this->product->loadByAttribute('sku', 'simple');
135  $product->load($product->getId());
136  $this->product = $product;
137 
138  $this->product->setStoreId(0)->setData('test_attribute', 'test_attribute_value')->save();
139  $this->productSecond = clone $this->product;
140  $this->productSecond->setId(null)->setUrlKey('product-second')->save();
141  $this->productThird = clone $this->product;
142  $this->productThird->setId(null)
143  ->setUrlKey('product-third')
144  ->setData('test_attribute', 'NO_test_attribute_value')
145  ->save();
146  }
147 }