packages/sql-semantics-mysql/tests/Unit/Statement/Query/Locking/LockingClauseTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Locking;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
11use SqlSemantics\Platform\MySql\Model\Rendering\Output;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Rendering\Codec;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Expression\OptionalWords;
17use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockedRowAction;
18use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockingClause;
19use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockStrength;
20
21#[CoversClass(LockingClause::class)]
22#[Medium]
23final class LockingClauseTest extends TestCase
24{
25 public function testRenderWritesTheClauses(): void
26 {
27 $semantics = new Semantics();
28 $clause = new LockingClause(LockStrength::Update, [new QualifiedName(new Name('t')), new QualifiedName(new Name('u'), new Name('db'))], LockedRowAction::Nowait);
29 $out = new Output(new Codec($semantics->profile()->grammar));
30 $clause->render($out);
31
32 self::assertSame('FOR UPDATE OF t, db.u NOWAIT', (new Lexical())->join($out->pieces()));
33 self::assertSame('SELECT a FROM t FOR SHARE SKIP LOCKED LOCK IN SHARE MODE', $semantics->analyze('select a from t for share skip locked lock in share mode')->toString());
34 self::assertSame('SELECT a FROM t FOR UPDATE', (new Semantics('mysql-5.6.51'))->analyze('select a from t for update')->toString());
35 }
36
37 public function testATableListOnShareModeIsRejected(): void
38 {
39 $this->expectExceptionMessage('LOCK IN SHARE MODE takes no table list and no action.');
40
41 new LockingClause(LockStrength::ShareMode, [], LockedRowAction::Nowait);
42 }
43
44 public function testACatalogQualifiedTableIsRejected(): void
45 {
46 $this->expectExceptionMessage('A table is qualified by at most a database.');
47
48 new LockingClause(LockStrength::Update, [new QualifiedName(new Name('t'), new Name('db'), new Name('c'))]);
49 }
50
51 public function testRenderWritesTheStarOfATableWrittenWithIt(): void
52 {
53 $clause = new LockingClause(LockStrength::Share, [new QualifiedName(new Name('t')), new QualifiedName(new Name('u'))], null, [OptionalWords::Written, OptionalWords::Omitted]);
54 $out = new Output(new Codec((new Semantics())->profile()->grammar));
55 $clause->render($out);
56
57 self::assertSame('FOR SHARE OF t.*, u', (new Lexical())->join($out->pieces()));
58 self::assertSame([OptionalWords::Omitted], (new LockingClause(LockStrength::Update, [new QualifiedName(new Name('t'))]))->wildcards);
59 }
60
61 public function testAWildcardListOfAnotherLengthIsRejected(): void
62 {
63 $this->expectExceptionMessage('A locking clause has one wildcard form per table.');
64
65 new LockingClause(LockStrength::Update, [new QualifiedName(new Name('t'))], null, [OptionalWords::Written, OptionalWords::Written]);
66 }
67}
68