|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brainbits\FunctionalTestHelpers\Tests\Schema; |
| 6 | + |
| 7 | +use ArrayObject; |
| 8 | +use Brainbits\FunctionalTestHelpers\Schema\MysqlBasedApplySchema; |
| 9 | +use Brainbits\FunctionalTestHelpers\Schema\SchemaBuilder; |
| 10 | +use Brainbits\FunctionalTestHelpers\Snapshot\SnapshotTrait; |
| 11 | +use Doctrine\DBAL\Cache\ArrayResult; |
| 12 | +use Doctrine\DBAL\Connection; |
| 13 | +use Doctrine\DBAL\Platforms\MySQLPlatform; |
| 14 | +use Doctrine\DBAL\Result; |
| 15 | +use Doctrine\DBAL\Schema\Schema; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +use function func_get_arg; |
| 19 | +use function Safe\preg_match; |
| 20 | +use function str_starts_with; |
| 21 | + |
| 22 | +/** @covers \Brainbits\FunctionalTestHelpers\Schema\MysqlBasedApplySchema */ |
| 23 | +final class MysqlBasedApplySchemaTest extends TestCase |
| 24 | +{ |
| 25 | + use SnapshotTrait; |
| 26 | + |
| 27 | + private MySQLPlatform $platform; |
| 28 | + private SchemaBuilder $schemaBuilder; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->platform = new MySQLPlatform(); |
| 33 | + |
| 34 | + $this->schemaBuilder = $this->createSchemaBuilder(); |
| 35 | + $this->schemaBuilder->foo(); |
| 36 | + } |
| 37 | + |
| 38 | + public function testApplySchema(): void |
| 39 | + { |
| 40 | + /** @phpstan-var ArrayObject<string, mixed[]> $queryLog */ |
| 41 | + $queryLog = new ArrayObject(); |
| 42 | + |
| 43 | + $connection = $this->createMock(Connection::class); |
| 44 | + $connection->expects($this->any()) |
| 45 | + ->method('quoteIdentifier') |
| 46 | + ->willReturnCallback($this->platform->quoteIdentifier(...)); |
| 47 | + $connection->expects($this->any()) |
| 48 | + ->method('getParams') |
| 49 | + ->willReturn(['driver' => 'pdo_mysql']); |
| 50 | + $connection->expects($this->any()) |
| 51 | + ->method('getDatabasePlatform') |
| 52 | + ->willReturn($this->platform); |
| 53 | + $connection->expects($this->any()) |
| 54 | + ->method('executeStatement') |
| 55 | + ->willReturnCallback( |
| 56 | + static function () use ($queryLog): void { |
| 57 | + $queryLog[] = ['statement' => func_get_arg(0)]; |
| 58 | + }, |
| 59 | + ); |
| 60 | + $connection->expects($this->any()) |
| 61 | + ->method('executeQuery') |
| 62 | + ->willReturnCallback( |
| 63 | + static function () use ($queryLog, $connection) { |
| 64 | + $query = func_get_arg(0); |
| 65 | + $result = []; |
| 66 | + |
| 67 | + $queryLog[] = ['query' => $query, 'result' => $result]; |
| 68 | + |
| 69 | + return new Result(new ArrayResult($result), $connection); |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + $applySchema = new MysqlBasedApplySchema(resetExecutedStatements: true); |
| 74 | + $applySchema($this->schemaBuilder, $connection); |
| 75 | + |
| 76 | + $this->assertMatchesArraySnapshot($queryLog->getArrayCopy()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void |
| 80 | + { |
| 81 | + /** @phpstan-var ArrayObject<string, mixed[]> $queryLog */ |
| 82 | + $queryLog = new ArrayObject(); |
| 83 | + |
| 84 | + $connection = $this->createMock(Connection::class); |
| 85 | + $connection->expects($this->any()) |
| 86 | + ->method('quoteIdentifier') |
| 87 | + ->willReturnCallback($this->platform->quoteIdentifier(...)); |
| 88 | + $connection->expects($this->any()) |
| 89 | + ->method('getParams') |
| 90 | + ->willReturn(['driver' => 'pdo_mysql']); |
| 91 | + $connection->expects($this->any()) |
| 92 | + ->method('getDatabasePlatform') |
| 93 | + ->willReturn($this->platform); |
| 94 | + $connection->expects($this->any()) |
| 95 | + ->method('executeStatement') |
| 96 | + ->willReturnCallback( |
| 97 | + static function () use ($queryLog): void { |
| 98 | + $queryLog[] = ['statement' => func_get_arg(0)]; |
| 99 | + }, |
| 100 | + ); |
| 101 | + $connection->expects($this->any()) |
| 102 | + ->method('executeQuery') |
| 103 | + ->willReturnCallback( |
| 104 | + static function () use ($queryLog, $connection) { |
| 105 | + $query = func_get_arg(0); |
| 106 | + |
| 107 | + $result = []; |
| 108 | + if (str_starts_with($query, 'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'')) { |
| 109 | + // two old tables exists |
| 110 | + $result = [['name' => 'old_table_1'], ['name' => 'old_table_2']]; |
| 111 | + } elseif (preg_match('/SELECT.*CONSTRAINT_NAME.*old_table_1/', $query)) { |
| 112 | + // "old_table_1" has two constraints |
| 113 | + $result = [['name' => 'constraint_1'], ['name' => 'constraint_2']]; |
| 114 | + } |
| 115 | + |
| 116 | + $queryLog[] = ['query' => $query, 'result' => $result]; |
| 117 | + |
| 118 | + return new Result(new ArrayResult($result), $connection); |
| 119 | + }, |
| 120 | + ); |
| 121 | + |
| 122 | + $applySchema = new MysqlBasedApplySchema(resetExecutedStatements: true); |
| 123 | + $applySchema($this->schemaBuilder, $connection); |
| 124 | + |
| 125 | + $applySchema = new MysqlBasedApplySchema(resetExecutedStatements: false); |
| 126 | + |
| 127 | + $this->assertMatchesArraySnapshot($queryLog->getArrayCopy()); |
| 128 | + } |
| 129 | + |
| 130 | + public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void |
| 131 | + { |
| 132 | + /** @phpstan-var ArrayObject<string, mixed[]> $queryLog */ |
| 133 | + $queryLog = new ArrayObject(); |
| 134 | + |
| 135 | + $connection = $this->createMock(Connection::class); |
| 136 | + $connection->expects($this->any()) |
| 137 | + ->method('quoteIdentifier') |
| 138 | + ->willReturnCallback($this->platform->quoteIdentifier(...)); |
| 139 | + $connection->expects($this->any()) |
| 140 | + ->method('getParams') |
| 141 | + ->willReturn(['driver' => 'pdo_mysql']); |
| 142 | + $connection->expects($this->any()) |
| 143 | + ->method('getDatabasePlatform') |
| 144 | + ->willReturn(new MySQLPlatform()); |
| 145 | + $connection->expects($this->any()) |
| 146 | + ->method('executeStatement') |
| 147 | + ->willReturnCallback( |
| 148 | + static function () use ($queryLog): void { |
| 149 | + $queryLog[] = ['statement' => func_get_arg(0)]; |
| 150 | + }, |
| 151 | + ); |
| 152 | + $connection->expects($this->any()) |
| 153 | + ->method('executeQuery') |
| 154 | + ->willReturnCallback( |
| 155 | + static function () use ($queryLog, $connection) { |
| 156 | + $query = func_get_arg(0); |
| 157 | + $result = []; |
| 158 | + |
| 159 | + $queryLog[] = ['query' => $query, 'result' => $result]; |
| 160 | + |
| 161 | + return new Result(new ArrayResult($result), $connection); |
| 162 | + }, |
| 163 | + ); |
| 164 | + |
| 165 | + $applySchema = new MysqlBasedApplySchema(resetExecutedStatements: true); |
| 166 | + $applySchema($this->schemaBuilder, $connection); |
| 167 | + |
| 168 | + $applySchema = new MysqlBasedApplySchema(resetExecutedStatements: false); |
| 169 | + $applySchema($this->schemaBuilder, $connection); |
| 170 | + |
| 171 | + $this->assertMatchesArraySnapshot($queryLog->getArrayCopy()); |
| 172 | + } |
| 173 | + |
| 174 | + private function createSchemaBuilder(): SchemaBuilder |
| 175 | + { |
| 176 | + return new class implements SchemaBuilder { |
| 177 | + private Schema $schema; |
| 178 | + |
| 179 | + public function __construct() |
| 180 | + { |
| 181 | + $this->schema = new Schema(); |
| 182 | + } |
| 183 | + |
| 184 | + public static function create(): SchemaBuilder |
| 185 | + { |
| 186 | + return new self(); |
| 187 | + } |
| 188 | + |
| 189 | + public function getSchema(): Schema |
| 190 | + { |
| 191 | + return $this->schema; |
| 192 | + } |
| 193 | + |
| 194 | + public function foo(): void |
| 195 | + { |
| 196 | + $table = $this->schema->createTable('foo'); |
| 197 | + $table->addColumn('bar', 'string'); |
| 198 | + } |
| 199 | + }; |
| 200 | + } |
| 201 | + |
| 202 | + private function snapshotPath(): string |
| 203 | + { |
| 204 | + return __DIR__; |
| 205 | + } |
| 206 | +} |
0 commit comments