Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/Sql/Ddl/AlterTableDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class AlterTableDecorator extends AlterTable implements PlatformDecoratorI
/** @var array{
* unsigned: int,
* zerofill: int,
* charset: int,
* collate: int,
* identity: int,
* serial: int,
* autoincrement: int,
Expand All @@ -40,14 +42,16 @@ final class AlterTableDecorator extends AlterTable implements PlatformDecoratorI
protected array $columnOptionSortOrder = [
'unsigned' => 0,
'zerofill' => 1,
'identity' => 2,
'serial' => 2,
'autoincrement' => 2,
'comment' => 3,
'columnformat' => 4,
'format' => 4,
'storage' => 5,
'after' => 6,
'charset' => 2,
'collate' => 3,
'identity' => 4,
'serial' => 4,
'autoincrement' => 4,
'comment' => 5,
'columnformat' => 6,
'format' => 6,
'storage' => 7,
'after' => 8,
];

public function setSubject(
Expand Down Expand Up @@ -115,6 +119,14 @@ protected function processAddColumns(?PlatformInterface $adapterPlatform = null)
$insert = ' ZEROFILL';
$j = 0;
break;
case 'charset':
$insert = ' CHARACTER SET ' . $coValue;
$j = 0;
break;
case 'collate':
$insert = ' COLLATE ' . $coValue;
$j = 0;
break;
case 'identity':
case 'serial':
case 'autoincrement':
Expand Down Expand Up @@ -179,6 +191,14 @@ protected function processChangeColumns(?PlatformInterface $adapterPlatform = nu
$insert = ' ZEROFILL';
$j = 0;
break;
case 'charset':
$insert = ' CHARACTER SET ' . $coValue;
$j = 0;
break;
case 'collate':
$insert = ' COLLATE ' . $coValue;
$j = 0;
break;
case 'identity':
case 'serial':
case 'autoincrement':
Expand Down
24 changes: 17 additions & 7 deletions src/Sql/Ddl/CreateTableDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ final class CreateTableDecorator extends CreateTable implements PlatformDecorato
protected $columnOptionSortOrder = [
'unsigned' => 0,
'zerofill' => 1,
'identity' => 2,
'serial' => 2,
'autoincrement' => 2,
'comment' => 3,
'columnformat' => 4,
'format' => 4,
'storage' => 5,
'charset' => 2,
'collate' => 3,
'identity' => 4,
'serial' => 4,
'autoincrement' => 4,
'comment' => 5,
'columnformat' => 6,
'format' => 6,
'storage' => 7,
];

public function setSubject(
Expand Down Expand Up @@ -113,6 +115,14 @@ protected function processColumns(?PlatformInterface $platform = null): ?array
$insert = ' ZEROFILL';
$j = 0;
break;
case 'charset':
$insert = ' CHARACTER SET ' . $coValue;
$j = 0;
break;
case 'collate':
$insert = ' COLLATE ' . $coValue;
$j = 0;
break;
case 'identity':
case 'serial':
case 'autoincrement':
Expand Down
163 changes: 163 additions & 0 deletions test/unit/Sql/Ddl/AlterTableDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

declare(strict_types=1);

namespace PhpDbTest\Mysql\Sql\Ddl;

use PhpDb\Adapter\Driver\Pdo\Result;
use PhpDb\Adapter\Driver\Pdo\Statement;
use PhpDb\Mysql\AdapterPlatform;
use PhpDb\Mysql\Pdo\Connection;
use PhpDb\Mysql\Pdo\Driver;
use PhpDb\Mysql\Sql\Ddl\AlterTableDecorator;
use PhpDb\Sql\Ddl\AlterTable;
use PhpDb\Sql\Ddl\Column;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;

#[CoversMethod(AlterTableDecorator::class, 'processAddColumns')]
#[CoversMethod(AlterTableDecorator::class, 'processChangeColumns')]
#[CoversMethod(AlterTableDecorator::class, 'getSqlInsertOffsets')]
final class AlterTableDecoratorTest extends TestCase
{
protected AdapterPlatform $platform;

protected function setUp(): void
{
$driver = new Driver(
$this->createMock(Connection::class),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These probably need to be createStub instead of Mock?

$this->createMock(Statement::class),
$this->createMock(Result::class),
);
$this->platform = new AdapterPlatform($driver);
}

private function buildSql(AlterTable $table): string
{
$decorator = new AlterTableDecorator();
$decorator->setSubject($table);

return $decorator->getSqlString($this->platform);
}

public function testAddColumnCharset(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setOption('charset', 'utf8mb3');
$alter->addColumn($col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('CHARACTER SET utf8mb3', $sql);
}

public function testAddColumnCollate(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setOption('collate', 'utf8mb3_unicode_ci');
$alter->addColumn($col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('COLLATE utf8mb3_unicode_ci', $sql);
}

public function testAddColumnCharsetAndCollate(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setOption('charset', 'utf8mb3');
$col->setOption('collate', 'utf8mb3_unicode_ci');
$alter->addColumn($col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci', $sql);
}

public function testAddColumnCharsetBeforeNotNull(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setNullable(false);
$col->setOption('charset', 'utf8mb3');
$col->setOption('collate', 'utf8mb3_unicode_ci');
$alter->addColumn($col);

$sql = $this->buildSql($alter);

self::assertMatchesRegularExpression(
'/CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL/',
$sql,
);
}

public function testChangeColumnCharset(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setOption('charset', 'utf8mb3');
$alter->changeColumn('name', $col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('CHARACTER SET utf8mb3', $sql);
}

public function testChangeColumnCollate(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setOption('collate', 'utf8mb3_unicode_ci');
$alter->changeColumn('name', $col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('COLLATE utf8mb3_unicode_ci', $sql);
}

public function testChangeColumnCharsetAndCollate(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setNullable(false);
$col->setOption('charset', 'utf8mb3');
$col->setOption('collate', 'utf8mb3_unicode_ci');
$alter->changeColumn('name', $col);

$sql = $this->buildSql($alter);

self::assertMatchesRegularExpression(
'/CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL/',
$sql,
);
}

public function testAddColumnAfter(): void
{
$alter = new AlterTable('test');
$col = new Column\Varchar('name', 255);
$col->setOption('after', 'id');
$alter->addColumn($col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('AFTER `id`', $sql);
}

public function testAddColumnUnsigned(): void
{
$alter = new AlterTable('test');
$col = new Column\Integer('id');
$col->setOption('unsigned', true);
$col->setOption('auto_increment', true);
$alter->addColumn($col);

$sql = $this->buildSql($alter);

self::assertStringContainsString('UNSIGNED', $sql);
self::assertStringContainsString('AUTO_INCREMENT', $sql);
}
}
Loading