packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Operator/BetweenTest.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\Identifier\Name;
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\BinaryOperation;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\Between;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\NullTest;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
26use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
27use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
28
29#[CoversClass(Between::class)]
30#[Small]
31final class BetweenTest 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 Between(new Constant(new IntegerConstant('2')), false, false, new Constant(new IntegerConstant('1')), 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 testRenderWritesNotAndSymmetric(): void
42 {
43 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
44 (new Between(new NullLiteral(), true, true, new NullLiteral(), new NullLiteral()))->render($out);
45 self::assertSame('NULL NOT BETWEEN SYMMETRIC NULL AND NULL', (new Lexical())->join($out->pieces()));
46 }
47
48 public function testRejectsALowBoundOutsideBExpr(): void
49 {
50 $this->expectExceptionMessage('The low bound of BETWEEN needs parentheses: it is a b_expr.');
51 new Between(new NullLiteral(), false, false, new NullTest(new NullLiteral(), false), new NullLiteral());
52 }
53
54 public function testRejectsATestedComparison(): void
55 {
56 $comparison = new BinaryOperation(new OperatorName(new Name('=')), new NullLiteral(), new NullLiteral());
57 $this->expectExceptionMessage('The tested value needs parentheses to keep its place.');
58 new Between($comparison, false, false, new NullLiteral(), new NullLiteral());
59 }
60}
61