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