Merge pull request #19552 from kamil-tekiela/Fix-default-values
[phpmyadmin.git] / tests / unit / LinterTest.php
blob4201a95ced887a7692a71068925e1d0dba4cee3d
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Linter;
8 use PHPUnit\Framework\Attributes\CoversClass;
9 use PHPUnit\Framework\Attributes\DataProvider;
11 use function str_repeat;
13 #[CoversClass(Linter::class)]
14 class LinterTest extends AbstractTestCase
16 /**
17 * Sets up the fixture, for example, opens a network connection.
18 * This method is called before a test is executed.
20 protected function setUp(): void
22 parent::setUp();
24 $this->setLanguage();
27 /**
28 * Test for Linter::getLines
30 public function testGetLines(): void
32 self::assertSame([0], Linter::getLines(''));
33 self::assertSame([0, 2], Linter::getLines("a\nb"));
34 self::assertSame([0, 4, 7], Linter::getLines("abc\nde\n"));
37 /**
38 * Test for Linter::findLineNumberAndColumn
40 public function testFindLineNumberAndColumn(): void
42 // Let the analyzed string be:
43 // ^abc$
44 // ^de$
45 // ^$
47 // Where `^` is the beginning of the line and `$` the end of the line.
49 // Positions of each character (by line):
50 // ( a, 0), ( b, 1), ( c, 2), (\n, 3),
51 // ( d, 4), ( e, 5), (\n, 6),
52 // (\n, 7).
53 self::assertSame(
54 [1, 0],
55 Linter::findLineNumberAndColumn([0, 4, 7], 4),
57 self::assertSame(
58 [1, 1],
59 Linter::findLineNumberAndColumn([0, 4, 7], 5),
61 self::assertSame(
62 [1, 2],
63 Linter::findLineNumberAndColumn([0, 4, 7], 6),
65 self::assertSame(
66 [2, 0],
67 Linter::findLineNumberAndColumn([0, 4, 7], 7),
71 /**
72 * Test for Linter::lint
74 * @param mixed[] $expected The expected result.
75 * @param string $query The query to be analyzed.
77 #[DataProvider('lintProvider')]
78 public function testLint(array $expected, string $query): void
80 self::assertSame($expected, Linter::lint($query));
83 /**
84 * Provides data for `testLint`.
86 * @return array<array{mixed[], string}>
88 public static function lintProvider(): array
90 return [
91 [[], ''],
92 [[], 'SELECT * FROM tbl'],
96 'message' => 'Unrecognized data type. (near <code>IN</code>)',
97 'fromLine' => 0,
98 'fromColumn' => 22,
99 'toLine' => 0,
100 'toColumn' => 24,
101 'severity' => 'error',
104 'message' => 'A closing bracket was expected. (near <code>IN</code>)',
105 'fromLine' => 0,
106 'fromColumn' => 22,
107 'toLine' => 0,
108 'toColumn' => 24,
109 'severity' => 'error',
112 'CREATE TABLE tbl ( id IN',
117 'message' => 'Linting is disabled for this query because it exceeds the maximum length.',
118 'fromLine' => 0,
119 'fromColumn' => 0,
120 'toLine' => 0,
121 'toColumn' => 0,
122 'severity' => 'warning',
125 str_repeat(';', 10001),