‪TYPO3CMS  ‪main
ConnectionMigratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Platforms\MySQLPlatform;
21 use Doctrine\DBAL\Schema\Column;
22 use Doctrine\DBAL\Schema\Table;
23 use Doctrine\DBAL\Types\Type;
24 use PHPUnit\Framework\Attributes\Test;
25 use PHPUnit\Framework\MockObject\MockObject;
28 use TYPO3\CMS\Core\Database\Schema\ConnectionMigrator;
31 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
34 final class ‪ConnectionMigratorTest extends UnitTestCase
35 {
36  protected MySQLPlatform ‪$platform;
37  protected AccessibleObjectInterface&MockObject ‪$subject;
38  protected int ‪$maxIdentifierLength = -1;
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43 
44  $platformMock = $this->createMock(MySQLPlatform::class);
45  $platformMock->method('quoteIdentifier')->with(self::anything())->willReturnArgument(0);
46  $this->platform = $platformMock;
47 
48  $connectionMock = $this->createMock(Connection::class);
49  $connectionMock->method('getDatabasePlatform')->willReturn($this->platform);
50  $connectionMock->method('quoteIdentifier')->with(self::anything())->willReturnArgument(0);
51 
52  $this->maxIdentifierLength = ‪PlatformInformation::getMaxIdentifierLength($this->platform);
53 
54  $this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, ['Default', $connectionMock, []]);
55  }
56 
57  #[Test]
59  {
60  $originalSchemaDiff = new ‪SchemaDiff(
61  createdSchemas: [],
62  droppedSchemas: [],
63  createdTables: [],
64  alteredTables: [],
65  droppedTables: [$this->‪getTable()->getName() => $this->‪getTable()],
66  createdSequences: [],
67  alteredSequences: [],
68  droppedSequences: [],
69  );
71  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedTablesToRenames', $originalSchemaDiff);
72  $firstAlteredTableName = array_key_first($renamedSchemaDiff->getAlteredTables());
73  $firstAlteredTableNewName = $renamedSchemaDiff->getAlteredTables()[$firstAlteredTableName]->newName;
74 
75  self::assertStringStartsWith('zzz_deleted_', $firstAlteredTableNewName);
76  self::assertEquals($this->maxIdentifierLength, strlen($firstAlteredTableNewName));
77  }
78 
79  #[Test]
81  {
82  $table = $this->‪getTable();
83  $tableDiff = new ‪TableDiff(
84  oldTable: $table,
85  addedColumns: [],
86  modifiedColumns: [],
87  droppedColumns: [$this->‪getColumn()->getName() => $this->‪getColumn()],
88  renamedColumns: [],
89  addedIndexes: [],
90  modifiedIndexes: [],
91  droppedIndexes: [],
92  renamedIndexes: [],
93  addedForeignKeys: [],
94  modifiedForeignKeys: [],
95  droppedForeignKeys: [],
96  );
97  $originalSchemaDiff = new ‪SchemaDiff(
98  createdSchemas: [],
99  droppedSchemas: [],
100  createdTables: [],
101  alteredTables: [$tableDiff->getOldTable()->getName() => $tableDiff],
102  droppedTables: [],
103  createdSequences: [],
104  alteredSequences: [],
105  droppedSequences: [],
106  );
108  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedFieldsToRenames', $originalSchemaDiff);
109  $firstColumnName = array_key_first($renamedSchemaDiff->getAlteredTables()[$table->getName()]->getModifiedColumns());
110  $firstColumn = $renamedSchemaDiff->getAlteredTables()[$table->getName()]->getModifiedColumns()[$firstColumnName];
111 
112  self::assertStringStartsWith(
113  'zzz_deleted_',
114  $firstColumn->getNewColumn()->getName()
115  );
116  self::assertEquals(
117  $this->maxIdentifierLength,
118  strlen($firstColumn->getNewColumn()->getName())
119  );
120  }
121 
125  protected function ‪getTable(): Table
126  {
127  $tableName = 'table_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
128  return new Table($tableName);
129  }
130 
134  protected function ‪getColumn(): Column
135  {
136  $columnName = 'column_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
137  return new Column(
138  $columnName,
139  Type::getType('string')
140  );
141  }
142 }
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema
Definition: ConnectionMigratorTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$subject
‪AccessibleObjectInterface &MockObject $subject
Definition: ConnectionMigratorTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$platform
‪MySQLPlatform $platform
Definition: ConnectionMigratorTest.php:36
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation\getMaxIdentifierLength
‪static getMaxIdentifierLength(DoctrineAbstractPlatform $platform)
Definition: PlatformInformation.php:95
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$maxIdentifierLength
‪int $maxIdentifierLength
Definition: ConnectionMigratorTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest
Definition: ConnectionMigratorTest.php:35
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff
Definition: SchemaDiff.php:33
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\setUp
‪setUp()
Definition: ConnectionMigratorTest.php:40
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation
Definition: PlatformInformation.php:33
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getTable
‪getTable()
Definition: ConnectionMigratorTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getColumn
‪getColumn()
Definition: ConnectionMigratorTest.php:134
‪TYPO3\CMS\Core\Database\Schema\TableDiff
Definition: TableDiff.php:33