packages/sql-faker/tests/Unit/MySql/Generation/Rewrite/Column/AutoIncrementRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\MySql\Generation\Rewrite\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlFaker\Generation\Token\ProductionOccurrence;
12use SqlFaker\Generation\Token\TerminalOccurrence;
13use SqlFaker\Generation\Token\TerminalSequence;
14use SqlFaker\MySql\Generation\Rewrite\Column\AutoIncrementRule;
15
16#[CoversClass(AutoIncrementRule::class)]
17#[UsesClass(ProductionOccurrence::class)]
18#[UsesClass(TerminalOccurrence::class)]
19#[UsesClass(TerminalSequence::class)]
20final class AutoIncrementRuleTest extends TestCase
21{
22 /**
23 * @param list<string> $attributeNames
24 */
25 #[DataProvider('providerTypes')]
26 public function testRewriteRetainsOnlyTypeCompatibleAttributes(string $name, bool $allowed, array $attributeNames): void
27 {
28 $type = new TerminalOccurrence($name, 10, [0, 1], ['field_def', 'type']);
29 $attributes = array_map(static fn (string $attribute, int $id): TerminalOccurrence => new TerminalOccurrence($attribute, 11 + $id, [0, 2], ['field_def', 'column_attribute']), $attributeNames, array_keys($attributeNames));
30 $input = new TerminalSequence([$type, ...$attributes], productions: [new ProductionOccurrence(0, null, 'field_def', 0), new ProductionOccurrence(1, 0, 'type', 0), new ProductionOccurrence(2, 0, 'column_attribute', 0)]);
31 $result = (new AutoIncrementRule())->rewrite($input);
32 self::assertSame($allowed ? [$name, ...array_column($attributes, 'name')] : [$name], $result->names());
33 self::assertSame($input->original, $result->original);
34 self::assertSame($input->productions, $result->productions);
35 self::assertSame($type, $result->terminals[0]);
36 self::assertSame($result, (new AutoIncrementRule())->rewrite($result));
37 }
38
39 /**
40 * @return iterable<array{string, bool, list<string>}>
41 */
42 public static function providerTypes(): iterable
43 {
44 foreach (['INT_SYM', 'TINYINT_SYM', 'SMALLINT_SYM', 'MEDIUMINT_SYM', 'BIGINT_SYM', 'BOOL_SYM', 'BOOLEAN_SYM'] as $name) {
45 yield [$name, true, ['AUTO_INC']];
46 yield [$name, true, ['SERIAL_SYM', 'DEFAULT_SYM', 'VALUE_SYM']];
47 }
48 foreach (['BIT_SYM', 'FLOAT_SYM', 'DOUBLE_SYM', 'DECIMAL_SYM', 'CHAR_SYM', 'BINARY_SYM', 'TINYBLOB_SYM', 'YEAR_SYM'] as $name) {
49 yield [$name, false, ['AUTO_INC']];
50 yield [$name, false, ['SERIAL_SYM', 'DEFAULT_SYM', 'VALUE_SYM']];
51 }
52 }
53
54 public function testRewritePreservesAttributesWithoutAnOwningType(): void
55 {
56 $input = new TerminalSequence([new TerminalOccurrence('AUTO_INC', 10, [0], ['column_attribute'])], productions: [new ProductionOccurrence(0, null, 'column_attribute', 0)]);
57 self::assertSame($input, (new AutoIncrementRule())->rewrite($input));
58 }
59}
60