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