packages/sql-semantics-mysql/tests/Unit/Statement/Name/AccountNameTest.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\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\MySql\Platform;
19use SqlSemantics\Platform\MySql\Rendering\Codec;
20use SqlSemantics\Platform\MySql\Statement\Name\AccountName;
21
22#[CoversClass(AccountName::class)]
23#[Small]
24final class AccountNameTest extends TestCase
25{
26 public function testRenderWritesTheUserAndTheHostJoinedByAtWithoutSpaces(): void
27 {
28 $out = new Output(new Codec(GrammarRelease::MySql847));
29 (new AccountName(new Name('bob'), new Name('localhost')))->render($out);
30 $pieces = $out->pieces();
31
32 self::assertSame([PieceKind::Name, PieceKind::Symbol, PieceKind::Name], array_column($pieces, 'kind'));
33 self::assertSame([false, true, true], array_column($pieces, 'glued'));
34 self::assertSame('bob@localhost', (new Lexical())->join($pieces));
35 }
36
37 public function testRenderQuotesAPartThatNeedsQuoting(): void
38 {
39 $out = new Output(new Codec(GrammarRelease::MySql847));
40 (new AccountName(new Name('bo b'), new Name('10.0.%')))->render($out);
41
42 self::assertSame('`bo b`@`10.0.%`', (new Lexical())->join($out->pieces()));
43 }
44
45 public function testRenderOmitsAnAbsentHost(): void
46 {
47 $out = new Output(new Codec(GrammarRelease::MySql847));
48 (new AccountName(new Name('bob')))->render($out);
49
50 self::assertSame('bob', (new Lexical())->join($out->pieces()));
51 }
52
53 public function testLoweredAccountHoldsTheDecodedParts(): void
54 {
55 $platform = new Platform();
56 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
57 $parser = $platform->parser($profile);
58 $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
59 $quoted = $lowering->users->account($parser->parse("DROP USER 'bo b'@'10.0.%'")->find('user')[0]);
60 $bare = $lowering->users->account($parser->parse('DROP USER bob')->find('user')[0]);
61 $role = $lowering->users->account($parser->parse('DROP ROLE r1@h')->find('role')[0]);
62
63 self::assertInstanceOf(AccountName::class, $quoted);
64 self::assertSame('bo b', $quoted->user->value);
65 self::assertSame('10.0.%', $quoted->host?->value);
66 self::assertInstanceOf(AccountName::class, $bare);
67 self::assertSame('bob', $bare->user->value);
68 self::assertNull($bare->host);
69 self::assertInstanceOf(AccountName::class, $role);
70 self::assertSame('r1', $role->user->value);
71 self::assertSame('h', $role->host?->value);
72 }
73}
74