|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Model\Indexer\Product\Flat\Action; |
| 10 | + |
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | +use Magento\TestFramework\Catalog\Model\Indexer\Product\Flat\Action\Full as FlatIndexerFull; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test relation customization |
| 18 | + */ |
| 19 | +class RelationTest extends \Magento\TestFramework\Indexer\TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var FlatIndexerFull |
| 23 | + */ |
| 24 | + private $indexer; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var StoreManagerInterface |
| 28 | + */ |
| 29 | + private $storeManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var AdapterInterface |
| 33 | + */ |
| 34 | + private $connection; |
| 35 | + |
| 36 | + /** |
| 37 | + * Updated flat tables |
| 38 | + * |
| 39 | + * @var array |
| 40 | + */ |
| 41 | + private $flatUpdated = []; |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritdoc |
| 45 | + */ |
| 46 | + protected function setUp() |
| 47 | + { |
| 48 | + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 49 | + |
| 50 | + $tableBuilderMock = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder::class); |
| 51 | + $flatTableBuilderMock = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder::class); |
| 52 | + |
| 53 | + $productIndexerHelper = $objectManager->create( |
| 54 | + \Magento\Catalog\Helper\Product\Flat\Indexer::class, |
| 55 | + ['addChildData' => 1] |
| 56 | + ); |
| 57 | + $this->indexer = $objectManager->create( |
| 58 | + FlatIndexerFull::class, |
| 59 | + [ |
| 60 | + 'productHelper' => $productIndexerHelper, |
| 61 | + 'tableBuilder' => $tableBuilderMock, |
| 62 | + 'flatTableBuilder' => $flatTableBuilderMock |
| 63 | + ] |
| 64 | + ); |
| 65 | + $this->storeManager = $objectManager->create(StoreManagerInterface::class); |
| 66 | + $this->connection = $objectManager->get(ResourceConnection::class)->getConnection(); |
| 67 | + |
| 68 | + foreach ($this->storeManager->getStores() as $store) { |
| 69 | + $flatTable = $productIndexerHelper->getFlatTableName($store->getId()); |
| 70 | + if ($this->connection->isTableExists($flatTable) && |
| 71 | + !$this->connection->tableColumnExists($flatTable, 'child_id') && |
| 72 | + !$this->connection->tableColumnExists($flatTable, 'is_child') |
| 73 | + ) { |
| 74 | + $this->connection->addColumn( |
| 75 | + $flatTable, |
| 76 | + 'child_id', |
| 77 | + [ |
| 78 | + 'type' => 'integer', |
| 79 | + 'length' => null, |
| 80 | + 'unsigned' => true, |
| 81 | + 'nullable' => true, |
| 82 | + 'default' => null, |
| 83 | + 'unique' => true, |
| 84 | + 'comment' => 'Child Id', |
| 85 | + ] |
| 86 | + ); |
| 87 | + $this->connection->addColumn( |
| 88 | + $flatTable, |
| 89 | + 'is_child', |
| 90 | + [ |
| 91 | + 'type' => 'smallint', |
| 92 | + 'length' => 1, |
| 93 | + 'unsigned' => true, |
| 94 | + 'nullable' => false, |
| 95 | + 'default' => '0', |
| 96 | + 'comment' => 'Checks If Entity Is Child', |
| 97 | + ] |
| 98 | + ); |
| 99 | + |
| 100 | + $this->flatUpdated[] = $flatTable; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @inheritdoc |
| 107 | + */ |
| 108 | + protected function tearDown() |
| 109 | + { |
| 110 | + foreach ($this->flatUpdated as $flatTable) { |
| 111 | + $this->connection->dropColumn($flatTable, 'child_id'); |
| 112 | + $this->connection->dropColumn($flatTable, 'is_child'); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test that SQL generated for relation customization is valid |
| 118 | + * |
| 119 | + * @return void |
| 120 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 121 | + * @throws \Exception |
| 122 | + */ |
| 123 | + public function testExecute() : void |
| 124 | + { |
| 125 | + try { |
| 126 | + $this->indexer->execute(); |
| 127 | + } catch (\Magento\Framework\Exception\LocalizedException $e) { |
| 128 | + if ($e->getPrevious() instanceof \Zend_Db_Statement_Exception) { |
| 129 | + $this->fail($e->getMessage()); |
| 130 | + } |
| 131 | + throw $e; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments