|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2019 Gerrit Addiks. |
|
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
|
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GPL-3.0 |
|
9
|
|
|
* @author Gerrit Addiks <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Addiks\StoredSQL\Tests\Unit\Lexing; |
|
13
|
|
|
|
|
14
|
|
|
use Addiks\StoredSQL\Exception\UnlexableSqlException; |
|
15
|
|
|
use Addiks\StoredSQL\Lexing\AbstractSqlToken; |
|
16
|
|
|
use Addiks\StoredSQL\Lexing\SqlTokenInstance; |
|
17
|
|
|
use Addiks\StoredSQL\Lexing\SqlTokenizerClass; |
|
18
|
|
|
use Addiks\StoredSQL\Lexing\SqlTokens; |
|
19
|
|
|
use Addiks\StoredSQL\Lexing\SqlTokensClass; |
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
use Webmozart\Assert\Assert; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @covers Addiks\StoredSQL\Lexing\SqlToken |
|
26
|
|
|
* @covers Addiks\StoredSQL\Lexing\SqlTokensClass |
|
27
|
|
|
* @covers Addiks\StoredSQL\Lexing\SqlTokenInstanceClass |
|
28
|
|
|
*/ |
|
29
|
|
|
final class SqlTokenizerClassTest extends TestCase |
|
30
|
|
|
{ |
|
31
|
|
|
const DATA_FOLDER_NAME = '../../../fixtures'; |
|
32
|
|
|
|
|
33
|
|
|
private ?SqlTokenizerClass $tokenizer = null; |
|
34
|
|
|
|
|
35
|
|
|
public function setUp(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->tokenizer = SqlTokenizerClass::defaultTokenizer(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
* @dataProvider dataProvider |
|
43
|
|
|
*/ |
|
44
|
|
|
public function shouldDetectCorrectTokens( |
|
45
|
|
|
string $sql, |
|
46
|
|
|
string $expectedDump |
|
47
|
|
|
): void { |
|
48
|
|
|
Assert::object($this->tokenizer); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
/** @var SqlTokens $tokens */ |
|
52
|
|
|
$tokens = $this->tokenizer->tokenize($sql); |
|
|
|
|
|
|
53
|
|
|
$tokens = $tokens->withoutWhitespace(); |
|
54
|
|
|
|
|
55
|
|
|
} catch (UnlexableSqlException $exception) { |
|
56
|
|
|
echo $exception->asciiLocationDump(); |
|
57
|
|
|
|
|
58
|
|
|
throw $exception; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @var array<string> $actualLines */ |
|
62
|
|
|
$actualLines = array(); |
|
63
|
|
|
|
|
64
|
|
|
/** @var SqlTokenInstance $token */ |
|
65
|
|
|
foreach ($tokens as $index => $token) { |
|
66
|
|
|
$actualLines[] = sprintf( |
|
67
|
|
|
'%d,%d,%s', |
|
68
|
|
|
$token->line(), |
|
69
|
|
|
$token->offset(), |
|
70
|
|
|
$token->token()->name() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$actualDump = implode("\n", $actualLines); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals($expectedDump, $actualDump); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @return array<string, array{0:string, 1:string}> */ |
|
80
|
|
|
public function dataProvider(): array |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var array<string> $sqlFiles */ |
|
83
|
|
|
$sqlFiles = glob(sprintf('%s/%s/*.sql', __DIR__, self::DATA_FOLDER_NAME)); |
|
84
|
|
|
|
|
85
|
|
|
/** @var array<string, array{0:string, 1:string}> $dataSets */ |
|
86
|
|
|
$dataSets = array(); |
|
87
|
|
|
|
|
88
|
|
|
/** @var string $sqlFile */ |
|
89
|
|
|
foreach ($sqlFiles as $sqlFile) { |
|
90
|
|
|
|
|
91
|
|
|
/** @var string $tokenFile */ |
|
92
|
|
|
$tokenFile = $sqlFile . '.tokens'; |
|
93
|
|
|
|
|
94
|
|
|
if (file_exists($tokenFile)) { |
|
95
|
|
|
$dataSets[basename($tokenFile)] = [ |
|
96
|
|
|
(string) file_get_contents($sqlFile), |
|
97
|
|
|
trim((string) file_get_contents($tokenFile)), |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $dataSets; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @test */ |
|
106
|
|
|
public function shouldNotAcceptNonSqlTokens(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
109
|
|
|
|
|
110
|
|
|
/** @psalm-suppress InvalidArgument */ |
|
111
|
|
|
new SqlTokensClass(['foo'], ''); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** @param array<int, AbstractSqlToken> $tokens */ |
|
115
|
|
|
private function tokenListToString(array $tokens): string |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
return implode("\n", array_map(function (AbstractSqlToken $token) { |
|
118
|
|
|
return $token->name(); |
|
119
|
|
|
}, $tokens)); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.