|
6 | 6 |
|
7 | 7 | use PhpDb\Sql\Argument; |
8 | 8 | use PhpDb\Sql\Ddl\Column\Column; |
| 9 | +use PhpDb\Sql\Ddl\Column\ColumnInterface; |
9 | 10 | use PhpDb\Sql\Ddl\Column\Integer; |
10 | 11 | use PhpDb\Sql\Ddl\Constraint\PrimaryKey; |
| 12 | +use PhpDb\Sql\Ddl\CreateTable; |
| 13 | +use PhpDb\Sql\Exception\InvalidArgumentException; |
| 14 | +use PhpDbTest\TestAsset\TrustingSql92Platform; |
11 | 15 | use PHPUnit\Framework\Attributes\CoversMethod; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
12 | 17 | use PHPUnit\Framework\Attributes\Group; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
13 | 19 | use PHPUnit\Framework\TestCase; |
14 | 20 |
|
15 | 21 | #[CoversMethod(Integer::class, '__construct')] |
16 | 22 | #[CoversMethod(Integer::class, 'getExpressionData')] |
| 23 | +#[CoversMethod(Integer::class, 'normaliseDisplayWidth')] |
17 | 24 | #[CoversMethod(Column::class, 'getExpressionData')] |
18 | 25 | #[Group('unit')] |
19 | 26 | final class IntegerTest extends TestCase |
20 | 27 | { |
| 28 | + /** |
| 29 | + * @return array<string, array{bool|int|string}> |
| 30 | + */ |
| 31 | + public static function invalidLengthProvider(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'boolean' => [true], |
| 35 | + 'negative integer' => [-1], |
| 36 | + 'non-digit string' => ['abc'], |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Asserts what a column renders to inside CREATE TABLE, with values quoted. |
| 42 | + */ |
| 43 | + private static function assertColumnRenders(string $expected, ColumnInterface $column): void |
| 44 | + { |
| 45 | + $createTable = new CreateTable('t'); |
| 46 | + $createTable->addColumn($column); |
| 47 | + |
| 48 | + static::assertSame( |
| 49 | + "CREATE TABLE \"t\" ( \n {$expected} \n)", |
| 50 | + $createTable->getSqlString(new TrustingSql92Platform()), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function getExpressionDataPlacesLengthDirectlyAfterType(): void |
| 56 | + { |
| 57 | + $expressionData = (new Integer('i', false, null, ['length' => 11]))->getExpressionData(); |
| 58 | + |
| 59 | + static::assertSame('%s %s(%s) NOT NULL', $expressionData['spec']); |
| 60 | + static::assertEquals( |
| 61 | + [ |
| 62 | + Argument::identifier('i'), |
| 63 | + Argument::literal('INTEGER'), |
| 64 | + Argument::literal('11'), |
| 65 | + ], |
| 66 | + $expressionData['values'], |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[Test] |
| 71 | + public function rendersLengthBeforeNullabilityAndDefault(): void |
| 72 | + { |
| 73 | + static::assertColumnRenders( |
| 74 | + '"i" INTEGER(11) NULL DEFAULT \'7\'', |
| 75 | + new Integer('i', true, 7, ['length' => 11]), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + public function rendersLengthDirectlyAfterType(): void |
| 81 | + { |
| 82 | + static::assertColumnRenders('"i" INTEGER(11) NOT NULL', new Integer('i', false, null, ['length' => 11])); |
| 83 | + } |
| 84 | + |
| 85 | + #[Test] |
| 86 | + public function rendersLengthSetAsIntegerOption(): void |
| 87 | + { |
| 88 | + $column = new Integer('i'); |
| 89 | + $column->setOption('length', 11); |
| 90 | + |
| 91 | + static::assertColumnRenders('"i" INTEGER(11) NOT NULL', $column); |
| 92 | + } |
| 93 | + |
21 | 94 | public function testGetExpressionData(): void |
22 | 95 | { |
23 | 96 | $column = new Integer('foo'); |
@@ -64,12 +137,32 @@ public function testGetExpressionDataIncludesLengthWhenOptionSet(): void |
64 | 137 |
|
65 | 138 | $expressionData = $column->getExpressionData(); |
66 | 139 |
|
67 | | - self::assertStringContainsString('(11)', $expressionData['spec']); |
| 140 | + static::assertSame('%s %s(%s) NOT NULL', $expressionData['spec']); |
| 141 | + static::assertEquals( |
| 142 | + [ |
| 143 | + Argument::identifier('id'), |
| 144 | + Argument::literal('INTEGER'), |
| 145 | + Argument::literal('11'), |
| 146 | + ], |
| 147 | + $expressionData['values'], |
| 148 | + ); |
68 | 149 | } |
69 | 150 |
|
70 | 151 | public function testObjectConstruction(): void |
71 | 152 | { |
72 | 153 | $integer = new Integer('foo'); |
73 | 154 | self::assertEquals('foo', $integer->getName()); |
74 | 155 | } |
| 156 | + |
| 157 | + #[Test] |
| 158 | + #[DataProvider('invalidLengthProvider')] |
| 159 | + public function throwsWhenLengthOptionIsNotANonNegativeInteger(bool|int|string $length): void |
| 160 | + { |
| 161 | + $column = new Integer('i', false, null, ['length' => $length]); |
| 162 | + |
| 163 | + $this->expectException(InvalidArgumentException::class); |
| 164 | + $this->expectExceptionMessage('Column "i" length option must be a non-negative integer'); |
| 165 | + |
| 166 | + $column->getExpressionData(); |
| 167 | + } |
75 | 168 | } |
0 commit comments