packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Operator/BooleanOperationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Operator;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\BooleanOperation;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\BooleanOperator;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\OperandMismatch;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
28
29#[CoversClass(BooleanOperation::class)]
30#[Small]
31final class BooleanOperationTest extends TestCase
32{
33 public function testDeriveScalarIsBoolean(): void
34 {
35 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
36 $fact = $derivation->scalar(new BooleanOperation(BooleanOperator::And, new BooleanLiteral(true), new NullLiteral()), $derivation->environment());
37 self::assertEquals(new Known(Builtin::Bool), $fact->type);
38 self::assertSame(Nullability::Nullable, $fact->nullability);
39 }
40
41 public function testDeriveScalarReportsANonBooleanOperand(): void
42 {
43 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
44 $fact = $derivation->scalar(new BooleanOperation(BooleanOperator::Or, new BooleanLiteral(true), new Constant(new IntegerConstant('1'))), $derivation->environment());
45 self::assertInstanceOf(Invalid::class, $fact->type);
46 self::assertInstanceOf(OperandMismatch::class, $fact->type->cause);
47 }
48
49 public function testRenderWritesTheOperator(): void
50 {
51 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52 $and = new BooleanOperation(BooleanOperator::And, new BooleanLiteral(false), new NullLiteral());
53 (new BooleanOperation(BooleanOperator::Or, new BooleanLiteral(true), $and))->render($out);
54 self::assertSame('TRUE OR FALSE AND NULL', (new Lexical())->join($out->pieces()));
55 }
56
57 public function testRejectsADisjunctionInsideAConjunction(): void
58 {
59 $or = new BooleanOperation(BooleanOperator::Or, new BooleanLiteral(true), new NullLiteral());
60 $this->expectExceptionMessage('The left operand needs parentheses to keep its place.');
61 new BooleanOperation(BooleanOperator::And, $or, new NullLiteral());
62 }
63}
64