packages/sql-semantics-mysql/tests/Unit/Statement/Account/User/IdentificationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Account\User;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Account\User\Credential;
13use SqlSemantics\Platform\MySql\Statement\Account\User\Identification;
14use SqlSemantics\Platform\MySql\Statement\Literal\Text;
15
16#[CoversClass(Identification::class)]
17#[Medium]
18final class IdentificationTest extends TestCase
19{
20 public function testPasswordTellsADefaultPluginPassword(): void
21 {
22 self::assertTrue((new Identification(null, Credential::RandomPassword))->password());
23 self::assertFalse((new Identification(new Name('p'), Credential::Password, new Text('x')))->password());
24 }
25
26 public function testReplaceableTellsANewPassword(): void
27 {
28 self::assertTrue((new Identification(new Name('p'), Credential::Password, new Text('x')))->replaceable());
29 self::assertFalse((new Identification(new Name('p'), Credential::RandomPassword))->replaceable());
30 }
31
32 public function testRetainableTellsANewSecret(): void
33 {
34 self::assertTrue((new Identification(new Name('p'), Credential::Hash, new Text('h')))->retainable());
35 self::assertFalse((new Identification(new Name('p'), Credential::None))->retainable());
36 }
37
38 public function testInitialTellsAnInitialAuthenticationMethod(): void
39 {
40 self::assertTrue((new Identification(null, Credential::Password, new Text('x')))->initial());
41 self::assertFalse((new Identification(new Name('p'), Credential::Password, new Text('x')))->initial());
42 }
43
44 public function testRenderWritesEveryCredential(): void
45 {
46 self::assertSame("CREATE USER a IDENTIFIED WITH p BY RANDOM PASSWORD, b IDENTIFIED WITH p AS 'h', c IDENTIFIED WITH p BY 'x', d IDENTIFIED BY RANDOM PASSWORD", (new Semantics())->analyze("create user a identified with p by random password, b identified with 'p' as 'h', c identified with p by 'x', d identified by random password")->toString());
47 self::assertSame("GRANT USAGE ON *.* TO a IDENTIFIED BY PASSWORD 'h'", (new Semantics('mysql-5.7.44'))->analyze("grant usage on *.* to a identified by password 'h'")->toString());
48 }
49}
50