packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Predicate/SoundsLikeTest.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\Regexp;
17use SqlSemantics\Platform\MySql\Statement\Expression\Predicate\SoundsLike;
18use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
19
20#[CoversClass(SoundsLike::class)]
21#[Medium]
22final class SoundsLikeTest extends TestCase
23{
24 public function testDeriveScalarIsAnIntegerTruthValue(): void
25 {
26 $platform = new Platform();
27 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
28 $fact = $derivation->scalar(new SoundsLike(new StringLiteral(['Smith']), new StringLiteral(['Smyth'])), $derivation->environment());
29
30 self::assertSame(Nullability::NotNull, $fact->nullability);
31 }
32
33 public function testRenderWritesBothWords(): void
34 {
35 $platform = new Platform();
36 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
37 (new SoundsLike(new StringLiteral(['a']), new StringLiteral(['b'])))->render($out);
38
39 self::assertSame("'a' SOUNDS LIKE 'b'", (new Lexical())->join($out->pieces()));
40 }
41
42 public function testAPredicateOperandIsRejected(): void
43 {
44 $this->expectExceptionMessage('The left operand of SOUNDS LIKE needs a grouping to keep its place.');
45
46 new SoundsLike(new Regexp(new StringLiteral(['a']), new StringLiteral(['b'])), new StringLiteral(['c']));
47 }
48}
49