packages/sql-semantics-mysql/tests/Unit/Rules/Expression/SubqueryRowsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Statement\Fact\QueryFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Comparison;
15use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\SessionState;
17use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
18use SqlSemantics\Platform\MySql\Model\Statement\Shape\OpenStar;
19use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
21use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
22use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
23use SqlSemantics\Platform\MySql\Platform;
24use SqlSemantics\Platform\MySql\Rules\Expression\SubqueryRows;
25use SqlSemantics\Platform\MySql\Statement\Expression\Tuple;
26use SqlSemantics\Platform\MySql\Statement\Type\Integral;
27use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
28
29#[CoversClass(SubqueryRows::class)]
30#[Medium]
31final class SubqueryRowsTest extends TestCase
32{
33 public function testValueIsTheOneColumnOrARow(): void
34 {
35 $integer = new Known(new Integral(IntegralKind::Int));
36 $one = new QueryFact([new Field(0, new OutputSlot(new Name('a'), $integer, Nullability::NotNull))], Comparison::AsciiInsensitive);
37 $two = new QueryFact([new Field(0, new OutputSlot(new Name('a'), $integer, Nullability::NotNull)), new Field(1, new OutputSlot(new Name('b'), $integer, Nullability::NotNull))], Comparison::AsciiInsensitive);
38 $open = new QueryFact([new OpenStar([new SessionState('x')])], Comparison::AsciiInsensitive);
39 $rows = new SubqueryRows();
40
41 self::assertEquals(new ScalarFact($integer, Nullability::Nullable), $rows->value($one));
42 self::assertEquals(new Known(new Tuple(2)), $rows->value($two)->type);
43 self::assertInstanceOf(Dependent::class, $rows->value($open)->type);
44 }
45
46 public function testTestCombinesTheColumnsAndReportsAWidthMismatch(): void
47 {
48 $platform = new Platform();
49 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
50 $integer = new Known(new Integral(IntegralKind::Int));
51 $two = new QueryFact([new Field(0, new OutputSlot(new Name('a'), $integer, Nullability::NotNull)), new Field(1, new OutputSlot(new Name('b'), $integer, Nullability::Nullable))], Comparison::AsciiInsensitive);
52 $nullability = (new SubqueryRows())->test(new ScalarFact($integer, Nullability::NotNull), $two, $derivation);
53
54 self::assertSame(Nullability::Nullable, $nullability);
55 self::assertSame('Operand should contain 1 column(s), not 2.', $derivation->facts()->diagnostics[0]->message());
56 }
57}
58