packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Predicate/MemberOfTest.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\Operator\Arithmetic;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\ArithmeticOperator;
18use SqlSemantics\Platform\MySql\Statement\Expression\Predicate\MemberOf;
19use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
20use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
22
23#[CoversClass(MemberOf::class)]
24#[Medium]
25final class MemberOfTest extends TestCase
26{
27 public function testDeriveScalarIsNullWhenTheArrayCanBe(): void
28 {
29 $platform = new Platform();
30 $derivation = new Derivation($platform->context($platform->profile('mysql-8.0.44', null, ParameterStyle::Native), null, [], true));
31
32 self::assertSame(Nullability::Nullable, $derivation->scalar(new MemberOf(new NumberLiteral('1'), new NullLiteral()), $derivation->environment())->nullability);
33 }
34
35 public function testDeriveScalarRejectsTheTestBeforeMySql80(): void
36 {
37 $platform = new Platform();
38 $derivation = new Derivation($platform->context($platform->profile('mysql-5.7.44', null, ParameterStyle::Native), null, [], true));
39
40 $this->expectExceptionMessage('MEMBER OF needs MySQL 8.0 or later.');
41
42 $derivation->scalar(new MemberOf(new NumberLiteral('1'), new StringLiteral(['[1]'])), $derivation->environment());
43 }
44
45 public function testRenderWritesOfAndTheArrayInParentheses(): void
46 {
47 $platform = new Platform();
48 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
49 (new MemberOf(new NumberLiteral('1'), new StringLiteral(['[1]'])))->render($out);
50
51 self::assertSame("1 MEMBER OF ('[1]')", (new Lexical())->join($out->pieces()));
52 }
53
54 public function testAnArithmeticArrayIsRejected(): void
55 {
56 $this->expectExceptionMessage('The array of MEMBER OF needs a grouping to keep its place.');
57
58 new MemberOf(new NumberLiteral('1'), new Arithmetic(ArithmeticOperator::Plus, new NumberLiteral('1'), new NumberLiteral('2')));
59 }
60}
61