|
| 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\Plugin; |
| 9 | + |
| 10 | +use Magento\Bundle\Model\Plugin\ProductPriceIndexModifier; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableStructure; |
| 12 | +use Magento\CatalogInventory\Model\Indexer\ProductPriceIndexFilter; |
| 13 | +use Magento\Framework\App\ResourceConnection; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\DB\Select; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class ProductPriceIndexModifierTest extends TestCase |
| 20 | +{ |
| 21 | + private const CONNECTION_NAME = 'indexer'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var ResourceConnection|MockObject |
| 25 | + */ |
| 26 | + private ResourceConnection $resourceConnection; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ProductPriceIndexModifier |
| 30 | + */ |
| 31 | + private ProductPriceIndexModifier $plugin; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var IndexTableStructure|MockObject |
| 35 | + */ |
| 36 | + private IndexTableStructure $table; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ProductPriceIndexFilter|MockObject |
| 40 | + */ |
| 41 | + private ProductPriceIndexFilter $subject; |
| 42 | + |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + $this->table = $this->createMock(IndexTableStructure::class); |
| 46 | + $this->subject = $this->createMock(ProductPriceIndexFilter::class); |
| 47 | + $this->resourceConnection = $this->createMock(ResourceConnection::class); |
| 48 | + $this->plugin = new ProductPriceIndexModifier($this->resourceConnection, self::CONNECTION_NAME); |
| 49 | + } |
| 50 | + |
| 51 | + public function testAroundModifyPriceNoEntities(): void |
| 52 | + { |
| 53 | + $called = false; |
| 54 | + $callable = function () use (&$called) { |
| 55 | + $called = true; |
| 56 | + }; |
| 57 | + |
| 58 | + $this->plugin->aroundModifyPrice($this->subject, $callable, $this->table); |
| 59 | + $this->assertTrue($called); |
| 60 | + } |
| 61 | + |
| 62 | + public function testAroundModifyPriceFilteredEntities() |
| 63 | + { |
| 64 | + $priceTableName = 'catalog_product_index_price_temp'; |
| 65 | + $entities = [1, 2]; |
| 66 | + $this->table->expects($this->exactly(2)) |
| 67 | + ->method('getTableName') |
| 68 | + ->willReturn($priceTableName); |
| 69 | + $select = $this->createMock(Select::class); |
| 70 | + $select->expects($this->exactly(2)) |
| 71 | + ->method('from') |
| 72 | + ->with(['selection' => 'catalog_product_bundle_selection'], 'selection_id'); |
| 73 | + $select->expects($this->exactly(2)) |
| 74 | + ->method('joinInner') |
| 75 | + ->with(['price' => $priceTableName], |
| 76 | + implode(' AND ', ['price.entity_id = selection.product_id']), |
| 77 | + null); |
| 78 | + $select->expects($this->exactly(4)) |
| 79 | + ->method('where') |
| 80 | + ->withConsecutive( |
| 81 | + ['selection.product_id = ?', $entities[0]], |
| 82 | + ['price.tax_class_id = ?', \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC], |
| 83 | + ['selection.product_id = ?', $entities[1]], |
| 84 | + ['price.tax_class_id = ?', \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC] |
| 85 | + ); |
| 86 | + $connection = $this->createMock(AdapterInterface::class); |
| 87 | + $connection->expects($this->exactly(2)) |
| 88 | + ->method('select') |
| 89 | + ->willReturn($select); |
| 90 | + $connection->expects($this->exactly(2)) |
| 91 | + ->method('fetchOne') |
| 92 | + ->with($select) |
| 93 | + ->willReturn(null); |
| 94 | + $this->resourceConnection->expects($this->exactly(2)) |
| 95 | + ->method('getConnection') |
| 96 | + ->with(self::CONNECTION_NAME) |
| 97 | + ->willReturn($connection); |
| 98 | + |
| 99 | + $calledPriceTable = ''; |
| 100 | + $calledEntities = []; |
| 101 | + $callable = function () use (&$calledPriceTable, &$calledEntities, $priceTableName, $entities) { |
| 102 | + $calledPriceTable = $priceTableName; |
| 103 | + $calledEntities = $entities; |
| 104 | + }; |
| 105 | + $this->plugin->aroundModifyPrice($this->subject, $callable, $this->table, [1, 2]); |
| 106 | + $this->assertSame($calledPriceTable, $priceTableName); |
| 107 | + $this->assertSame($calledEntities, $entities); |
| 108 | + } |
| 109 | +} |
0 commit comments