[BUGFIX] Set correct unit test namespaces
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / EventListener / SchemaColumnDefinitionListenerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Database\Schema\EventListener;
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
18 use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
19 use Doctrine\DBAL\Platforms\AbstractPlatform;
20 use Doctrine\DBAL\Schema\Column;
21 use Doctrine\DBAL\Types\Type;
22 use Prophecy\Prophecy\ObjectProphecy;
23 use TYPO3\CMS\Core\Database\Connection;
24 use TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener;
25 use TYPO3\CMS\Core\Database\Schema\Types\EnumType;
26 use TYPO3\CMS\Core\Database\Schema\Types\SetType;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29
30 /**
31 * Test case
32 */
33 class SchemaColumnDefinitionListenerTest extends UnitTestCase
34 {
35 /**
36 * @var SchemaColumnDefinitionListener
37 */
38 protected $subject;
39
40 /**
41 * @var Connection|ObjectProphecy
42 */
43 protected $connectionProphet;
44
45 /**
46 * Set up the test subject
47 */
48 protected function setUp(): void
49 {
50 parent::setUp();
51 $this->subject = GeneralUtility::makeInstance(SchemaColumnDefinitionListener::class);
52 $this->connectionProphet = $this->prophesize(Connection::class);
53 }
54
55 /**
56 * @test
57 */
58 public function isInactiveForStandardColumnTypes(): void
59 {
60 $event = new SchemaColumnDefinitionEventArgs(
61 ['Type' => 'int(11)'],
62 'aTestTable',
63 'aTestDatabase',
64 $this->connectionProphet->reveal()
65 );
66
67 $this->subject->onSchemaColumnDefinition($event);
68 $this->assertNotTrue($event->isDefaultPrevented());
69 $this->assertNull($event->getColumn());
70 }
71
72 /**
73 * @test
74 */
75 public function buildsColumnForEnumDataType(): void
76 {
77 if (Type::hasType('enum')) {
78 Type::overrideType('enum', EnumType::class);
79 } else {
80 Type::addType('enum', EnumType::class);
81 }
82 $databasePlatformProphet = $this->prophesize(AbstractPlatform::class);
83 $databasePlatformProphet->getDoctrineTypeMapping('enum')->willReturn('enum');
84 $this->connectionProphet->getDatabasePlatform()->willReturn($databasePlatformProphet->reveal());
85
86 $event = new SchemaColumnDefinitionEventArgs(
87 ['Type' => "enum('value1', 'value2','value3')"],
88 'aTestTable',
89 'aTestDatabase',
90 $this->connectionProphet->reveal()
91 );
92
93 $this->subject->onSchemaColumnDefinition($event);
94 $this->assertTrue($event->isDefaultPrevented());
95 $this->assertInstanceOf(Column::class, $event->getColumn());
96 $this->assertInstanceOf(EnumType::class, $event->getColumn()->getType());
97 $this->assertSame(['value1', 'value2', 'value3'], $event->getColumn()->getPlatformOption('unquotedValues'));
98 }
99
100 /**
101 * @test
102 */
103 public function buildsColumnForSetDataType(): void
104 {
105 if (Type::hasType('set')) {
106 Type::overrideType('set', SetType::class);
107 } else {
108 Type::addType('set', SetType::class);
109 }
110 $databasePlatformProphet = $this->prophesize(AbstractPlatform::class);
111 $databasePlatformProphet->getDoctrineTypeMapping('set')->willReturn('set');
112 $this->connectionProphet->getDatabasePlatform()->willReturn($databasePlatformProphet->reveal());
113
114 $event = new SchemaColumnDefinitionEventArgs(
115 ['Type' => "set('value1', 'value3')"],
116 'aTestTable',
117 'aTestDatabase',
118 $this->connectionProphet->reveal()
119 );
120
121 $this->subject->onSchemaColumnDefinition($event);
122 $this->assertTrue($event->isDefaultPrevented());
123 $this->assertInstanceOf(Column::class, $event->getColumn());
124 $this->assertInstanceOf(SetType::class, $event->getColumn()->getType());
125 $this->assertSame(['value1', 'value3'], $event->getColumn()->getPlatformOption('unquotedValues'));
126 }
127 }