‪TYPO3CMS  10.4
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\SchemaDiff;
23 use Doctrine\DBAL\Schema\Table;
24 use Doctrine\DBAL\Schema\TableDiff;
25 use Doctrine\DBAL\Types\Type;
26 use Prophecy\Argument;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
36 class ‪ConnectionMigratorTest extends UnitTestCase
37 {
41  protected ‪$platform;
42 
46  protected ‪$subject;
47 
51  protected ‪$maxIdentifierLength = -1;
52 
56  protected function ‪setUp(): void
57  {
58  parent::setUp();
59 
60  $platformMock = $this->prophesize(MySqlPlatform::class);
61  $platformMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
62  $this->platform = $platformMock->reveal();
63 
64  $connectionMock = $this->prophesize(Connection::class);
65  $connectionMock->getDatabasePlatform()->willReturn($this->platform);
66  $connectionMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
67 
68  $this->maxIdentifierLength = ‪PlatformInformation::getMaxIdentifierLength($this->platform);
69 
70  $this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, [], '', false);
71  $this->subject->_set('connection', $connectionMock->reveal());
72  }
73 
78  {
79  $originalSchemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, null, null, [$this->‪getTable()]);
80  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedTablesToRenames', $originalSchemaDiff);
81 
82  self::assertStringStartsWith('zzz_deleted_', $renamedSchemaDiff->changedTables[0]->newName);
83  self::assertEquals(
84  $this->maxIdentifierLength,
85  strlen($renamedSchemaDiff->changedTables[0]->newName)
86  );
87  }
88 
93  {
94  $table = $this->‪getTable();
95  $tableDiff = new TableDiff($table->getName());
96  $originalSchemaDiff = new SchemaDiff(null, [$tableDiff]);
97  $originalSchemaDiff->changedTables[0]->removedColumns[] = $this->‪getColumn();
98  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedFieldsToRenames', $originalSchemaDiff);
99 
100  self::assertStringStartsWith(
101  'zzz_deleted_',
102  $renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName()
103  );
104  self::assertEquals(
105  $this->maxIdentifierLength,
106  strlen($renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName())
107  );
108  }
109 
115  protected function ‪getTable(): Table
116  {
117  $tableName = 'table_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
118  $table = GeneralUtility::makeInstance(
119  Table::class,
120  $tableName
121  );
122 
123  return $table;
124  }
125 
132  protected function ‪getColumn(): Column
133  {
134  $columnName = 'column_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
135  $column = GeneralUtility::makeInstance(
136  Column::class,
137  $columnName,
138  Type::getType('string')
139  );
140 
141  return $column;
142  }
143 }
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getColumn
‪Column getColumn()
Definition: ConnectionMigratorTest.php:129
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema
Definition: ConnectionMigratorTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getTable
‪Table getTable()
Definition: ConnectionMigratorTest.php:112
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$subject
‪PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $subject
Definition: ConnectionMigratorTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:74
‪TYPO3\CMS\Core\Database\Schema\ConnectionMigrator
Definition: ConnectionMigrator.php:44
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$maxIdentifierLength
‪int $maxIdentifierLength
Definition: ConnectionMigratorTest.php:48
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest
Definition: ConnectionMigratorTest.php:37
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\setUp
‪setUp()
Definition: ConnectionMigratorTest.php:53
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation
Definition: PlatformInformation.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation\getMaxIdentifierLength
‪static int getMaxIdentifierLength(AbstractPlatform $platform)
Definition: PlatformInformation.php:111
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$platform
‪Doctrine DBAL Platforms AbstractPlatform Prophecy Prophecy ObjectProphecy $platform
Definition: ConnectionMigratorTest.php:40