packages/sql-semantics-mysql/tests/Unit/Statement/Name/CurrentUserTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Name;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
15use SqlSemantics\Platform\MySql\Model\Rendering\Output;
16use SqlSemantics\Platform\MySql\Model\Rendering\PieceKind;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rendering\Codec;
19use SqlSemantics\Platform\MySql\Statement\Name\CurrentUser;
20
21#[CoversClass(CurrentUser::class)]
22#[Small]
23final class CurrentUserTest extends TestCase
24{
25 public function testRenderWritesTheKeyword(): void
26 {
27 $out = new Output(new Codec(GrammarRelease::MySql847));
28 (new CurrentUser())->render($out);
29 $pieces = $out->pieces();
30
31 self::assertCount(1, $pieces);
32 self::assertSame(PieceKind::Keyword, $pieces[0]->kind);
33 self::assertSame('CURRENT_USER', (new Lexical())->join($pieces));
34 }
35
36 public function testLoweredCurrentUserIsOneValueWithOrWithoutParentheses(): void
37 {
38 $platform = new Platform();
39 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
40 $parser = $platform->parser($profile);
41 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
42 $bare = $lowering->users->account($parser->parse('DROP USER CURRENT_USER')->find('user')[0]);
43 $called = $lowering->users->account($parser->parse('DROP USER current_user()')->find('user')[0]);
44
45 self::assertInstanceOf(CurrentUser::class, $bare);
46 self::assertInstanceOf(CurrentUser::class, $called);
47 }
48}
49