Skip to content
Closed
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
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
"@test",
"@test-integration"
],
"unit-test": [
"@test",
"@test-integration"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always --testsuite \"unit test\"",
Expand Down
36 changes: 36 additions & 0 deletions test/unit/DeprecatedAssertionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PhpDbTest\Adapter\Mysql;

use PHPUnit\Framework\Assert;
use ReflectionException;
use ReflectionProperty;

trait DeprecatedAssertionsTrait
{
/**
* @throws ReflectionException
*/
public static function assertAttributeEquals(
mixed $expected,
string $attribute,
object $instance,
string $message = ''
): void {
$r = new ReflectionProperty($instance, $attribute);
/** @psalm-suppress UnusedMethodCall */
$r->setAccessible(true);
Assert::assertEquals($expected, $r->getValue($instance), $message);
}

/**
* @throws ReflectionException
*/
public function readAttribute(object $instance, string $attribute): mixed
{
$r = new ReflectionProperty($instance, $attribute);
/** @psalm-suppress UnusedMethodCall */
$r->setAccessible(true);
return $r->getValue($instance);
}
}
118 changes: 118 additions & 0 deletions test/unit/Sql/Ddl/AlterTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace PhpDbTest\Adapter\Mysql\Sql\Ddl;

use PhpDb\Sql\Ddl\AlterTable;
use PhpDb\Sql\Ddl\Column;
use PhpDb\Sql\Ddl\Column\ColumnInterface;
use PhpDb\Sql\Ddl\Constraint;
use PhpDb\Sql\Ddl\Constraint\ConstraintInterface;
use PhpDb\Sql\TableIdentifier;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;

use function str_replace;

#[CoversMethod(AlterTable::class, 'setTable')]
#[CoversMethod(AlterTable::class, 'addColumn')]
#[CoversMethod(AlterTable::class, 'changeColumn')]
#[CoversMethod(AlterTable::class, 'dropColumn')]
#[CoversMethod(AlterTable::class, 'dropConstraint')]
#[CoversMethod(AlterTable::class, 'addConstraint')]
#[CoversMethod(AlterTable::class, 'dropIndex')]
#[CoversMethod(AlterTable::class, 'getSqlString')]
final class AlterTableTest extends TestCase
{
public function testSetTable(): void
{
$at = new AlterTable();
self::assertEquals('', $at->getRawState('table'));
self::assertSame($at, $at->setTable('test'));
self::assertEquals('test', $at->getRawState('table'));
}

public function testAddColumn(): void
{
$at = new AlterTable();
/** @var ColumnInterface $colMock */
$colMock = $this->getMockBuilder(ColumnInterface::class)->getMock();

Check failure on line 38 in test/unit/Sql/Ddl/AlterTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

PHPDoc tag `@var` with type PhpDb\Sql\Ddl\Column\ColumnInterface is not subtype of native type PHPUnit\Framework\MockObject\MockObject.

Check failure on line 38 in test/unit/Sql/Ddl/AlterTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

PHPDoc tag `@var` with type PhpDb\Sql\Ddl\Column\ColumnInterface is not subtype of native type PHPUnit\Framework\MockObject\MockObject.
self::assertSame($at, $at->addColumn($colMock));
self::assertEquals([$colMock], $at->getRawState($at::ADD_COLUMNS));
}

public function testChangeColumn(): void
{
$at = new AlterTable();
/** @var ColumnInterface $colMock */
$colMock = $this->getMockBuilder(ColumnInterface::class)->getMock();

Check failure on line 47 in test/unit/Sql/Ddl/AlterTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

PHPDoc tag `@var` with type PhpDb\Sql\Ddl\Column\ColumnInterface is not subtype of native type PHPUnit\Framework\MockObject\MockObject.

Check failure on line 47 in test/unit/Sql/Ddl/AlterTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

PHPDoc tag `@var` with type PhpDb\Sql\Ddl\Column\ColumnInterface is not subtype of native type PHPUnit\Framework\MockObject\MockObject.
self::assertSame($at, $at->changeColumn('newname', $colMock));
self::assertEquals(['newname' => $colMock], $at->getRawState($at::CHANGE_COLUMNS));
}

public function testDropColumn(): void
{
$at = new AlterTable();
self::assertSame($at, $at->dropColumn('foo'));
self::assertEquals(['foo'], $at->getRawState($at::DROP_COLUMNS));
}

public function testDropConstraint(): void
{
$at = new AlterTable();
self::assertSame($at, $at->dropConstraint('foo'));
self::assertEquals(['foo'], $at->getRawState($at::DROP_CONSTRAINTS));
}

public function testAddConstraint(): void
{
$at = new AlterTable();
/** @var ConstraintInterface $conMock */
$conMock = $this->getMockBuilder(ConstraintInterface::class)->getMock();

Check failure on line 70 in test/unit/Sql/Ddl/AlterTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

PHPDoc tag `@var` with type PhpDb\Sql\Ddl\Constraint\ConstraintInterface is not subtype of native type PHPUnit\Framework\MockObject\MockObject.

Check failure on line 70 in test/unit/Sql/Ddl/AlterTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

PHPDoc tag `@var` with type PhpDb\Sql\Ddl\Constraint\ConstraintInterface is not subtype of native type PHPUnit\Framework\MockObject\MockObject.
self::assertSame($at, $at->addConstraint($conMock));
self::assertEquals([$conMock], $at->getRawState($at::ADD_CONSTRAINTS));
}

public function testDropIndex(): void
{
$at = new AlterTable();
self::assertSame($at, $at->dropIndex('foo'));
self::assertEquals(['foo'], $at->getRawState($at::DROP_INDEXES));
}

/**
* @todo Implement testGetSqlString().
*/
public function testGetSqlString(): void
{
$at = new AlterTable('foo');
$at->addColumn(new Column\Varchar('another', 255));
$at->changeColumn('name', new Column\Varchar('new_name', 50));
$at->dropColumn('foo');
$at->addConstraint(new Constraint\ForeignKey('my_fk', 'other_id', 'other_table', 'id', 'CASCADE', 'CASCADE'));
$at->dropConstraint('my_constraint');
$at->dropIndex('my_index');
$expected = <<<EOS
ALTER TABLE "foo"
ADD COLUMN "another" VARCHAR(255) NOT NULL,
CHANGE COLUMN "name" "new_name" VARCHAR(50) NOT NULL,
DROP COLUMN "foo",
ADD CONSTRAINT "my_fk" FOREIGN KEY ("other_id") REFERENCES "other_table" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
DROP CONSTRAINT "my_constraint",
DROP INDEX "my_index"
EOS;

$actual = $at->getSqlString();
self::assertEquals(
str_replace(["\r", "\n"], "", $expected),
str_replace(["\r", "\n"], "", $actual)
);

$at = new AlterTable(new TableIdentifier('foo'));
$at->addColumn(new Column\Column('bar'));
$this->assertEquals("ALTER TABLE \"foo\"\n ADD COLUMN \"bar\" INTEGER NOT NULL", $at->getSqlString());

$at = new AlterTable(new TableIdentifier('bar', 'foo'));
$at->addColumn(new Column\Column('baz'));
$this->assertEquals("ALTER TABLE \"foo\".\"bar\"\n ADD COLUMN \"baz\" INTEGER NOT NULL", $at->getSqlString());
}
}
154 changes: 154 additions & 0 deletions test/unit/Sql/Ddl/CreateTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace PhpDbTest\Adapter\Mysql\Sql\Ddl;

use PhpDb\Sql\Ddl\Column\Column;
use PhpDb\Sql\Ddl\Column\ColumnInterface;
use PhpDb\Sql\Ddl\Constraint;
use PhpDb\Sql\Ddl\Constraint\ConstraintInterface;
use PhpDb\Sql\Ddl\CreateTable;
use PhpDb\Sql\TableIdentifier;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;

use function array_pop;

#[CoversMethod(CreateTable::class, '__construct')]
#[CoversMethod(CreateTable::class, 'setTemporary')]
#[CoversMethod(CreateTable::class, 'isTemporary')]
#[CoversMethod(CreateTable::class, 'setTable')]
#[CoversMethod(CreateTable::class, 'getRawState')]
#[CoversMethod(CreateTable::class, 'addColumn')]
#[CoversMethod(CreateTable::class, 'addConstraint')]
#[CoversMethod(CreateTable::class, 'getSqlString')]
final class CreateTableTest extends TestCase
{
/**
* test object construction
*/
public function testObjectConstruction(): void
{
$ct = new CreateTable('foo', true);
self::assertEquals('foo', $ct->getRawState($ct::TABLE));
self::assertTrue($ct->isTemporary());
}

public function testSetTemporary(): void
{
$ct = new CreateTable();
self::assertSame($ct, $ct->setTemporary(false));
self::assertFalse($ct->isTemporary());
$ct->setTemporary(true);
self::assertTrue($ct->isTemporary());
$ct->setTemporary('yes');

Check failure on line 44 in test/unit/Sql/Ddl/CreateTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #1 $temporary of method PhpDb\Sql\Ddl\CreateTable::setTemporary() expects bool, string given.

Check failure on line 44 in test/unit/Sql/Ddl/CreateTableTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #1 $temporary of method PhpDb\Sql\Ddl\CreateTable::setTemporary() expects bool, string given.
self::assertTrue($ct->isTemporary());

self::assertStringStartsWith("CREATE TEMPORARY TABLE", $ct->getSqlString());
}

public function testIsTemporary(): void
{
$ct = new CreateTable();
self::assertFalse($ct->isTemporary());
$ct->setTemporary(true);
self::assertTrue($ct->isTemporary());
}

public function testSetTable(): CreateTable
{
$ct = new CreateTable();
self::assertEquals('', $ct->getRawState('table'));
$ct->setTable('test');
return $ct;
}

#[Depends('testSetTable')]
public function testRawStateViaTable(CreateTable $ct): void
{
self::assertEquals('test', $ct->getRawState('table'));
}

public function testAddColumn(): CreateTable
{
$column = $this->getMockBuilder(ColumnInterface::class)->getMock();
$ct = new CreateTable();
self::assertSame($ct, $ct->addColumn($column));
return $ct;
}

#[Depends('testAddColumn')]
public function testRawStateViaColumn(CreateTable $ct): void
{
$state = $ct->getRawState('columns');
self::assertIsArray($state);
$column = array_pop($state);
self::assertInstanceOf(ColumnInterface::class, $column);
}

public function testAddConstraint(): CreateTable
{
$constraint = $this->getMockBuilder(ConstraintInterface::class)->getMock();
$ct = new CreateTable();
self::assertSame($ct, $ct->addConstraint($constraint));
return $ct;
}

#[Depends('testAddConstraint')]
public function testRawStateViaConstraint(CreateTable $ct): void
{
$state = $ct->getRawState('constraints');
self::assertIsArray($state);
$constraint = array_pop($state);
self::assertInstanceOf(ConstraintInterface::class, $constraint);
}

public function testGetSqlString(): void
{
$ct = new CreateTable('foo');
self::assertEquals("CREATE TABLE \"foo\" ( \n)", $ct->getSqlString());

$ct = new CreateTable('foo', true);
self::assertEquals("CREATE TEMPORARY TABLE \"foo\" ( \n)", $ct->getSqlString());

$ct = new CreateTable('foo');
$ct->addColumn(new Column('bar'));
self::assertEquals("CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());

$ct = new CreateTable('foo', true);
$ct->addColumn(new Column('bar'));
self::assertEquals("CREATE TEMPORARY TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());

$ct = new CreateTable('foo', true);
$ct->addColumn(new Column('bar'));
$ct->addColumn(new Column('baz'));
self::assertEquals(
"CREATE TEMPORARY TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL,\n \"baz\" INTEGER NOT NULL \n)",
$ct->getSqlString()
);

$ct = new CreateTable('foo');
$ct->addColumn(new Column('bar'));
$ct->addConstraint(new Constraint\PrimaryKey('bat'));
self::assertEquals(
"CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL , \n PRIMARY KEY (\"bat\") \n)",
$ct->getSqlString()
);

$ct = new CreateTable('foo');
$ct->addConstraint(new Constraint\PrimaryKey('bar'));
$ct->addConstraint(new Constraint\PrimaryKey('bat'));
self::assertEquals(
"CREATE TABLE \"foo\" ( \n PRIMARY KEY (\"bar\"),\n PRIMARY KEY (\"bat\") \n)",
$ct->getSqlString()
);

$ct = new CreateTable(new TableIdentifier('foo'));
$ct->addColumn(new Column('bar'));
self::assertEquals("CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());

$ct = new CreateTable(new TableIdentifier('bar', 'foo'));
$ct->addColumn(new Column('baz'));
self::assertEquals("CREATE TABLE \"foo\".\"bar\" ( \n \"baz\" INTEGER NOT NULL \n)", $ct->getSqlString());
}
}
24 changes: 24 additions & 0 deletions test/unit/Sql/Ddl/DropTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PhpDbTest\Adapter\Mysql\Sql\Ddl;

use PhpDb\Sql\Ddl\DropTable;
use PhpDb\Sql\TableIdentifier;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;

#[CoversMethod(DropTable::class, 'getSqlString')]
final class DropTableTest extends TestCase
{
public function testGetSqlString(): void
{
$dt = new DropTable('foo');
self::assertEquals('DROP TABLE "foo"', $dt->getSqlString());

$dt = new DropTable(new TableIdentifier('foo'));
self::assertEquals('DROP TABLE "foo"', $dt->getSqlString());

$dt = new DropTable(new TableIdentifier('bar', 'foo'));
self::assertEquals('DROP TABLE "foo"."bar"', $dt->getSqlString());
}
}
Loading