packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Predicate/RegexpTest.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\Predicate\InList;
17use SqlSemantics\Platform\MySql\Statement\Expression\Predicate\Regexp;
18use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
19use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
20use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
21
22#[CoversClass(Regexp::class)]
23#[Medium]
24final class RegexpTest extends TestCase
25{
26 public function testDeriveScalarIsNullWhenThePatternCanBe(): void
27 {
28 $platform = new Platform();
29 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
30
31 self::assertSame(Nullability::Nullable, $derivation->scalar(new Regexp(new StringLiteral(['a']), new NullLiteral()), $derivation->environment())->nullability);
32 }
33
34 public function testRenderWritesTheKeyword(): void
35 {
36 $platform = new Platform();
37 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
38 (new Regexp(new StringLiteral(['a']), new StringLiteral(['^a']), true))->render($out);
39
40 self::assertSame("'a' NOT REGEXP '^a'", (new Lexical())->join($out->pieces()));
41 }
42
43 public function testAPredicatePatternIsRejected(): void
44 {
45 $this->expectExceptionMessage('The pattern of REGEXP needs a grouping to keep its place.');
46
47 new Regexp(new StringLiteral(['a']), new InList(new NumberLiteral('1'), [new NumberLiteral('2')]));
48 }
49}
50