packages/sql-semantics-mysql/tests/Unit/Statement/Expression/NotTest.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\Mode;
11use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\MySql\Model\Rendering\Output;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
18use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
19use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
20use SqlSemantics\Platform\MySql\Statement\Expression\LogicalOperator;
21use SqlSemantics\Platform\MySql\Statement\Expression\Not;
22use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
23use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
24
25#[CoversClass(Not::class)]
26#[Medium]
27final class NotTest extends TestCase
28{
29    public function testDeriveScalarIsNullWhenTheOperandIs(): void
30    {
31        $platform = new Platform();
32        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
33
34        self::assertSame(Nullability::Nullable, $derivation->scalar(new Not(new NullLiteral()), $derivation->environment())->nullability);
35        self::assertSame(Nullability::NotNull, $derivation->scalar(new Not(new NumberLiteral('1')), $derivation->environment())->nullability);
36    }
37
38    public function testDeriveScalarRejectsTheLowNegationUnderHighNotPrecedence(): void
39    {
40        $platform = new Platform();
41        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', Mode::fromString('HIGH_NOT_PRECEDENCE'), ParameterStyle::Native), null, [], true));
42
43        $this->expectExceptionMessage('Under HIGH_NOT_PRECEDENCE, NOT is the tight negation; use the unary NOT.');
44
45        $derivation->scalar(new Not(new NumberLiteral('1')), $derivation->environment());
46    }
47
48    public function testRenderNegatesAComparisonWithoutParentheses(): void
49    {
50        $platform = new Platform();
51        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
52        (new Not(new Comparison(ComparisonOperator::Equal, new NumberLiteral('1'), new NumberLiteral('2'))))->render($out);
53
54        self::assertSame('NOT 1 = 2', (new Lexical())->join($out->pieces()));
55    }
56
57    public function testAConjunctionOperandIsRejected(): void
58    {
59        $this->expectExceptionMessage('The operand of NOT needs a grouping to keep its place.');
60
61        new Not(new Logical(LogicalOperator::And, new NumberLiteral('1'), new NumberLiteral('2')));
62    }
63}
64