|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Framework\Setup\Test\Unit\Declaration\Schema\Db; |
| 9 | + |
| 10 | +use Magento\Framework\App\ResourceConnection; |
| 11 | +use Magento\Framework\DB\Adapter\SqlVersionProvider; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 13 | +use Magento\Framework\Setup\Declaration\Schema\Db\Statement; |
| 14 | +use Magento\Framework\Setup\Declaration\Schema\Db\StatementAggregator; |
| 15 | +use Magento\Framework\Setup\Declaration\Schema\Db\StatementFactory; |
| 16 | +use Magento\Framework\Setup\Declaration\Schema\DryRunLogger; |
| 17 | +use Magento\Framework\Setup\Declaration\Schema\Db\MySQL\DbSchemaWriter; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class DbSchemaWriterTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ResourceConnection|MockObject |
| 25 | + */ |
| 26 | + private $resourceConnection; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var StatementFactory|MockObject |
| 30 | + */ |
| 31 | + private $statementFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var DryRunLogger|MockObject |
| 35 | + */ |
| 36 | + private $dryRunLogger; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var SqlVersionProvider|MockObject |
| 40 | + */ |
| 41 | + private $sqlVersionProvider; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var AdapterInterface|MockObject |
| 45 | + */ |
| 46 | + private $adapter; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var DbSchemaWriter |
| 50 | + */ |
| 51 | + private $model; |
| 52 | + |
| 53 | + protected function setUp(): void |
| 54 | + { |
| 55 | + $this->resourceConnection = $this->getMockBuilder(ResourceConnection::class) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->getMock(); |
| 58 | + $this->statementFactory = $this->getMockBuilder(StatementFactory::class) |
| 59 | + ->disableOriginalConstructor() |
| 60 | + ->getMock(); |
| 61 | + $this->dryRunLogger = $this->getMockBuilder(DryRunLogger::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + $this->sqlVersionProvider = $this->getMockBuilder(SqlVersionProvider::class) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->getMock(); |
| 67 | + |
| 68 | + $this->adapter = $this->getMockBuilder(AdapterInterface::class) |
| 69 | + ->getMockForAbstractClass(); |
| 70 | + $this->resourceConnection->expects($this->any()) |
| 71 | + ->method('getConnection') |
| 72 | + ->willReturn($this->adapter); |
| 73 | + |
| 74 | + $this->model = new DbSchemaWriter( |
| 75 | + $this->resourceConnection, |
| 76 | + $this->statementFactory, |
| 77 | + $this->dryRunLogger, |
| 78 | + $this->sqlVersionProvider |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test to check that column modification and adding fk are run as separate queries with MariaDb |
| 84 | + * |
| 85 | + * @param string $dbVersion |
| 86 | + * @param int $numberOfQueries |
| 87 | + * @return void |
| 88 | + * |
| 89 | + * @dataProvider compileDataProvider |
| 90 | + */ |
| 91 | + public function testCompileWithColumnModificationAndFK(string $dbVersion, int $numberOfQueries) : void |
| 92 | + { |
| 93 | + $dryRun = false; |
| 94 | + $statementAggregator = $this->getMockBuilder(StatementAggregator::class) |
| 95 | + ->disableOriginalConstructor() |
| 96 | + ->getMock(); |
| 97 | + $statement1 = $this->getMockBuilder(Statement::class) |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->getMock(); |
| 100 | + $statement1->expects($this->any()) |
| 101 | + ->method('getResource') |
| 102 | + ->willReturn('resource'); |
| 103 | + $statement1->expects($this->any()) |
| 104 | + ->method('getType') |
| 105 | + ->willReturn('alter'); |
| 106 | + $statement1->expects($this->any()) |
| 107 | + ->method('getName') |
| 108 | + ->willReturn('column'); |
| 109 | + $statement1->expects($this->any()) |
| 110 | + ->method('getStatement') |
| 111 | + ->willReturn('MODIFY COLUMN `column1` varchar(64) NOT NULL'); |
| 112 | + |
| 113 | + $statement2 = $this->getMockBuilder(Statement::class) |
| 114 | + ->disableOriginalConstructor() |
| 115 | + ->getMock(); |
| 116 | + $statement2->expects($this->any()) |
| 117 | + ->method('getResource') |
| 118 | + ->willReturn('resource'); |
| 119 | + $statement2->expects($this->any()) |
| 120 | + ->method('getType') |
| 121 | + ->willReturn('alter'); |
| 122 | + $statement2->expects($this->any()) |
| 123 | + ->method('getName') |
| 124 | + ->willReturn('FK_COLUMN'); |
| 125 | + $statement2->expects($this->any()) |
| 126 | + ->method('getStatement') |
| 127 | + ->willReturn('ADD CONSTRAINT `FK_COLUMN` FOREIGN KEY (`column`)'); |
| 128 | + |
| 129 | + $statementBank = [$statement1, $statement2]; |
| 130 | + $statementAggregator->expects($this->any()) |
| 131 | + ->method('getStatementsBank') |
| 132 | + ->willReturn([$statementBank]); |
| 133 | + $this->sqlVersionProvider->expects($this->once()) |
| 134 | + ->method('getSqlVersion') |
| 135 | + ->willReturn($dbVersion); |
| 136 | + $this->adapter->expects($this->exactly($numberOfQueries)) |
| 137 | + ->method('query'); |
| 138 | + |
| 139 | + $this->model->compile($statementAggregator, $dryRun); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @return array |
| 144 | + */ |
| 145 | + public static function compileDataProvider() : array |
| 146 | + { |
| 147 | + return [ |
| 148 | + [SqlVersionProvider::MARIA_DB_10_VERSION, 2], |
| 149 | + [SqlVersionProvider::MYSQL_8_0_VERSION, 1], |
| 150 | + ]; |
| 151 | + } |
| 152 | +} |
0 commit comments