Skip to content

Commit d6d6f65

Browse files
committed
Attempt to improve adapter specific platform testing.
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent e49dd91 commit d6d6f65

File tree

11 files changed

+2233
-0
lines changed

11 files changed

+2233
-0
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
"@test",
6767
"@test-integration"
6868
],
69+
"unit-test": [
70+
"@test",
71+
"@test-integration"
72+
],
6973
"cs-check": "phpcs",
7074
"cs-fix": "phpcbf",
7175
"test": "phpunit --colors=always --testsuite \"unit test\"",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace PhpDbTest\Adapter\Mysql;
4+
5+
use PHPUnit\Framework\Assert;
6+
use ReflectionException;
7+
use ReflectionProperty;
8+
9+
trait DeprecatedAssertionsTrait
10+
{
11+
/**
12+
* @throws ReflectionException
13+
*/
14+
public static function assertAttributeEquals(
15+
mixed $expected,
16+
string $attribute,
17+
object $instance,
18+
string $message = ''
19+
): void {
20+
$r = new ReflectionProperty($instance, $attribute);
21+
/** @psalm-suppress UnusedMethodCall */
22+
$r->setAccessible(true);
23+
Assert::assertEquals($expected, $r->getValue($instance), $message);
24+
}
25+
26+
/**
27+
* @throws ReflectionException
28+
*/
29+
public function readAttribute(object $instance, string $attribute): mixed
30+
{
31+
$r = new ReflectionProperty($instance, $attribute);
32+
/** @psalm-suppress UnusedMethodCall */
33+
$r->setAccessible(true);
34+
return $r->getValue($instance);
35+
}
36+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace PhpDbTest\Adapter\Mysql\Sql\Ddl;
4+
5+
use PhpDb\Sql\Ddl\AlterTable;
6+
use PhpDb\Sql\Ddl\Column;
7+
use PhpDb\Sql\Ddl\Column\ColumnInterface;
8+
use PhpDb\Sql\Ddl\Constraint;
9+
use PhpDb\Sql\Ddl\Constraint\ConstraintInterface;
10+
use PhpDb\Sql\TableIdentifier;
11+
use PHPUnit\Framework\Attributes\CoversMethod;
12+
use PHPUnit\Framework\TestCase;
13+
14+
use function str_replace;
15+
16+
#[CoversMethod(AlterTable::class, 'setTable')]
17+
#[CoversMethod(AlterTable::class, 'addColumn')]
18+
#[CoversMethod(AlterTable::class, 'changeColumn')]
19+
#[CoversMethod(AlterTable::class, 'dropColumn')]
20+
#[CoversMethod(AlterTable::class, 'dropConstraint')]
21+
#[CoversMethod(AlterTable::class, 'addConstraint')]
22+
#[CoversMethod(AlterTable::class, 'dropIndex')]
23+
#[CoversMethod(AlterTable::class, 'getSqlString')]
24+
final class AlterTableTest extends TestCase
25+
{
26+
public function testSetTable(): void
27+
{
28+
$at = new AlterTable();
29+
self::assertEquals('', $at->getRawState('table'));
30+
self::assertSame($at, $at->setTable('test'));
31+
self::assertEquals('test', $at->getRawState('table'));
32+
}
33+
34+
public function testAddColumn(): void
35+
{
36+
$at = new AlterTable();
37+
/** @var ColumnInterface $colMock */
38+
$colMock = $this->getMockBuilder(ColumnInterface::class)->getMock();
39+
self::assertSame($at, $at->addColumn($colMock));
40+
self::assertEquals([$colMock], $at->getRawState($at::ADD_COLUMNS));
41+
}
42+
43+
public function testChangeColumn(): void
44+
{
45+
$at = new AlterTable();
46+
/** @var ColumnInterface $colMock */
47+
$colMock = $this->getMockBuilder(ColumnInterface::class)->getMock();
48+
self::assertSame($at, $at->changeColumn('newname', $colMock));
49+
self::assertEquals(['newname' => $colMock], $at->getRawState($at::CHANGE_COLUMNS));
50+
}
51+
52+
public function testDropColumn(): void
53+
{
54+
$at = new AlterTable();
55+
self::assertSame($at, $at->dropColumn('foo'));
56+
self::assertEquals(['foo'], $at->getRawState($at::DROP_COLUMNS));
57+
}
58+
59+
public function testDropConstraint(): void
60+
{
61+
$at = new AlterTable();
62+
self::assertSame($at, $at->dropConstraint('foo'));
63+
self::assertEquals(['foo'], $at->getRawState($at::DROP_CONSTRAINTS));
64+
}
65+
66+
public function testAddConstraint(): void
67+
{
68+
$at = new AlterTable();
69+
/** @var ConstraintInterface $conMock */
70+
$conMock = $this->getMockBuilder(ConstraintInterface::class)->getMock();
71+
self::assertSame($at, $at->addConstraint($conMock));
72+
self::assertEquals([$conMock], $at->getRawState($at::ADD_CONSTRAINTS));
73+
}
74+
75+
public function testDropIndex(): void
76+
{
77+
$at = new AlterTable();
78+
self::assertSame($at, $at->dropIndex('foo'));
79+
self::assertEquals(['foo'], $at->getRawState($at::DROP_INDEXES));
80+
}
81+
82+
/**
83+
* @todo Implement testGetSqlString().
84+
*/
85+
public function testGetSqlString(): void
86+
{
87+
$at = new AlterTable('foo');
88+
$at->addColumn(new Column\Varchar('another', 255));
89+
$at->changeColumn('name', new Column\Varchar('new_name', 50));
90+
$at->dropColumn('foo');
91+
$at->addConstraint(new Constraint\ForeignKey('my_fk', 'other_id', 'other_table', 'id', 'CASCADE', 'CASCADE'));
92+
$at->dropConstraint('my_constraint');
93+
$at->dropIndex('my_index');
94+
$expected = <<<EOS
95+
ALTER TABLE "foo"
96+
ADD COLUMN "another" VARCHAR(255) NOT NULL,
97+
CHANGE COLUMN "name" "new_name" VARCHAR(50) NOT NULL,
98+
DROP COLUMN "foo",
99+
ADD CONSTRAINT "my_fk" FOREIGN KEY ("other_id") REFERENCES "other_table" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
100+
DROP CONSTRAINT "my_constraint",
101+
DROP INDEX "my_index"
102+
EOS;
103+
104+
$actual = $at->getSqlString();
105+
self::assertEquals(
106+
str_replace(["\r", "\n"], "", $expected),
107+
str_replace(["\r", "\n"], "", $actual)
108+
);
109+
110+
$at = new AlterTable(new TableIdentifier('foo'));
111+
$at->addColumn(new Column\Column('bar'));
112+
$this->assertEquals("ALTER TABLE \"foo\"\n ADD COLUMN \"bar\" INTEGER NOT NULL", $at->getSqlString());
113+
114+
$at = new AlterTable(new TableIdentifier('bar', 'foo'));
115+
$at->addColumn(new Column\Column('baz'));
116+
$this->assertEquals("ALTER TABLE \"foo\".\"bar\"\n ADD COLUMN \"baz\" INTEGER NOT NULL", $at->getSqlString());
117+
}
118+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace PhpDbTest\Adapter\Mysql\Sql\Ddl;
4+
5+
use PhpDb\Sql\Ddl\Column\Column;
6+
use PhpDb\Sql\Ddl\Column\ColumnInterface;
7+
use PhpDb\Sql\Ddl\Constraint;
8+
use PhpDb\Sql\Ddl\Constraint\ConstraintInterface;
9+
use PhpDb\Sql\Ddl\CreateTable;
10+
use PhpDb\Sql\TableIdentifier;
11+
use PHPUnit\Framework\Attributes\CoversMethod;
12+
use PHPUnit\Framework\Attributes\Depends;
13+
use PHPUnit\Framework\TestCase;
14+
15+
use function array_pop;
16+
17+
#[CoversMethod(CreateTable::class, '__construct')]
18+
#[CoversMethod(CreateTable::class, 'setTemporary')]
19+
#[CoversMethod(CreateTable::class, 'isTemporary')]
20+
#[CoversMethod(CreateTable::class, 'setTable')]
21+
#[CoversMethod(CreateTable::class, 'getRawState')]
22+
#[CoversMethod(CreateTable::class, 'addColumn')]
23+
#[CoversMethod(CreateTable::class, 'addConstraint')]
24+
#[CoversMethod(CreateTable::class, 'getSqlString')]
25+
final class CreateTableTest extends TestCase
26+
{
27+
/**
28+
* test object construction
29+
*/
30+
public function testObjectConstruction(): void
31+
{
32+
$ct = new CreateTable('foo', true);
33+
self::assertEquals('foo', $ct->getRawState($ct::TABLE));
34+
self::assertTrue($ct->isTemporary());
35+
}
36+
37+
public function testSetTemporary(): void
38+
{
39+
$ct = new CreateTable();
40+
self::assertSame($ct, $ct->setTemporary(false));
41+
self::assertFalse($ct->isTemporary());
42+
$ct->setTemporary(true);
43+
self::assertTrue($ct->isTemporary());
44+
$ct->setTemporary('yes');
45+
self::assertTrue($ct->isTemporary());
46+
47+
self::assertStringStartsWith("CREATE TEMPORARY TABLE", $ct->getSqlString());
48+
}
49+
50+
public function testIsTemporary(): void
51+
{
52+
$ct = new CreateTable();
53+
self::assertFalse($ct->isTemporary());
54+
$ct->setTemporary(true);
55+
self::assertTrue($ct->isTemporary());
56+
}
57+
58+
public function testSetTable(): CreateTable
59+
{
60+
$ct = new CreateTable();
61+
self::assertEquals('', $ct->getRawState('table'));
62+
$ct->setTable('test');
63+
return $ct;
64+
}
65+
66+
#[Depends('testSetTable')]
67+
public function testRawStateViaTable(CreateTable $ct): void
68+
{
69+
self::assertEquals('test', $ct->getRawState('table'));
70+
}
71+
72+
public function testAddColumn(): CreateTable
73+
{
74+
$column = $this->getMockBuilder(ColumnInterface::class)->getMock();
75+
$ct = new CreateTable();
76+
self::assertSame($ct, $ct->addColumn($column));
77+
return $ct;
78+
}
79+
80+
#[Depends('testAddColumn')]
81+
public function testRawStateViaColumn(CreateTable $ct): void
82+
{
83+
$state = $ct->getRawState('columns');
84+
self::assertIsArray($state);
85+
$column = array_pop($state);
86+
self::assertInstanceOf(ColumnInterface::class, $column);
87+
}
88+
89+
public function testAddConstraint(): CreateTable
90+
{
91+
$constraint = $this->getMockBuilder(ConstraintInterface::class)->getMock();
92+
$ct = new CreateTable();
93+
self::assertSame($ct, $ct->addConstraint($constraint));
94+
return $ct;
95+
}
96+
97+
#[Depends('testAddConstraint')]
98+
public function testRawStateViaConstraint(CreateTable $ct): void
99+
{
100+
$state = $ct->getRawState('constraints');
101+
self::assertIsArray($state);
102+
$constraint = array_pop($state);
103+
self::assertInstanceOf(ConstraintInterface::class, $constraint);
104+
}
105+
106+
public function testGetSqlString(): void
107+
{
108+
$ct = new CreateTable('foo');
109+
self::assertEquals("CREATE TABLE \"foo\" ( \n)", $ct->getSqlString());
110+
111+
$ct = new CreateTable('foo', true);
112+
self::assertEquals("CREATE TEMPORARY TABLE \"foo\" ( \n)", $ct->getSqlString());
113+
114+
$ct = new CreateTable('foo');
115+
$ct->addColumn(new Column('bar'));
116+
self::assertEquals("CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());
117+
118+
$ct = new CreateTable('foo', true);
119+
$ct->addColumn(new Column('bar'));
120+
self::assertEquals("CREATE TEMPORARY TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());
121+
122+
$ct = new CreateTable('foo', true);
123+
$ct->addColumn(new Column('bar'));
124+
$ct->addColumn(new Column('baz'));
125+
self::assertEquals(
126+
"CREATE TEMPORARY TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL,\n \"baz\" INTEGER NOT NULL \n)",
127+
$ct->getSqlString()
128+
);
129+
130+
$ct = new CreateTable('foo');
131+
$ct->addColumn(new Column('bar'));
132+
$ct->addConstraint(new Constraint\PrimaryKey('bat'));
133+
self::assertEquals(
134+
"CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL , \n PRIMARY KEY (\"bat\") \n)",
135+
$ct->getSqlString()
136+
);
137+
138+
$ct = new CreateTable('foo');
139+
$ct->addConstraint(new Constraint\PrimaryKey('bar'));
140+
$ct->addConstraint(new Constraint\PrimaryKey('bat'));
141+
self::assertEquals(
142+
"CREATE TABLE \"foo\" ( \n PRIMARY KEY (\"bar\"),\n PRIMARY KEY (\"bat\") \n)",
143+
$ct->getSqlString()
144+
);
145+
146+
$ct = new CreateTable(new TableIdentifier('foo'));
147+
$ct->addColumn(new Column('bar'));
148+
self::assertEquals("CREATE TABLE \"foo\" ( \n \"bar\" INTEGER NOT NULL \n)", $ct->getSqlString());
149+
150+
$ct = new CreateTable(new TableIdentifier('bar', 'foo'));
151+
$ct->addColumn(new Column('baz'));
152+
self::assertEquals("CREATE TABLE \"foo\".\"bar\" ( \n \"baz\" INTEGER NOT NULL \n)", $ct->getSqlString());
153+
}
154+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PhpDbTest\Adapter\Mysql\Sql\Ddl;
4+
5+
use PhpDb\Sql\Ddl\DropTable;
6+
use PhpDb\Sql\TableIdentifier;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use PHPUnit\Framework\TestCase;
9+
10+
#[CoversMethod(DropTable::class, 'getSqlString')]
11+
final class DropTableTest extends TestCase
12+
{
13+
public function testGetSqlString(): void
14+
{
15+
$dt = new DropTable('foo');
16+
self::assertEquals('DROP TABLE "foo"', $dt->getSqlString());
17+
18+
$dt = new DropTable(new TableIdentifier('foo'));
19+
self::assertEquals('DROP TABLE "foo"', $dt->getSqlString());
20+
21+
$dt = new DropTable(new TableIdentifier('bar', 'foo'));
22+
self::assertEquals('DROP TABLE "foo"."bar"', $dt->getSqlString());
23+
}
24+
}

0 commit comments

Comments
 (0)