|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\Database\Tests\Functional\Driver\MySQL\Schema; |
| 6 | + |
| 7 | +use Cycle\Database\Driver\MySQL\Schema\MySQLColumn; |
| 8 | +use Cycle\Database\Tests\Functional\Driver\Common\BaseTest; |
| 9 | + |
| 10 | +/** |
| 11 | + * @group driver |
| 12 | + * @group driver-mysql |
| 13 | + */ |
| 14 | +class CharsetCollationTest extends BaseTest |
| 15 | +{ |
| 16 | + public const DRIVER = 'mysql'; |
| 17 | + |
| 18 | + public function testStringColumnWithCharsetAndCollation(): void |
| 19 | + { |
| 20 | + $schema = $this->schema('table'); |
| 21 | + $this->assertFalse($schema->exists()); |
| 22 | + |
| 23 | + $schema->primary('id'); |
| 24 | + $column = $schema->string('email', size: 255); |
| 25 | + $column->charset('ascii'); |
| 26 | + $column->collation('ascii_bin'); |
| 27 | + $column->nullable(false); |
| 28 | + $schema->save(); |
| 29 | + |
| 30 | + $schema = $this->schema('table'); |
| 31 | + $this->assertTrue($schema->exists()); |
| 32 | + |
| 33 | + $emailColumn = $schema->column('email'); |
| 34 | + \assert($emailColumn instanceof MySQLColumn); |
| 35 | + |
| 36 | + $this->assertSame('ascii', $emailColumn->getCharset()); |
| 37 | + $this->assertSame('ascii_bin', $emailColumn->getCollation()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testStringColumnCharsetCollationPersistsAfterReload(): void |
| 41 | + { |
| 42 | + $schema = $this->schema('table'); |
| 43 | + |
| 44 | + $schema->primary('id'); |
| 45 | + $column = $schema->string('name', size: 100); |
| 46 | + $column->charset('utf8mb4'); |
| 47 | + $column->collation('utf8mb4_unicode_ci'); |
| 48 | + $schema->save(); |
| 49 | + |
| 50 | + $schema = $this->schema('table'); |
| 51 | + |
| 52 | + $nameColumn = $schema->column('name'); |
| 53 | + \assert($nameColumn instanceof MySQLColumn); |
| 54 | + |
| 55 | + $this->assertSame('utf8mb4', $nameColumn->getCharset()); |
| 56 | + $this->assertSame('utf8mb4_unicode_ci', $nameColumn->getCollation()); |
| 57 | + $this->assertSameAsInDB($schema); |
| 58 | + } |
| 59 | + |
| 60 | + public function testChangeCollation(): void |
| 61 | + { |
| 62 | + $schema = $this->schema('table'); |
| 63 | + |
| 64 | + $schema->primary('id'); |
| 65 | + $column = $schema->string('title', size: 255); |
| 66 | + $column->charset('ascii'); |
| 67 | + $column->collation('ascii_general_ci'); |
| 68 | + $schema->save(); |
| 69 | + |
| 70 | + $schema = $this->schema('table'); |
| 71 | + $titleColumn = $schema->column('title'); |
| 72 | + \assert($titleColumn instanceof MySQLColumn); |
| 73 | + $this->assertSame('ascii_general_ci', $titleColumn->getCollation()); |
| 74 | + |
| 75 | + // Change collation |
| 76 | + $titleColumn->collation('ascii_bin'); |
| 77 | + $schema->save(); |
| 78 | + |
| 79 | + $schema = $this->schema('table'); |
| 80 | + $titleColumn = $schema->column('title'); |
| 81 | + \assert($titleColumn instanceof MySQLColumn); |
| 82 | + $this->assertSame('ascii_bin', $titleColumn->getCollation()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testTextColumnWithCharsetAndCollation(): void |
| 86 | + { |
| 87 | + $schema = $this->schema('table'); |
| 88 | + |
| 89 | + $schema->primary('id'); |
| 90 | + $column = $schema->text('content'); |
| 91 | + $column->charset('utf8mb4'); |
| 92 | + $column->collation('utf8mb4_bin'); |
| 93 | + $schema->save(); |
| 94 | + |
| 95 | + $schema = $this->schema('table'); |
| 96 | + |
| 97 | + $contentColumn = $schema->column('content'); |
| 98 | + \assert($contentColumn instanceof MySQLColumn); |
| 99 | + |
| 100 | + $this->assertSame('utf8mb4', $contentColumn->getCharset()); |
| 101 | + $this->assertSame('utf8mb4_bin', $contentColumn->getCollation()); |
| 102 | + } |
| 103 | + |
| 104 | + public function testColumnWithoutExplicitCharsetInheritsDefault(): void |
| 105 | + { |
| 106 | + $schema = $this->schema('table'); |
| 107 | + |
| 108 | + $schema->primary('id'); |
| 109 | + $schema->string('name', size: 100); |
| 110 | + $schema->save(); |
| 111 | + |
| 112 | + $schema = $this->schema('table'); |
| 113 | + $this->assertSameAsInDB($schema); |
| 114 | + |
| 115 | + $nameColumn = $schema->column('name'); |
| 116 | + \assert($nameColumn instanceof MySQLColumn); |
| 117 | + |
| 118 | + // Column should have inherited charset/collation from table/database defaults |
| 119 | + $this->assertNotEmpty($nameColumn->getCharset()); |
| 120 | + $this->assertNotEmpty($nameColumn->getCollation()); |
| 121 | + } |
| 122 | + |
| 123 | + public function testCompareColumnsWithSameCharset(): void |
| 124 | + { |
| 125 | + $schema = $this->schema('table'); |
| 126 | + |
| 127 | + $schema->primary('id'); |
| 128 | + $column = $schema->string('email', size: 255); |
| 129 | + $column->charset('ascii'); |
| 130 | + $column->collation('ascii_bin'); |
| 131 | + $column->nullable(false); |
| 132 | + $schema->save(); |
| 133 | + |
| 134 | + $schema = $this->schema('table'); |
| 135 | + $dbColumn = $schema->column('email'); |
| 136 | + |
| 137 | + $this->assertTrue($column->compare($dbColumn)); |
| 138 | + } |
| 139 | +} |
0 commit comments