packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Access/FullTextSearchTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Access;
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\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Statement\Expression\Access\FullTextMode;
19use SqlSemantics\Platform\MySql\Statement\Expression\Access\FullTextSearch;
20use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
21use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Interval;
22use SqlSemantics\Platform\MySql\Statement\Expression\Operator\IntervalAddition;
23use SqlSemantics\Platform\MySql\Statement\Expression\Predicate\InList;
24use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
25use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
26use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
27use SqlSemantics\Platform\MySql\Statement\Type\Floating;
28use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
29
30#[CoversClass(FullTextSearch::class)]
31#[Medium]
32final class FullTextSearchTest extends TestCase
33{
34    public function testDeriveScalarIsADoubleRelevance(): void
35    {
36        $platform = new Platform();
37        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
38        $fact = $derivation->scalar(new FullTextSearch([new ColumnUse(new Name('a'))], new StringLiteral(['x'])), $derivation->environment());
39
40        self::assertEquals(new Known(new Floating(FloatingKind::Double)), $fact->type);
41        self::assertSame(Nullability::Nullable, $fact->nullability);
42    }
43
44    public function testRenderWritesTheColumnsInParenthesesAndTheMode(): void
45    {
46        $platform = new Platform();
47        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
48        (new FullTextSearch([new ColumnUse(new Name('a')), new ColumnUse(new Name('b'))], new StringLiteral(['x']), FullTextMode::Boolean))->render($out);
49
50        self::assertSame("MATCH (a, b) AGAINST ('x' IN BOOLEAN MODE)", (new Lexical())->join($out->pieces()));
51    }
52
53    public function testAPredicateSearchStringIsRejected(): void
54    {
55        $this->expectExceptionMessage('The search string of AGAINST needs a grouping to keep its place.');
56
57        new FullTextSearch([new ColumnUse(new Name('a'))], new InList(new NumberLiteral('1'), [new NumberLiteral('2')]));
58    }
59
60    public function testASearchStringThatTakesTheBooleanModeIsRejected(): void
61    {
62        $this->expectExceptionMessage('A search string followed by IN ... MODE needs a grouping to keep its place.');
63
64        new FullTextSearch([new ColumnUse(new Name('a'))], new IntervalAddition(new Interval(new NumberLiteral('1'), IntervalUnit::Day), new NumberLiteral('2')), FullTextMode::Boolean);
65    }
66}
67