packages/ztd-query-core/tests/Unit/Rewrite/InsertRowProjectionPlannerTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rewrite;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use ZtdQuery\Rewrite\InsertRowProjection;
11use ZtdQuery\Rewrite\InsertRowProjectionPlanner;
12
13#[CoversClass(InsertRowProjectionPlanner::class)]
14#[UsesClass(InsertRowProjection::class)]
15final class InsertRowProjectionPlannerTest extends TestCase
16{
17    public function testPlansProvidedGeneratedDefaultsAndNulls(): void
18    {
19        $projections = (new InsertRowProjectionPlanner())->plan(
20            ['id', 'name', 'status', 'note'],
21            ['name' => "'Ada'"],
22            ['id' => '99', 'status' => "'active'"],
23            ['id' => 8],
24        );
25
26        self::assertCount(4, $projections);
27        self::assertSame(8, $projections[0]->generatedIdentityValue());
28        self::assertNull($projections[0]->defaultExpressionValue());
29        self::assertSame("'Ada'", $projections[1]->providedExpression());
30        self::assertSame("'active'", $projections[2]->defaultExpressionValue());
31        self::assertTrue($projections[3]->isNullValue());
32    }
33
34    public function testUsesProvidedColumnsWhenTableShapeIsUnavailable(): void
35    {
36        $projections = (new InsertRowProjectionPlanner())->plan([], ['id' => '1', 'name' => "'Ada'"], []);
37
38        self::assertCount(2, $projections);
39        self::assertSame('id', $projections[0]->targetColumn());
40        self::assertSame('1', $projections[0]->providedExpression());
41        self::assertSame('name', $projections[1]->targetColumn());
42    }
43}
44