|
| 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 | +} |
0 commit comments