|
| 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 | + |
| 99 | + public function testCreateTablesForStoreCreatesMainAndReplicaWhenMissing(): void |
| 100 | + { |
| 101 | + $storeId = 2; |
| 102 | + $resolvedMain = 'index_2'; |
| 103 | + $baseReplica = 'pref_' . AbstractAction::MAIN_INDEX_TABLE . '_replica'; |
| 104 | + |
| 105 | + $this->tableResolver->expects($this->any()) |
| 106 | + ->method('resolve') |
| 107 | + ->willReturn($resolvedMain); |
| 108 | + |
| 109 | + $expectedIsTableArgs = [$resolvedMain, $resolvedMain . '_replica']; |
| 110 | + $this->adapter->expects($this->exactly(2)) |
| 111 | + ->method('isTableExists') |
| 112 | + ->willReturnCallback(function ($arg) use (&$expectedIsTableArgs) { |
| 113 | + $expected = array_shift($expectedIsTableArgs); |
| 114 | + \PHPUnit\Framework\Assert::assertSame($expected, $arg); |
| 115 | + return false; |
| 116 | + }); |
| 117 | + |
| 118 | + $ddlMain = $this->createMock(DdlTable::class); |
| 119 | + $ddlReplica = $this->createMock(DdlTable::class); |
| 120 | + |
| 121 | + $createByDdlCall = 0; |
| 122 | + $this->adapter->expects($this->exactly(2)) |
| 123 | + ->method('createTableByDdl') |
| 124 | + ->willReturnCallback(function ( |
| 125 | + $base, |
| 126 | + $new |
| 127 | + ) use ( |
| 128 | + $baseReplica, |
| 129 | + $resolvedMain, |
| 130 | + $ddlMain, |
| 131 | + $ddlReplica, |
| 132 | + &$createByDdlCall |
| 133 | + ) { |
| 134 | + if ($createByDdlCall === 0) { |
| 135 | + \PHPUnit\Framework\Assert::assertSame($baseReplica, $base); |
| 136 | + \PHPUnit\Framework\Assert::assertSame($resolvedMain, $new); |
| 137 | + $createByDdlCall++; |
| 138 | + return $ddlMain; |
| 139 | + } |
| 140 | + \PHPUnit\Framework\Assert::assertSame($baseReplica, $base); |
| 141 | + \PHPUnit\Framework\Assert::assertSame($resolvedMain . '_replica', $new); |
| 142 | + $createByDdlCall++; |
| 143 | + return $ddlReplica; |
| 144 | + }); |
| 145 | + |
| 146 | + $expectedCreateArgs = [$ddlMain, $ddlReplica]; |
| 147 | + $this->adapter->expects($this->exactly(2)) |
| 148 | + ->method('createTable') |
| 149 | + ->willReturnCallback(function ($ddl) use (&$expectedCreateArgs) { |
| 150 | + $expected = array_shift($expectedCreateArgs); |
| 151 | + \PHPUnit\Framework\Assert::assertSame($expected, $ddl); |
| 152 | + return null; |
| 153 | + }); |
| 154 | + |
| 155 | + $this->maintainer->createTablesForStore($storeId); |
| 156 | + } |
| 157 | + |
| 158 | + public function testDropTablesForStoreDropsWhenExists(): void |
| 159 | + { |
| 160 | + $storeId = 4; |
| 161 | + $resolvedMain = 'index_4'; |
| 162 | + |
| 163 | + $this->tableResolver->expects($this->any()) |
| 164 | + ->method('resolve') |
| 165 | + ->willReturn($resolvedMain); |
| 166 | + |
| 167 | + $expectedIsTableArgs = [$resolvedMain, $resolvedMain . '_replica']; |
| 168 | + $this->adapter->expects($this->exactly(2)) |
| 169 | + ->method('isTableExists') |
| 170 | + ->willReturnCallback(function ($arg) use (&$expectedIsTableArgs) { |
| 171 | + $expected = array_shift($expectedIsTableArgs); |
| 172 | + \PHPUnit\Framework\Assert::assertSame($expected, $arg); |
| 173 | + return true; |
| 174 | + }); |
| 175 | + |
| 176 | + $expectedDropArgs = [$resolvedMain, $resolvedMain . '_replica']; |
| 177 | + $this->adapter->expects($this->exactly(2)) |
| 178 | + ->method('dropTable') |
| 179 | + ->willReturnCallback(function ($arg) use (&$expectedDropArgs) { |
| 180 | + $expected = array_shift($expectedDropArgs); |
| 181 | + \PHPUnit\Framework\Assert::assertSame($expected, $arg); |
| 182 | + return null; |
| 183 | + }); |
| 184 | + |
| 185 | + $this->maintainer->dropTablesForStore($storeId); |
| 186 | + } |
| 187 | +} |
0 commit comments