‪TYPO3CMS  9.5
ConnectionMigratorTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Doctrine\DBAL\Platforms\MySqlPlatform;
20 use Doctrine\DBAL\Schema\Column;
21 use Doctrine\DBAL\Schema\SchemaDiff;
22 use Doctrine\DBAL\Schema\Table;
23 use Doctrine\DBAL\Schema\TableDiff;
24 use Doctrine\DBAL\Types\Type;
25 use Prophecy\Argument;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪ConnectionMigratorTest extends UnitTestCase
36 {
40  protected ‪$platform;
41 
45  protected ‪$subject;
46 
50  protected ‪$maxIdentifierLength = -1;
51 
55  protected function ‪setUp()
56  {
57  parent::setUp();
58 
59  $platformMock = $this->prophesize(MySqlPlatform::class);
60  $platformMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
61  $this->platform = $platformMock->reveal();
62 
63  $connectionMock = $this->prophesize(Connection::class);
64  $connectionMock->getDatabasePlatform()->willReturn($this->platform);
65  $connectionMock->quoteIdentifier(Argument::any())->willReturnArgument(0);
66 
67  $this->maxIdentifierLength = ‪PlatformInformation::getMaxIdentifierLength($this->platform);
68 
69  $this->subject = $this->getAccessibleMock(ConnectionMigrator::class, null, [], '', false);
70  $this->subject->_set('connection', $connectionMock->reveal());
71  }
72 
77  {
78  $originalSchemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, null, null, [$this->‪getTable()]);
79  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedTablesToRenames', $originalSchemaDiff);
80 
81  $this->assertStringStartsWith('zzz_deleted_', $renamedSchemaDiff->changedTables[0]->newName);
82  $this->assertEquals(
83  $this->maxIdentifierLength,
84  strlen($renamedSchemaDiff->changedTables[0]->newName)
85  );
86  }
87 
92  {
93  $table = $this->‪getTable();
94  $tableDiff = new TableDiff($table->getName());
95  $originalSchemaDiff = new SchemaDiff(null, [$tableDiff]);
96  $originalSchemaDiff->changedTables[0]->removedColumns[] = $this->‪getColumn();
97  $renamedSchemaDiff = $this->subject->_call('migrateUnprefixedRemovedFieldsToRenames', $originalSchemaDiff);
98 
99  $this->assertStringStartsWith(
100  'zzz_deleted_',
101  $renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName()
102  );
103  $this->assertEquals(
104  $this->maxIdentifierLength,
105  strlen($renamedSchemaDiff->changedTables[0]->changedColumns[0]->column->getName())
106  );
107  }
108 
114  protected function ‪getTable(): Table
115  {
116  $tableName = 'table_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
117  $table = GeneralUtility::makeInstance(
118  Table::class,
119  $tableName
120  );
121 
122  return $table;
123  }
124 
131  protected function ‪getColumn(): Column
132  {
133  $columnName = 'column_name_that_is_ridiculously_long_' . bin2hex(random_bytes(100));
134  $column = GeneralUtility::makeInstance(
135  Column::class,
136  $columnName,
137  Type::getType('string')
138  );
139 
140  return $column;
141  }
142 }
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getColumn
‪Column getColumn()
Definition: ConnectionMigratorTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪columnNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema
Definition: ConnectionMigratorTest.php:4
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\getTable
‪Table getTable()
Definition: ConnectionMigratorTest.php:111
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval
‪tableNamesStickToTheMaximumCharactersWhenPrefixedForRemoval()
Definition: ConnectionMigratorTest.php:73
‪TYPO3\CMS\Core\Database\Schema\ConnectionMigrator
Definition: ConnectionMigrator.php:41
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$subject
‪PHPUnit_Framework_MockObject_MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $subject
Definition: ConnectionMigratorTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$maxIdentifierLength
‪int $maxIdentifierLength
Definition: ConnectionMigratorTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest
Definition: ConnectionMigratorTest.php:36
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\setUp
‪setUp()
Definition: ConnectionMigratorTest.php:52
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation
Definition: PlatformInformation.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation\getMaxIdentifierLength
‪static int getMaxIdentifierLength(AbstractPlatform $platform)
Definition: PlatformInformation.php:57
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\ConnectionMigratorTest\$platform
‪Doctrine DBAL Platforms AbstractPlatform Prophecy Prophecy ObjectProphecy $platform
Definition: ConnectionMigratorTest.php:39