‪TYPO3CMS  11.5
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;
27 use Prophecy\PhpUnit\ProphecyTrait;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪ConnectionMigratorTest extends UnitTestCase
38 {
39  use ProphecyTrait;
40 
41  protected MySQLPlatform ‪$platform;
42 
46  protected ‪$subject;
47 
48  protected int ‪$maxIdentifierLength = -1;
49 
53  protected function ‪setUp(): void
54  {
55  parent::setUp();
56 
57  $platformMock = $this->prophesize(MySqlPlatform::class);
58  $platformMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
59  $this->platform = $platformMock->reveal();
60 
61  $connectionMock = $this->prophesize(Connection::class);
62  $connectionMock->getDatabasePlatform()->willReturn($this->platform);
63  $connectionMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
64 
65  $this->maxIdentifierLength = ‪PlatformInformation::getMaxIdentifierLength($this->platform);
66 
67  $this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, [], '', false);
68  $this->subject->_set('connection', $connectionMock->reveal());
69  }
70 
75  {
76  $originalSchemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, null, null, [$this->‪getTable()]);
77  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedTablesToRenames', $originalSchemaDiff);
78 
79  self::assertStringStartsWith('zzz_deleted_', $renamedSchemaDiff->changedTables[0]->newName);
80  self::assertEquals(
81  $this->maxIdentifierLength,
82  strlen($renamedSchemaDiff->changedTables[0]->newName)
83  );
84  }
85 
90  {
91  $table = $this->‪getTable();
92  $tableDiff = new TableDiff($table->getName());
93  $originalSchemaDiff = new SchemaDiff(null, [$tableDiff]);
94  $originalSchemaDiff->changedTables[0]->removedColumns[] = $this->‪getColumn();
95  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedFieldsToRenames', $originalSchemaDiff);
96 
97  self::assertStringStartsWith(
98  'zzz_deleted_',
99  $renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName()
100  );
101  self::assertEquals(
102  $this->maxIdentifierLength,
103  strlen($renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName())
104  );
105  }
106 
112  protected function ‪getTable(): Table
113  {
114  $tableName = 'table_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
115  return new Table($tableName);
116  }
117 
124  protected function ‪getColumn(): Column
125  {
126  $columnName = 'column_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
127  return new Column(
128  $columnName,
129  Type::getType('string')
130  );
131  }
132 }
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getColumn
‪Column getColumn()
Definition: ConnectionMigratorTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:87
‪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:110
‪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:72
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$platform
‪MySQLPlatform $platform
Definition: ConnectionMigratorTest.php:40
‪TYPO3\CMS\Core\Database\Schema\ConnectionMigrator
Definition: ConnectionMigrator.php:44
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$maxIdentifierLength
‪int $maxIdentifierLength
Definition: ConnectionMigratorTest.php:46
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest
Definition: ConnectionMigratorTest.php:38
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\setUp
‪setUp()
Definition: ConnectionMigratorTest.php:51
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation
Definition: PlatformInformation.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation\getMaxIdentifierLength
‪static int getMaxIdentifierLength(AbstractPlatform $platform)
Definition: PlatformInformation.php:111