packages/sql-parser/tests/Unit/Resource/VersionRegistryTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Resource;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use RuntimeException;
12use SqlParser\Resource\SqlVersion;
13use SqlParser\Resource\VersionRegistry;
14
15#[CoversClass(VersionRegistry::class)]
16#[UsesClass(SqlVersion::class)]
17#[Small]
18final class VersionRegistryTest extends TestCase
19{
20    public function testResolveDefaultsToTheNewestRelease(): void
21    {
22        $version = (new VersionRegistry())->resolve('mysql');
23
24        self::assertSame('mysql-8.4.7', $version->name);
25        self::assertStringEndsWith('/resources/tables/mysql-8.4.7.bin', $version->tablePath);
26        self::assertStringEndsWith('/resources/keywords/mysql-8.4.7.php', $version->keywordPath);
27        self::assertFileExists($version->tablePath);
28    }
29
30    public function testResolveAcceptsAnExplicitRelease(): void
31    {
32        self::assertSame('sqlite-3.47.2', (new VersionRegistry())->resolve('sqlite', 'sqlite-3.47.2')->name);
33    }
34
35    public function testResolveRejectsAnUnknownDialect(): void
36    {
37        $this->expectException(RuntimeException::class);
38        $this->expectExceptionMessage('Unknown SQL dialect: oracle');
39
40        (new VersionRegistry())->resolve('oracle');
41    }
42
43    public function testResolveRejectsAnUnknownRelease(): void
44    {
45        $this->expectException(RuntimeException::class);
46        $this->expectExceptionMessage('Unsupported postgresql version: pg-9.6');
47
48        (new VersionRegistry())->resolve('postgresql', 'pg-9.6');
49    }
50
51    public function testNames(): void
52    {
53        $names = (new VersionRegistry())->names('mysql');
54
55        self::assertSame('mysql-5.6.51', $names[0]);
56        self::assertContains('mysql-9.1.0', $names);
57        self::assertSame(['pg-17.2'], (new VersionRegistry())->names('postgresql'));
58    }
59
60    public function testNamesRejectsAnUnknownDialect(): void
61    {
62        $this->expectException(RuntimeException::class);
63
64        (new VersionRegistry())->names('oracle');
65    }
66
67    public function testEntries(): void
68    {
69        $entries = (new VersionRegistry())->entries();
70
71        self::assertSame(['mysql', 'postgresql', 'sqlite'], array_keys($entries));
72        self::assertSame('tables/sqlite-3.47.2.bin', $entries['sqlite']['versions']['sqlite-3.47.2']['table']);
73    }
74
75    public function testPath(): void
76    {
77        self::assertSame('/res/tables/x.bin', (new VersionRegistry('/res'))->path('tables/x.bin'));
78    }
79
80    public function testPathRejectsAPathThatClimbsOut(): void
81    {
82        $this->expectException(RuntimeException::class);
83
84        (new VersionRegistry('/res'))->path('../etc/passwd');
85    }
86
87    public function testPathRejectsAnAbsolutePath(): void
88    {
89        $this->expectException(RuntimeException::class);
90
91        (new VersionRegistry('/res'))->path('/etc/passwd');
92    }
93
94    public function testDirectory(): void
95    {
96        self::assertSame('/res', (new VersionRegistry('/res'))->directory());
97        self::assertStringEndsWith('/resources', (new VersionRegistry())->directory());
98    }
99}
100