|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Test\Unit\Model\Indexer\Category\Product; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Indexer\Category\Product\AbstractAction; |
| 12 | +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer; |
| 13 | +use Magento\Framework\App\ResourceConnection; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\DB\Ddl\Table as DdlTable; |
| 16 | +use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver as TableResolver; |
| 17 | +use Magento\Framework\Search\Request\Dimension; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class TableMaintainerTest extends TestCase |
| 22 | +{ |
| 23 | + /** @var ResourceConnection|MockObject */ |
| 24 | + private $resource; |
| 25 | + |
| 26 | + /** @var TableResolver|MockObject */ |
| 27 | + private $tableResolver; |
| 28 | + |
| 29 | + /** @var AdapterInterface|MockObject */ |
| 30 | + private $adapter; |
| 31 | + |
| 32 | + /** @var TableMaintainer */ |
| 33 | + private $maintainer; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->resource = $this->createMock(ResourceConnection::class); |
| 38 | + $this->tableResolver = $this->createMock(TableResolver::class); |
| 39 | + $this->adapter = $this->createMock(AdapterInterface::class); |
| 40 | + |
| 41 | + $this->resource->method('getConnection')->willReturn($this->adapter); |
| 42 | + $this->resource->method('getTableName')->willReturnCallback( |
| 43 | + static function (string $name) { |
| 44 | + return 'pref_' . $name; |
| 45 | + } |
| 46 | + ); |
| 47 | + |
| 48 | + $this->maintainer = new TableMaintainer($this->resource, $this->tableResolver); |
| 49 | + } |
| 50 | + |
| 51 | + public function testGetMainTableAndReplicaTableName(): void |
| 52 | + { |
| 53 | + $storeId = 3; |
| 54 | + $resolvedMain = 'catalog_category_product_index_store3'; |
| 55 | + |
| 56 | + $this->tableResolver->expects($this->atLeastOnce()) |
| 57 | + ->method('resolve') |
| 58 | + ->with(AbstractAction::MAIN_INDEX_TABLE, $this->callback(function (array $dims): bool { |
| 59 | + return isset($dims[0]) && $dims[0] instanceof Dimension; |
| 60 | + })) |
| 61 | + ->willReturn($resolvedMain); |
| 62 | + |
| 63 | + $this->assertSame($resolvedMain, $this->maintainer->getMainTable($storeId)); |
| 64 | + $this->assertSame($resolvedMain . '_replica', $this->maintainer->getMainReplicaTable($storeId)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testCreateMainTmpTableAndGetMainTmpTable(): void |
| 68 | + { |
| 69 | + $storeId = 7; |
| 70 | + $resolvedMain = 'index_7'; |
| 71 | + $tmpName = $resolvedMain . '_tmp'; |
| 72 | + |
| 73 | + $this->tableResolver->expects($this->any()) |
| 74 | + ->method('resolve') |
| 75 | + ->willReturn($resolvedMain); |
| 76 | + |
| 77 | + $this->adapter->expects($this->once()) |
| 78 | + ->method('createTemporaryTableLike') |
| 79 | + ->with($tmpName, $resolvedMain, true); |
| 80 | + |
| 81 | + // First call creates and caches temp table |
| 82 | + $this->maintainer->createMainTmpTable($storeId); |
| 83 | + // Second call should be a no-op (still once) |
| 84 | + $this->maintainer->createMainTmpTable($storeId); |
| 85 | + |
| 86 | + self::assertSame($tmpName, $this->maintainer->getMainTmpTable($storeId)); |
| 87 | + |
| 88 | + $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class); |
| 89 | + // Different store id should not have tmp table created |
| 90 | + $this->maintainer->getMainTmpTable($storeId + 1); |
| 91 | + } |
| 92 | + |
| 93 | + public function testGetSameAdapterConnectionReturnsSameInstance(): void |
| 94 | + { |
| 95 | + $same = $this->maintainer->getSameAdapterConnection(); |
| 96 | + $this->assertSame($this->adapter, $same); |
| 97 | + } |
| 98 | +} |
0 commit comments