packages/sql-semantics-mysql/tests/Unit/Rules/Expression/OperandsTest.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\ScalarFact;
13use SqlSemantics\Platform\MySql\Model\Statement\Reference\Missing\SessionState;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Invalid;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\MySql\Model\Statement\Type\NullOnly;
19use SqlSemantics\Platform\MySql\Platform;
20use SqlSemantics\Platform\MySql\Rules\Expression\Operands;
21use SqlSemantics\Platform\MySql\Statement\Expression\Tuple;
22use SqlSemantics\Platform\MySql\Statement\Type\Integral;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
24
25#[CoversClass(Operands::class)]
26#[Medium]
27final class OperandsTest extends TestCase
28{
29 public function testWidthCountsTheColumnsOfAKnownValue(): void
30 {
31 $operands = new Operands();
32
33 self::assertSame([2, 1, null], [
34 $operands->width(new ScalarFact(new Known(new Tuple(2)), Nullability::NotNull)),
35 $operands->width(new ScalarFact(new NullOnly(), Nullability::Nullable)),
36 $operands->width(new ScalarFact(new Dependent([new SessionState('x')]), Nullability::Dependent)),
37 ]);
38 }
39
40 public function testSingleReportsARow(): void
41 {
42 $platform = new Platform();
43 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
44 (new Operands())->single(new ScalarFact(new Known(new Tuple(3)), Nullability::NotNull), $derivation);
45
46 self::assertSame('Operand should contain 1 column(s), not 3.', $derivation->facts()->diagnostics[0]->message());
47 }
48
49 public function testSingleAnswersAValueWithoutTypeForARowAndTheFactOtherwise(): void
50 {
51 $platform = new Platform();
52 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
53 $one = new ScalarFact(new Known(new Integral(IntegralKind::Int)), Nullability::Nullable);
54 $row = (new Operands())->single(new ScalarFact(new Known(new Tuple(2)), Nullability::Nullable), $derivation);
55
56 self::assertSame($one, (new Operands())->single($one, $derivation));
57 self::assertInstanceOf(Invalid::class, $row->type);
58 self::assertSame($derivation->facts()->diagnostics[0], $row->type->cause);
59 self::assertSame(Nullability::Nullable, $row->nullability);
60 }
61
62 public function testComparableReportsTheFirstDifferentWidth(): void
63 {
64 $platform = new Platform();
65 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
66 (new Operands())->comparable([new ScalarFact(new Known(new Tuple(2)), Nullability::NotNull), new ScalarFact(new NullOnly(), Nullability::Nullable), new ScalarFact(new Known(new Tuple(3)), Nullability::NotNull)], $derivation);
67
68 self::assertCount(1, $derivation->facts()->diagnostics);
69 self::assertSame('Operand should contain 2 column(s), not 1.', $derivation->facts()->diagnostics[0]->message());
70 }
71
72 public function testTruthIsAnInteger(): void
73 {
74 self::assertEquals(new ScalarFact(new Known(new Integral(IntegralKind::BigInt)), Nullability::Dependent), (new Operands())->truth(Nullability::Dependent));
75 }
76}
77