packages/sql-semantics-mysql/tests/Unit/Statement/Relation/Hint/IndexHintTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation\Hint;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Semantics;
11use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
12use SqlSemantics\Platform\MySql\Statement\Relation\Hint\IndexHint;
13use SqlSemantics\Platform\MySql\Statement\Relation\Hint\IndexHintAction;
14use SqlSemantics\Platform\MySql\Statement\Relation\Hint\IndexHintScope;
15
16#[CoversClass(IndexHint::class)]
17#[Medium]
18final class IndexHintTest extends TestCase
19{
20 public function testRenderWritesTheHints(): void
21 {
22 self::assertSame('SELECT a FROM t USE INDEX () FORCE INDEX FOR JOIN (i) IGNORE INDEX FOR ORDER BY (j, k)', (new Semantics())->analyze('select a from t use index () force key for join (i) ignore index for order by (j, k)')->toString());
23 self::assertSame('SELECT a FROM t x USE INDEX FOR GROUP BY (i)', (new Semantics('mysql-5.6.51'))->analyze('select a from t x use index for group by (i)')->toString());
24 }
25
26 public function testForceWithoutIndexIsRejected(): void
27 {
28 $this->expectExceptionMessage('FORCE INDEX and IGNORE INDEX name at least one index.');
29
30 new IndexHint(IndexHintAction::Ignore, IndexHintScope::Join, []);
31 }
32
33 public function testAnotherIndexValueIsRejected(): void
34 {
35 $this->expectExceptionMessage('An index is named or is the primary key.');
36
37 new IndexHint(IndexHintAction::Use, null, [new Dual()]);
38 }
39}
40