packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Predicate/InListTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Predicate;
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\Platform;
16use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
17use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
18use SqlSemantics\Platform\MySql\Statement\Expression\Predicate\InList;
19use SqlSemantics\Platform\MySql\Statement\Expression\Row;
20use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
23
24#[CoversClass(InList::class)]
25#[Medium]
26final class InListTest extends TestCase
27{
28 public function testDeriveScalarIsNullWhenAnElementCanBe(): void
29 {
30 $platform = new Platform();
31 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
32
33 self::assertSame(Nullability::Nullable, $derivation->scalar(new InList(new NumberLiteral('1'), [new NumberLiteral('2'), new NullLiteral()]), $derivation->environment())->nullability);
34 self::assertSame([], $derivation->facts()->diagnostics);
35 }
36
37 public function testDeriveScalarReportsAnElementOfAnotherWidth(): void
38 {
39 $platform = new Platform();
40 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
41 $derivation->scalar(new InList(new Row([new NumberLiteral('1'), new NumberLiteral('2')]), [new NumberLiteral('3')]), $derivation->environment());
42
43 self::assertSame('Operand should contain 2 column(s), not 1.', $derivation->facts()->diagnostics[0]->message());
44 }
45
46 public function testRenderWritesTheNegatedList(): void
47 {
48 $platform = new Platform();
49 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
50 (new InList(new NumberLiteral('1'), [new NumberLiteral('2'), new StringLiteral(['x'])], true))->render($out);
51
52 self::assertSame("1 NOT IN (2, 'x')", (new Lexical())->join($out->pieces()));
53 }
54
55 public function testAComparisonOperandIsRejected(): void
56 {
57 $this->expectExceptionMessage('The operand of IN needs a grouping to keep its place.');
58
59 new InList(new Comparison(ComparisonOperator::Equal, new NumberLiteral('1'), new NumberLiteral('2')), [new NumberLiteral('3')]);
60 }
61
62 public function testAnEmptyListIsRejected(): void
63 {
64 $this->expectExceptionMessage('An IN list has at least one element.');
65
66 new InList(new NumberLiteral('1'), []);
67 }
68}
69