1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\DBAL\Schema; |
||
6 | |||
7 | use Doctrine\Common\EventManager; |
||
8 | use Doctrine\DBAL\Configuration; |
||
9 | use Doctrine\DBAL\Connection; |
||
10 | use Doctrine\DBAL\Driver; |
||
11 | use Doctrine\DBAL\Platforms\MySqlPlatform; |
||
12 | use Doctrine\DBAL\Schema\Column; |
||
13 | use Doctrine\DBAL\Schema\MySqlSchemaManager; |
||
14 | use Doctrine\DBAL\Schema\Table; |
||
15 | use Doctrine\DBAL\Types\Type; |
||
16 | use PHPUnit\Framework\TestCase; |
||
17 | use function array_merge; |
||
18 | |||
19 | class MySqlInheritCharsetTest extends TestCase |
||
20 | { |
||
21 | public function testInheritTableOptionsFromDatabase() : void |
||
22 | { |
||
23 | // default, no overrides |
||
24 | $options = $this->getTableOptionsForOverride(); |
||
25 | self::assertFalse(isset($options['charset'])); |
||
26 | |||
27 | // explicit utf8 |
||
28 | $options = $this->getTableOptionsForOverride(['charset' => 'utf8']); |
||
29 | self::assertTrue(isset($options['charset'])); |
||
30 | self::assertSame($options['charset'], 'utf8'); |
||
31 | |||
32 | // explicit utf8mb4 |
||
33 | $options = $this->getTableOptionsForOverride(['charset' => 'utf8mb4']); |
||
34 | self::assertTrue(isset($options['charset'])); |
||
35 | self::assertSame($options['charset'], 'utf8mb4'); |
||
36 | } |
||
37 | |||
38 | public function testTableOptions() : void |
||
39 | { |
||
40 | $eventManager = new EventManager(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
41 | $driverMock = $this->createMock(Driver::class); |
||
42 | $platform = new MySqlPlatform(); |
||
43 | |||
44 | // default, no overrides |
||
45 | $table = new Table('foobar', [new Column('aa', Type::getType('integer'))]); |
||
46 | self::assertSame( |
||
47 | $platform->getCreateTableSQL($table), |
||
48 | ['CREATE TABLE foobar (aa INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'] |
||
49 | ); |
||
50 | |||
51 | // explicit utf8 |
||
52 | $table = new Table('foobar', [new Column('aa', Type::getType('integer'))]); |
||
53 | $table->addOption('charset', 'utf8'); |
||
54 | self::assertSame( |
||
55 | $platform->getCreateTableSQL($table), |
||
56 | ['CREATE TABLE foobar (aa INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'] |
||
57 | ); |
||
58 | |||
59 | // explicit utf8mb4 |
||
60 | $table = new Table('foobar', [new Column('aa', Type::getType('integer'))]); |
||
61 | $table->addOption('charset', 'utf8mb4'); |
||
62 | self::assertSame( |
||
63 | $platform->getCreateTableSQL($table), |
||
64 | ['CREATE TABLE foobar (aa INT NOT NULL) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'] |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string[] $overrideOptions |
||
70 | * |
||
71 | * @return string[] |
||
72 | */ |
||
73 | private function getTableOptionsForOverride(array $overrideOptions = []) : array |
||
74 | { |
||
75 | $eventManager = new EventManager(); |
||
76 | $driverMock = $this->createMock(Driver::class); |
||
77 | $platform = new MySqlPlatform(); |
||
78 | $connOptions = array_merge(['platform' => $platform], $overrideOptions); |
||
79 | $conn = new Connection($connOptions, $driverMock, new Configuration(), $eventManager); |
||
80 | $manager = new MySqlSchemaManager($conn, $platform); |
||
81 | |||
82 | $schemaConfig = $manager->createSchemaConfig(); |
||
83 | |||
84 | return $schemaConfig->getDefaultTableOptions(); |
||
85 | } |
||
86 | } |
||
87 |