Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/platform/SqlitePlatformTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use InvalidArgumentException;
6 use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
7 use MediaWikiCoversValidator;
8 use PHPUnit\Framework\TestCase;
9 use Wikimedia\Rdbms\DatabaseDomain;
10 use Wikimedia\Rdbms\DBLanguageError;
11 use Wikimedia\Rdbms\Platform\SqlitePlatform;
12
13 /**
14 * @covers \Wikimedia\Rdbms\Platform\SqlitePlatform
15 */
16 class SqlitePlatformTest extends TestCase {
17
18 use MediaWikiCoversValidator;
19
20 /** @var SqlitePlatform */
21 private $platform;
22
23 protected function setUp(): void {
24 parent::setUp();
25 $this->platform = new SqlitePlatform(
26 new AddQuoterMock(),
27 null,
28 new DatabaseDomain( null, null, '' )
29 );
30 }
31
32 public static function provideBuildSubstring() {
33 yield [ 'someField', 1, 2, 'SUBSTR(someField,1,2)' ];
34 yield [ 'someField', 1, null, 'SUBSTR(someField,1)' ];
35 }
36
37 /**
38 * @dataProvider provideBuildSubstring
39 */
40 public function testBuildSubstring( $input, $start, $length, $expected ) {
41 $output = $this->platform->buildSubstring( $input, $start, $length );
42 $this->assertSame( $expected, $output );
43 }
44
45 public static function provideBuildSubstring_invalidParams() {
46 yield [ -1, 1 ];
47 yield [ 1, -1 ];
48 yield [ 1, 'foo' ];
49 yield [ 'foo', 1 ];
50 yield [ null, 1 ];
51 yield [ 0, 1 ];
52 }
53
54 /**
55 * @dataProvider provideBuildSubstring_invalidParams
56 */
57 public function testBuildSubstring_invalidParams( $start, $length ) {
58 $this->expectException( InvalidArgumentException::class );
59 $this->platform->buildSubstring( 'foo', $start, $length );
60 }
61
62 /**
63 * @dataProvider provideGreatest
64 */
65 public function testBuildGreatest( $fields, $values, $sqlText ) {
66 $this->assertEquals(
67 $sqlText,
68 trim( $this->platform->buildGreatest( $fields, $values ) )
69 );
70 }
71
72 public static function provideGreatest() {
73 return [
74 [
75 'field',
76 'value',
77 "MAX(\"field\",'value')"
78 ],
79 [
80 [ 'field' ],
81 [ 'value' ],
82 "MAX(\"field\",'value')"
83 ],
84 [
85 [ 'field', 'field2' ],
86 [ 'value', 'value2', 3, 7.6 ],
87 "MAX(\"field\",\"field2\",'value','value2',3,7.6)"
88 ],
89 [
90 [ 'field', 'a' => "\"field2\"+1" ],
91 [ 'value', 'value2', 3, 7.6 ],
92 "MAX(\"field\",\"field2\"+1,'value','value2',3,7.6)"
93 ],
94 ];
95 }
96
97 /**
98 * @dataProvider provideLeast
99 */
100 public function testBuildLeast( $fields, $values, $sqlText ) {
101 $this->assertEquals(
102 $sqlText,
103 trim( $this->platform->buildLeast( $fields, $values ) )
104 );
105 }
106
107 public static function provideLeast() {
108 return [
109 [
110 'field',
111 'value',
112 "MIN(\"field\",'value')"
113 ],
114 [
115 [ 'field' ],
116 [ 'value' ],
117 "MIN(\"field\",'value')"
118 ],
119 [
120 [ 'field', 'field2' ],
121 [ 'value', 'value2', 3, 7.6 ],
122 "MIN(\"field\",\"field2\",'value','value2',3,7.6)"
123 ],
124 [
125 [ 'field', 'a' => "\"field2\"+1" ],
126 [ 'value', 'value2', 3, 7.6 ],
127 "MIN(\"field\",\"field2\"+1,'value','value2',3,7.6)"
128 ],
129 ];
130 }
131
132 public function testTableName() {
133 // @todo Moar!
134 $platform = $this->platform;
135 $this->assertEquals( '"foo"', $platform->tableName( 'foo' ) );
136 $this->assertEquals( 'sqlite_master', $platform->tableName( 'sqlite_master' ) );
137 $platform->setPrefix( 'foo_' );
138 $this->assertEquals( 'sqlite_master', $platform->tableName( 'sqlite_master' ) );
139 $this->assertEquals( '"foo_bar"', $platform->tableName( 'bar' ) );
140 }
141
142 public static function provideTableIdentifiers() {
143 // No DB name set
144 yield [
145 'table',
146 new DatabaseDomain( null, null, '' ),
147 [ '', 'table' ],
148 null
149 ];
150 yield [
151 'database.table',
152 new DatabaseDomain( null, null, '' ),
153 [ 'database', 'table' ],
154 null
155 ];
156 yield [
157 'database.schema.table',
158 new DatabaseDomain( null, null, '' ),
159 [ 'database', 'table' ],
160 DBLanguageError::class
161 ];
162 yield [
163 '"database"."schema"."table"',
164 new DatabaseDomain( null, null, '' ),
165 [ 'database', 'table' ],
166 DBLanguageError::class
167 ];
168 yield [
169 '"database"."table"',
170 new DatabaseDomain( null, null, '' ),
171 [ 'database', 'table' ],
172 null
173 ];
174 // DB name set
175 yield [
176 'table',
177 new DatabaseDomain( 'database', null, '' ),
178 [ 'database', 'table' ],
179 null
180 ];
181 yield [
182 'database.table',
183 new DatabaseDomain( 'database', null, '' ),
184 [ 'database', 'table' ],
185 null
186 ];
187 yield [
188 'database.schema.table',
189 new DatabaseDomain( 'database', null, '' ),
190 [ 'database', 'table' ],
191 DBLanguageError::class
192 ];
193 yield [
194 '"database"."schema"."table"',
195 new DatabaseDomain( 'database', null, '' ),
196 [ 'database', 'table' ],
197 DBLanguageError::class
198 ];
199 yield [
200 '"database"."table"',
201 new DatabaseDomain( 'database', null, '' ),
202 [ 'database', 'table' ],
203 null
204 ];
205 }
206
207 /**
208 * @dataProvider provideTableIdentifiers
209 */
210 public function testGetDatabaseAndTableIdentifiers(
211 $tableName,
212 $domain,
213 $expectedIdentifiers,
214 $expectedException
215 ) {
216 $platform = new SqlitePlatform( new AddQuoterMock(), null, $domain );
217
218 if ( $expectedException !== null ) {
219 $this->expectException( DBLanguageError::class );
220 $platform->getDatabaseAndTableIdentifier( $tableName );
221 } else {
222 $this->assertSame(
223 $expectedIdentifiers,
224 $platform->getDatabaseAndTableIdentifier( $tableName )
225 );
226 }
227 }
228 }