packages/sql-semantics-mysql/tests/Unit/Statement/Expression/GroupedTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\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\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\NullOnly;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Grouped;
18use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
19use SqlSemantics\Platform\MySql\Statement\Expression\LogicalOperator;
20use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22
23#[CoversClass(Grouped::class)]
24#[Medium]
25final class GroupedTest extends TestCase
26{
27 public function testDeriveScalarHasTheFactsOfTheOperand(): void
28 {
29 $platform = new Platform();
30 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
31 $fact = $derivation->scalar(new Grouped(new NullLiteral()), $derivation->environment());
32
33 self::assertInstanceOf(NullOnly::class, $fact->type);
34 self::assertSame(Nullability::Nullable, $fact->nullability);
35 }
36
37 public function testRenderLetsAWeakerOperandStandOnTheRight(): void
38 {
39 $platform = new Platform();
40 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
41 (new Logical(LogicalOperator::And, new NumberLiteral('1'), new Grouped(new Logical(LogicalOperator::Or, new NumberLiteral('2'), new NumberLiteral('3')))))->render($out);
42
43 self::assertSame('1 AND (2 OR 3)', (new Lexical())->join($out->pieces()));
44 }
45}
46