|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Bundle\Test\Unit\Model\ResourceModel\Indexer; |
| 9 | + |
| 10 | +use Magento\Bundle\Model\ResourceModel\Indexer\Price; |
| 11 | +use Magento\Catalog\Model\Indexer\Product\Price\TableMaintainer; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BasePriceModifier; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructureFactory; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\Query\JoinAttributeProcessor; |
| 15 | +use Magento\Framework\App\ResourceConnection; |
| 16 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 17 | +use Magento\Framework\EntityManager\MetadataPool; |
| 18 | +use Magento\Framework\Event\ManagerInterface; |
| 19 | +use Magento\Framework\Module\Manager; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class to test Bundle products Price indexer resource model |
| 25 | + */ |
| 26 | +class PriceTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private $connectionName = 'test_connection'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ResourceConnection|MockObject |
| 35 | + */ |
| 36 | + private $resourceMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var AdapterInterface|MockObject |
| 40 | + */ |
| 41 | + private $connectionMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Price |
| 45 | + */ |
| 46 | + private $priceModel; |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function setUp(): void |
| 52 | + { |
| 53 | + parent::setUp(); |
| 54 | + |
| 55 | + $this->connectionMock = $this->createMock(AdapterInterface::class); |
| 56 | + $this->resourceMock = $this->createMock(ResourceConnection::class); |
| 57 | + $this->resourceMock->method('getConnection') |
| 58 | + ->with($this->connectionName) |
| 59 | + ->willReturn($this->connectionMock); |
| 60 | + $this->resourceMock->method('getTableName')->willReturnArgument(0); |
| 61 | + |
| 62 | + /** @var IndexTableStructureFactory|MockObject $indexTableStructureFactory */ |
| 63 | + $indexTableStructureFactory = $this->createMock(IndexTableStructureFactory::class); |
| 64 | + /** @var TableMaintainer|MockObject $tableMaintainer */ |
| 65 | + $tableMaintainer = $this->createMock(TableMaintainer::class); |
| 66 | + /** @var MetadataPool|MockObject $metadataPool */ |
| 67 | + $metadataPool = $this->createMock(MetadataPool::class); |
| 68 | + /** @var BasePriceModifier|MockObject $basePriceModifier */ |
| 69 | + $basePriceModifier = $this->createMock(BasePriceModifier::class); |
| 70 | + /** @var JoinAttributeProcessor|MockObject $joinAttributeProcessor */ |
| 71 | + $joinAttributeProcessor = $this->createMock(JoinAttributeProcessor::class); |
| 72 | + /** @var ManagerInterface|MockObject $eventManager */ |
| 73 | + $eventManager = $this->createMock(ManagerInterface::class); |
| 74 | + /** @var Manager|MockObject $moduleManager */ |
| 75 | + $moduleManager = $this->createMock(Manager::class); |
| 76 | + $fullReindexAction = false; |
| 77 | + |
| 78 | + $this->priceModel = new Price( |
| 79 | + $indexTableStructureFactory, |
| 80 | + $tableMaintainer, |
| 81 | + $metadataPool, |
| 82 | + $this->resourceMock, |
| 83 | + $basePriceModifier, |
| 84 | + $joinAttributeProcessor, |
| 85 | + $eventManager, |
| 86 | + $moduleManager, |
| 87 | + $fullReindexAction, |
| 88 | + $this->connectionName |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Tests create Bundle Price temporary table |
| 94 | + */ |
| 95 | + public function testGetBundlePriceTable(): void |
| 96 | + { |
| 97 | + $expectedTmpTableName = 'catalog_product_index_price_bundle_temp'; |
| 98 | + $expectedTableName = 'catalog_product_index_price_bundle_tmp'; |
| 99 | + |
| 100 | + $this->connectionMock->expects($this->once()) |
| 101 | + ->method('createTemporaryTableLike') |
| 102 | + ->with($expectedTmpTableName, $expectedTableName, true); |
| 103 | + |
| 104 | + $this->assertEquals( |
| 105 | + $expectedTmpTableName, |
| 106 | + $this->invokeMethodViaReflection('getBundlePriceTable') |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Tests create Bundle Selection Prices Index temporary table |
| 112 | + */ |
| 113 | + public function testGetBundleSelectionTable(): void |
| 114 | + { |
| 115 | + $expectedTmpTableName = 'catalog_product_index_price_bundle_sel_temp'; |
| 116 | + $expectedTableName = 'catalog_product_index_price_bundle_sel_tmp'; |
| 117 | + |
| 118 | + $this->connectionMock->expects($this->once()) |
| 119 | + ->method('createTemporaryTableLike') |
| 120 | + ->with($expectedTmpTableName, $expectedTableName, true); |
| 121 | + |
| 122 | + $this->assertEquals( |
| 123 | + $expectedTmpTableName, |
| 124 | + $this->invokeMethodViaReflection('getBundleSelectionTable') |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Tests create Bundle Option Prices Index temporary table |
| 130 | + */ |
| 131 | + public function testGetBundleOptionTable(): void |
| 132 | + { |
| 133 | + $expectedTmpTableName = 'catalog_product_index_price_bundle_opt_temp'; |
| 134 | + $expectedTableName = 'catalog_product_index_price_bundle_opt_tmp'; |
| 135 | + |
| 136 | + $this->connectionMock->expects($this->once()) |
| 137 | + ->method('createTemporaryTableLike') |
| 138 | + ->with($expectedTmpTableName, $expectedTableName, true); |
| 139 | + |
| 140 | + $this->assertEquals( |
| 141 | + $expectedTmpTableName, |
| 142 | + $this->invokeMethodViaReflection('getBundleOptionTable') |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Invoke private method via reflection |
| 148 | + * |
| 149 | + * @param string $methodName |
| 150 | + * @return string |
| 151 | + */ |
| 152 | + private function invokeMethodViaReflection(string $methodName): string |
| 153 | + { |
| 154 | + $method = new \ReflectionMethod( |
| 155 | + Price::class, |
| 156 | + $methodName |
| 157 | + ); |
| 158 | + $method->setAccessible(true); |
| 159 | + |
| 160 | + return (string)$method->invoke($this->priceModel); |
| 161 | + } |
| 162 | +} |
0 commit comments