Repository navigation
Expand file tree
/
Copy pathWP_MySQL_On_SQLite_PDO_API_Tests.php
More file actions
1462 lines (1227 loc) · 51.8 KB
/
Copy pathWP_MySQL_On_SQLite_PDO_API_Tests.php
File metadata and controls
1462 lines (1227 loc) · 51.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use PHPUnit\Framework\TestCase;
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
class FetchObjectTestClass {
public $col1;
public $col2;
public $col3;
public $arg1;
public $arg2;
public function __construct( $arg1 = null, $arg2 = null ) {
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
}
class WP_MySQL_On_SQLite_PDO_API_Tests extends TestCase {
/** @var WP_MySQL_On_SQLite */
private $driver;
public function setUp(): void {
$this->driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;' );
// Run all tests with stringified fetch mode results, so we can use
// assertions that are consistent across all tested PHP versions.
// The "PDO::ATTR_STRINGIFY_FETCHES" mode is tested separately.
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
}
public function test_connection(): void {
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=WordPress;' );
$this->assertInstanceOf( PDO::class, $driver );
}
public function test_static_connect(): void {
if ( PHP_VERSION_ID < 80400 ) {
$this->markTestSkipped( 'PDO::connect() requires PHP 8.4 or newer.' );
}
$driver = WP_MySQL_On_SQLite::connect(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array( PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC )
);
$this->assertInstanceOf( WP_MySQL_On_SQLite::class, $driver );
$this->assertSame( array( 'value' => 1 ), $driver->query( 'SELECT 1 AS value' )->fetch() );
}
public function test_constructor_accepts_null_options(): void {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
null
);
$this->assertInstanceOf( PDO::class, $driver );
}
public function test_constructor_does_not_forward_driver_specific_pdo_options(): void {
// PDO MySQL and PDO SQLite assign different options to attributes 1000 and 1002.
foreach (
array(
1000 => true,
1002 => 'SET NAMES utf8mb4',
) as $attribute => $value
) {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array( $attribute => $value )
);
$this->assertEquals( 1, $driver->query( 'SELECT 1' )->fetchColumn() );
}
}
public function test_constructor_applies_pdo_options(): void {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array(
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_STRINGIFY_FETCHES => true,
)
);
$this->assertSame( PDO::FETCH_ASSOC, $driver->getAttribute( PDO::ATTR_DEFAULT_FETCH_MODE ) );
$this->assertSame( PDO::ERRMODE_SILENT, $driver->getAttribute( PDO::ATTR_ERRMODE ) );
$this->assertTrue( $driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
$this->assertSame( array( 'value' => '1' ), $driver->query( 'SELECT 1 AS value' )->fetch() );
// Internal operations always retain exception mode.
$this->assertSame( PDO::ERRMODE_EXCEPTION, $driver->get_sqlite_pdo()->getAttribute( PDO::ATTR_ERRMODE ) );
}
public function test_reports_mysql_driver_name(): void {
$this->assertSame( 'mysql', $this->driver->getAttribute( PDO::ATTR_DRIVER_NAME ) );
$this->assertSame( 'sqlite', $this->driver->get_sqlite_pdo()->getAttribute( PDO::ATTR_DRIVER_NAME ) );
}
public function test_configured_mysql_version_controls_reporting_and_parsing(): void {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array( 'mysql_version' => 50744 )
);
$server_version = '5.7.44-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION;
$this->assertSame( $server_version, $driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
$this->assertSame( 'mysqlnd ' . $server_version, $driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
$this->assertSame( 'mysqlnd ' . $server_version, $driver->client_info );
$this->assertSame( $server_version, $driver->query( 'SELECT VERSION()' )->fetchColumn() );
$this->assertSame( $server_version, $driver->query( 'SELECT @@version' )->fetchColumn() );
$this->assertEquals( 1, $driver->query( 'SELECT 1 /*!80000 + 1 */' )->fetchColumn() );
}
public function test_formats_six_digit_mysql_version(): void {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array( 'mysql_version' => 100000 )
);
$server_version = '10.0.0-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION;
$this->assertSame( $server_version, $driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
$this->assertSame( 'mysqlnd ' . $server_version, $driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
$this->assertSame( $server_version, $driver->query( 'SELECT VERSION()' )->fetchColumn() );
$this->assertSame( $server_version, $driver->query( 'SELECT @@version' )->fetchColumn() );
}
/**
* @dataProvider data_invalid_mysql_versions
*/
public function test_rejects_invalid_mysql_version( $mysql_version ): void {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage(
'The "mysql_version" option must be an integer greater than or equal to 50700.'
);
new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array( 'mysql_version' => $mysql_version )
);
}
public function data_invalid_mysql_versions(): array {
return array(
'string' => array( '80038' ),
'float' => array( 80038.0 ),
'boolean' => array( true ),
'array' => array( array( 80038 ) ),
'below minimum' => array( 50699 ),
'zero' => array( 0 ),
'negative' => array( -80038 ),
);
}
public function test_uses_shared_default_mysql_version(): void {
$this->assertSame( 80038, WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION );
$this->assertSame( '8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $this->driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
$this->assertSame( 'mysqlnd 8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $this->driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
}
public function test_constructor_applies_fetch_column_default(): void {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
null,
null,
array( PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_COLUMN )
);
$this->assertSame( 'value', $driver->query( "SELECT 'value'" )->fetch() );
}
public function test_constructor_applies_persistent_option(): void {
$path = tempnam( sys_get_temp_dir(), 'wp_sqlite_' );
unlink( $path );
try {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=' . $path . ';dbname=WordPress;',
null,
null,
array( PDO::ATTR_PERSISTENT => true )
);
$this->assertTrue( $driver->getAttribute( PDO::ATTR_PERSISTENT ) );
} finally {
$this->remove_database_files( $path );
}
}
public function test_constructor_reports_stringify_fetches_from_injected_pdo(): void {
if ( PHP_VERSION_ID < 80200 ) {
$this->markTestSkipped( 'PDO SQLite cannot report PDO::ATTR_STRINGIFY_FETCHES before PHP 8.2.' );
}
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
$pdo = new $pdo_class( 'sqlite::memory:' );
$pdo->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array( 'sqlite_pdo' => $pdo )
);
$this->assertTrue( $driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
}
public function test_driver_exception_exposes_originating_driver(): void {
$exception = new WP_MySQL_On_SQLite_Exception( $this->driver, 'Test error.' );
$this->assertSame( $this->driver, $exception->get_driver() );
$this->assertSame( array( 'HY000', 1105, 'Test error.' ), $exception->errorInfo );
}
public function test_driver_exception_preserves_pdo_error_information(): void {
$previous = new PDOException( 'Test PDO error.' );
$previous->errorInfo = array( 'HY000', 1, 'Test PDO error.' );
$exception = new WP_MySQL_On_SQLite_Exception(
$this->driver,
$previous->getMessage(),
$previous->getCode(),
$previous
);
$this->assertSame( $previous->errorInfo, $exception->errorInfo );
}
/**
* @dataProvider data_missing_table_queries
*/
public function test_missing_table_errors_use_mysql_identity( string $query ): void {
$driver_message = "Table 'missing_table' doesn't exist";
$message = 'SQLSTATE[42S02]: Base table or view not found: 1146 ' . $driver_message;
try {
$this->driver->query( $query );
$this->fail( 'Expected query() to throw an exception.' );
} catch ( WP_MySQL_On_SQLite_Exception $exception ) {
$this->assertSame( '42S02', $exception->getCode() );
$this->assertSame( $message, $exception->getMessage() );
$this->assertSame( array( '42S02', 1146, $driver_message ), $exception->errorInfo );
}
$this->assertSame( '42S02', $this->driver->errorCode() );
$this->assertSame( array( '42S02', 1146, $driver_message ), $this->driver->errorInfo() );
}
public function data_missing_table_queries(): array {
return array(
'Select' => array( 'SELECT * FROM missing_table' ),
'Insert' => array( 'INSERT INTO missing_table VALUES (1)' ),
'Update' => array( 'UPDATE missing_table SET value = 1' ),
'Delete' => array( 'DELETE FROM missing_table' ),
);
}
public function test_emulated_driver_exception_exposes_mysql_error_information(): void {
$this->driver->query( 'CREATE TABLE t (id INT)' );
try {
$this->driver->query( 'CREATE TABLE t (id INT)' );
$this->fail( 'Expected query() to throw an exception.' );
} catch ( WP_MySQL_On_SQLite_Exception $exception ) {
$this->assertSame( '42S01', $exception->getCode() );
$this->assertSame(
array( '42S01', 1050, "Table 't' already exists" ),
$exception->errorInfo
);
}
}
public function test_exposes_underlying_sqlite_pdo(): void {
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
$pdo = new $pdo_class( 'sqlite::memory:' );
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array( 'sqlite_pdo' => $pdo )
);
$this->assertSame( $pdo, $driver->get_sqlite_pdo() );
}
public function test_dsn_parsing(): void {
// Standard DSN.
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp' );
$this->assertSame( 'wp', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with trailing semicolon.
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;' );
$this->assertSame( 'wp', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with whitespace before argument names.
$driver = new WP_MySQL_On_SQLite( "mysql-on-sqlite: path=:memory:; \n\r\t\v\fdbname=wp" );
$this->assertSame( 'wp', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with whitespace in the database name.
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname= w p ' );
$this->assertSame( ' w p ', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with semicolon in the database name.
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;dbname=w;;p;' );
$this->assertSame( 'w;p', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with semicolon in the database name and a terminating semicolon.
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=w;;;p' );
$this->assertSame( 'w;', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with two semicolons in the database name.
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=w;;;;p' );
$this->assertSame( 'w;;p', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
// DSN with a "\0" byte (always terminates the DSN string).
$driver = new WP_MySQL_On_SQLite( "mysql-on-sqlite:path=:memory:;dbname=w\0p;" );
$this->assertSame( 'w', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
}
public function test_journal_mode_defaults_to_wal(): void {
$path = tempnam( sys_get_temp_dir(), 'wp_sqlite_' );
unlink( $path );
try {
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=' . $path . ';dbname=wp' );
$connection = $driver->get_connection();
$this->assertSame(
'wal',
strtolower( (string) $connection->query( 'PRAGMA journal_mode' )->fetchColumn() )
);
$this->assertSame(
'1',
(string) $connection->query( 'PRAGMA synchronous' )->fetchColumn()
);
} finally {
$this->remove_database_files( $path );
}
}
public function test_journal_mode_and_synchronous_driver_options(): void {
$path = tempnam( sys_get_temp_dir(), 'wp_sqlite_' );
unlink( $path );
try {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:path=' . $path . ';dbname=wp',
null,
null,
array(
'sqlite_journal_mode' => 'DELETE',
'sqlite_synchronous' => 'FULL',
)
);
$connection = $driver->get_connection();
$this->assertSame(
'delete',
strtolower( (string) $connection->query( 'PRAGMA journal_mode' )->fetchColumn() )
);
$this->assertSame(
'2',
(string) $connection->query( 'PRAGMA synchronous' )->fetchColumn()
);
} finally {
$this->remove_database_files( $path );
}
}
public function test_query(): void {
$result = $this->driver->query( "SELECT 1, 'abc'" );
$this->assertInstanceOf( WP_MySQL_On_SQLite_Statement::class, $result );
$this->assertInstanceOf( PDOStatement::class, $result );
if ( PHP_VERSION_ID < 80000 ) {
$this->assertSame(
array(
1 => '1',
2 => '1',
'abc' => 'abc',
3 => 'abc',
),
$result->fetch()
);
} else {
$this->assertSame(
array(
1 => '1',
0 => '1',
'abc' => 'abc',
),
$result->fetch()
);
}
}
public function test_inet_aton(): void {
$result = $this->driver->query( "SELECT INET_ATON('1.2.3.4')" );
$this->assertSame( '16909060', $result->fetchColumn() );
}
public function test_statement_query_string(): void {
$query = 'SELECT 1 AS value';
$stmt = $this->driver->query( $query );
// Userland cannot initialize PDOStatement::$queryString before PHP 8.1.
$this->assertSame( PHP_VERSION_ID < 80100 ? null : $query, $stmt->queryString );
}
public function test_statement_column_metadata_is_snapshotted(): void {
$stmt = $this->driver->query( "SELECT 1 AS first, 'value' AS second" );
$this->assertSame( 'first', $stmt->getColumnMeta( 0 )['name'] );
$this->assertSame( 'second', $stmt->getColumnMeta( 1 )['name'] );
$this->assertSame( 'second', $stmt->getColumnMeta( '1' )['name'] );
$this->assertFalse( $stmt->getColumnMeta( 2 ) );
$this->driver->query( 'SELECT 3 AS third' );
$this->assertSame( 'first', $stmt->getColumnMeta( 0 )['name'] );
$this->assertSame( 'second', $stmt->getColumnMeta( 1 )['name'] );
}
public function test_statement_column_metadata_rejects_negative_index(): void {
if ( PHP_VERSION_ID < 80000 ) {
$this->markTestSkipped( 'PDOStatement::getColumnMeta() throws ValueError on PHP 8.0 or newer.' );
}
$stmt = $this->driver->query( 'SELECT 1' );
$this->expectException( ValueError::class );
$stmt->getColumnMeta( -1 );
}
public function test_statement_column_metadata_rejects_invalid_index_type(): void {
if ( PHP_VERSION_ID < 80000 ) {
$this->markTestSkipped( 'PDOStatement::getColumnMeta() throws TypeError on PHP 8.0 or newer.' );
}
$stmt = $this->driver->query( 'SELECT 1' );
$this->expectException( TypeError::class );
$stmt->getColumnMeta( 'invalid' );
}
public function test_statement_column_metadata_is_resolved_lazily(): void {
$resolved_columns = array();
$raw_column_meta = array(
array( 'name' => 'first' ),
array( 'name' => 'second' ),
);
$stmt = new WP_MySQL_On_SQLite_Statement(
$this->driver->get_sqlite_pdo()->query( 'SELECT 1, 2' ),
'SELECT 1, 2',
function ( $column ) use ( &$resolved_columns, $raw_column_meta ) {
if ( ! array_key_exists( $column, $raw_column_meta ) ) {
return false;
}
$column_meta = $raw_column_meta[ $column ];
$resolved_columns[] = $column_meta['name'];
return $column_meta;
}
);
$this->assertSame( array(), $resolved_columns );
$this->assertSame( 'second', $stmt->getColumnMeta( 1 )['name'] );
$this->assertSame( array( 'second' ), $resolved_columns );
$this->assertSame( 'second', $stmt->getColumnMeta( 1 )['name'] );
$this->assertFalse( $stmt->getColumnMeta( 2 ) );
$this->assertSame( array( 'second' ), $resolved_columns );
$this->assertSame( 'first', $stmt->getColumnMeta( 0 )['name'] );
$this->assertSame( array( 'second', 'first' ), $resolved_columns );
}
public function test_statement_column_metadata_resolution_preserves_the_query_log(): void {
$this->driver->exec( 'CREATE TABLE metadata_test (id INT)' );
$this->driver->exec( 'INSERT INTO metadata_test VALUES (1)' );
$stmt = $this->driver->query( 'SELECT id FROM metadata_test' );
$last_sqlite_queries = $this->driver->get_last_sqlite_queries();
$this->assertSame( 'id', $stmt->getColumnMeta( 0 )['name'] );
$this->assertSame( $last_sqlite_queries, $this->driver->get_last_sqlite_queries() );
}
public function test_statement_column_metadata_snapshots_database_context(): void {
$stmt = $this->driver->query( 'SELECT 1 AS value' );
$this->driver->exec( 'USE information_schema' );
$this->assertSame( 'wp', $stmt->getColumnMeta( 0 )['mysqli:db'] );
}
public function test_statement_error_information(): void {
$stmt = $this->driver->query( 'SELECT 1' );
$this->assertSame( '00000', $stmt->errorCode() );
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
}
public function test_statement_error_information_discards_stale_sqlite_error(): void {
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
$pdo = new $pdo_class( 'sqlite::memory:' );
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
$pdo->query( 'SELECT * FROM missing_table' );
$stmt = new WP_MySQL_On_SQLite_Statement(
$pdo->query( 'SELECT 1' ),
'SELECT 1',
function () {
return false;
}
);
$this->assertSame( '00000', $stmt->errorCode() );
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
}
public function test_empty_result_statement(): void {
// The row count of a previous write must not leak into the empty result.
$this->driver->exec( 'CREATE TABLE t (id INT)' );
$this->driver->exec( 'INSERT INTO t VALUES (1), (2), (3)' );
$stmt = $this->driver->query( 'SET @a = 1' );
// Check the error state and iteration first, as fetching resets the error state.
$this->assertSame( '00000', $stmt->errorCode() );
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
$this->assertSame( array(), iterator_to_array( $stmt ) );
$this->assertSame( 0, $stmt->columnCount() );
$this->assertSame( 0, $stmt->rowCount() );
$this->assertFalse( $stmt->getColumnMeta( 0 ) );
$fetch_modes = array(
PDO::FETCH_BOTH,
PDO::FETCH_NUM,
PDO::FETCH_ASSOC,
PDO::FETCH_NAMED,
PDO::FETCH_OBJ,
PDO::FETCH_COLUMN,
PDO::FETCH_KEY_PAIR,
);
foreach ( $fetch_modes as $mode ) {
$this->assertFalse( $stmt->fetch( $mode ) );
$this->assertSame( array(), $stmt->fetchAll( $mode ) );
}
$this->assertFalse( $stmt->fetchColumn() );
$this->assertFalse( $stmt->fetchObject() );
$this->assertSame( array(), $stmt->fetchAll( PDO::FETCH_GROUP | PDO::FETCH_ASSOC ) );
$this->assertSame( array(), $stmt->fetchAll( PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC ) );
$this->assertSame( array(), $stmt->fetchAll( PDO::FETCH_CLASS, stdClass::class ) );
$this->assertTrue( $stmt->setFetchMode( PDO::FETCH_CLASS, stdClass::class ) );
$this->assertFalse( $stmt->fetch() );
$value = 'unchanged';
$this->assertTrue( $stmt->bindColumn( 1, $value ) );
$this->assertFalse( $stmt->fetch( PDO::FETCH_BOUND ) );
$this->assertSame( 'unchanged', $value );
$this->assertTrue( $stmt->closeCursor() );
$this->assertFalse( $stmt->fetch() );
// Holding an unread statement must not block a commit or a schema change.
$stmt = $this->driver->query( 'SET @a = 1' );
$this->driver->exec( 'START TRANSACTION' );
$this->driver->exec( 'INSERT INTO t VALUES (4)' );
$this->assertSame( 0, $this->driver->exec( 'COMMIT' ) );
$this->assertSame( 0, $this->driver->exec( 'DROP TABLE t' ) );
}
public function test_statement_iteration(): void {
$stmt = $this->driver->query( 'SELECT 1 AS value UNION ALL SELECT 2', PDO::FETCH_ASSOC );
$this->assertSame(
array(
array( 'value' => '1' ),
array( 'value' => '2' ),
),
iterator_to_array( $stmt )
);
}
public function test_statement_close_cursor(): void {
$stmt = $this->driver->query( 'SELECT 1 UNION ALL SELECT 2' );
$this->assertTrue( $stmt->closeCursor() );
$this->assertFalse( $stmt->fetch() );
}
public function test_statement_bind_column(): void {
$stmt = $this->driver->query( 'SELECT 1 AS value' );
$value = null;
$this->assertTrue( $stmt->bindColumn( 'value', $value ) );
$this->assertTrue( $stmt->fetch( PDO::FETCH_BOUND ) );
$this->assertSame( '1', $value );
}
/**
* @dataProvider data_pdo_fetch_methods
*/
public function test_query_with_fetch_mode( $query, $mode, $expected ): void {
$stmt = $this->driver->query( $query, $mode );
$result = $stmt->fetch();
if ( is_object( $expected ) ) {
$this->assertInstanceOf( get_class( $expected ), $result );
$this->assertSame( (array) $expected, (array) $result );
} elseif ( PDO::FETCH_NAMED === $mode ) {
// PDO::FETCH_NAMED returns all array keys as strings, even numeric
// ones. This is not possible in plain PHP and might be a PDO bug.
$this->assertSame( array_map( 'strval', array_keys( $expected ) ), array_keys( $result ) );
$this->assertSame( array_values( $expected ), array_values( $result ) );
} else {
$this->assertSame( $expected, $result );
}
$this->assertFalse( $stmt->fetch() );
}
public function test_query_fetch_mode_not_set(): void {
$result = $this->driver->query( 'SELECT 1' );
if ( PHP_VERSION_ID < 80000 ) {
$this->assertSame(
array(
1 => '1',
2 => '1',
),
$result->fetch()
);
} else {
$this->assertSame(
array(
1 => '1',
0 => '1',
),
$result->fetch()
);
}
$this->assertFalse( $result->fetch() );
}
public function test_query_fetch_mode_invalid_arg_count(): void {
$this->expectException( ArgumentCountError::class );
$this->expectExceptionMessage( 'PDO::query() expects exactly 2 arguments for the fetch mode provided, 3 given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_ASSOC, 0 );
}
public function test_query_fetch_default_mode_allow_any_args(): void {
if ( PHP_VERSION_ID < 80100 ) {
// On PHP < 8.1, fetch mode value of NULL is not allowed.
$result = @$this->driver->query( 'SELECT 1', null, 1, 2, 'abc', array(), true ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$this->assertFalse( $result );
$this->assertSame( 'PDO::query(): SQLSTATE[HY000]: General error: mode must be an integer', error_get_last()['message'] );
return;
}
// On PHP >= 8.1, NULL fetch mode is allowed to use the default fetch mode.
// In such cases, any additional arguments are ignored and not validated.
$expected_result = array(
array(
1 => '1',
0 => '1',
),
);
$result = $this->driver->query( 'SELECT 1' );
$this->assertSame( $expected_result, $result->fetchAll() );
$result = $this->driver->query( 'SELECT 1', null );
$this->assertSame( $expected_result, $result->fetchAll() );
$result = $this->driver->query( 'SELECT 1', null, 1 );
$this->assertSame( $expected_result, $result->fetchAll() );
$result = $this->driver->query( 'SELECT 1', null, 'abc' );
$this->assertSame( $expected_result, $result->fetchAll() );
$result = $this->driver->query( 'SELECT 1', null, 1, 2, 'abc', array(), true );
$this->assertSame( $expected_result, $result->fetchAll() );
}
public function test_query_fetch_class_not_enough_args(): void {
$this->expectException( ArgumentCountError::class );
$this->expectExceptionMessage( 'PDO::query() expects at least 3 arguments for the fetch mode provided, 2 given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_CLASS );
}
public function test_query_fetch_class_too_many_args(): void {
$this->expectException( ArgumentCountError::class );
$this->expectExceptionMessage( 'PDO::query() expects at most 4 arguments for the fetch mode provided, 5 given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_CLASS, '\stdClass', array(), array() );
}
public function test_query_fetch_class_invalid_class_type(): void {
$this->expectException( TypeError::class );
$this->expectExceptionMessage( 'PDO::query(): Argument #3 must be of type string, int given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_CLASS, 1 );
}
public function test_query_fetch_class_invalid_class_name(): void {
$this->expectException( TypeError::class );
$this->expectExceptionMessage( 'PDO::query(): Argument #3 must be a valid class' );
$this->driver->query( 'SELECT 1', PDO::FETCH_CLASS, 'non-existent-class' );
}
public function test_query_fetch_class_invalid_constructor_args_type(): void {
$this->expectException( TypeError::class );
$this->expectExceptionMessage( 'PDO::query(): Argument #4 must be of type ?array, int given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_CLASS, 'stdClass', 1 );
}
public function test_query_fetch_into_invalid_arg_count(): void {
$this->expectException( ArgumentCountError::class );
$this->expectExceptionMessage( 'PDO::query() expects exactly 3 arguments for the fetch mode provided, 2 given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_INTO );
}
public function test_query_fetch_into_invalid_object_type(): void {
$this->expectException( TypeError::class );
$this->expectExceptionMessage( 'PDO::query(): Argument #3 must be of type object, int given' );
$this->driver->query( 'SELECT 1', PDO::FETCH_INTO, 1 );
}
public function test_exec(): void {
$result = $this->driver->exec( 'SELECT 1' );
$this->assertEquals( 0, $result );
$result = $this->driver->exec( 'CREATE TABLE t (id INT)' );
$this->assertEquals( 0, $result );
$result = $this->driver->exec( 'INSERT INTO t (id) VALUES (1)' );
$this->assertEquals( 1, $result );
$result = $this->driver->exec( 'INSERT INTO t (id) VALUES (2), (3)' );
$this->assertEquals( 2, $result );
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id = 0' );
$this->assertEquals( 0, $result );
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id = 1' );
$this->assertEquals( 1, $result );
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id < 10' );
$this->assertEquals( 2, $result );
$result = $this->driver->exec( 'DELETE FROM t WHERE id = 11' );
$this->assertEquals( 1, $result );
$result = $this->driver->exec( 'DELETE FROM t' );
$this->assertEquals( 2, $result );
$result = $this->driver->exec( 'DROP TABLE t' );
$this->assertEquals( 0, $result );
}
public function test_row_count_of_result_set_without_rows(): void {
// The row count of a previous write must not leak into the result set.
$this->driver->exec( 'CREATE TABLE t (id INT)' );
$this->assertSame( 0, $this->driver->query( 'SELECT * FROM t' )->rowCount() );
$this->driver->exec( 'INSERT INTO t VALUES (1), (2), (3)' );
$this->assertSame( 0, $this->driver->query( 'SELECT * FROM t WHERE id > 3' )->rowCount() );
$this->assertSame( 0, $this->driver->query( 'SELECT 1 WHERE FALSE' )->rowCount() );
$this->assertSame( 0, $this->driver->query( "SHOW TABLES LIKE 'missing'" )->rowCount() );
$this->driver->exec( 'INSERT INTO t VALUES (4), (5), (6)' );
$this->driver->exec( 'SET @a = 1' );
$this->assertSame( 0, $this->driver->query( 'SELECT * FROM t WHERE id > 6' )->rowCount() );
}
public function test_row_count_of_non_dml_statements(): void {
// The row count of the internal writes of CREATE TABLE must not leak.
$this->driver->exec( 'CREATE TABLE t (id INT PRIMARY KEY AUTO_INCREMENT, a INT, b INT)' );
$this->assertSame( 0, $this->driver->exec( 'DROP VIEW IF EXISTS v' ) );
$this->assertSame( 0, $this->driver->exec( 'DROP TRIGGER IF EXISTS tr' ) );
// The row count of a previous write must not leak.
$this->driver->exec( 'INSERT INTO t (a, b) VALUES (1, 1), (2, 2), (3, 3)' );
$this->assertSame( 0, $this->driver->exec( 'DROP VIEW IF EXISTS v' ) );
$this->assertSame( 0, $this->driver->exec( 'DROP TRIGGER IF EXISTS tr' ) );
// TRUNCATE reports 0 affected rows in MySQL, regardless of the deleted rows.
$this->assertSame( 0, $this->driver->exec( 'TRUNCATE TABLE t' ) );
// DML statements still report their affected rows.
$this->assertSame( 3, $this->driver->exec( 'INSERT INTO t (a, b) VALUES (1, 1), (2, 2), (3, 3)' ) );
$this->assertSame( 1, $this->driver->exec( 'REPLACE INTO t (id, a, b) VALUES (4, 4, 4)' ) );
$this->assertSame( 2, $this->driver->exec( 'UPDATE t SET b = 0 WHERE a > 2' ) );
$this->driver->exec( 'CREATE TABLE u (id INT)' );
$this->driver->exec( 'INSERT INTO u VALUES (1), (2)' );
$this->assertSame( 2, $this->driver->exec( 'DELETE t FROM t JOIN u ON t.id = u.id' ) );
$this->assertSame( 2, $this->driver->exec( 'DELETE FROM t' ) );
}
public function test_last_insert_id(): void {
$this->assertSame( '0', $this->driver->lastInsertId() );
$this->driver->query( 'CREATE TABLE t (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)' );
$this->assertSame( '0', $this->driver->lastInsertId() );
$this->driver->query( 'INSERT INTO t (id) VALUES (NULL)' );
$this->assertSame( '1', $this->driver->lastInsertId() );
$this->assertSame( '1', $this->driver->lastInsertId( 'ignored_sequence_name' ) );
$this->driver->query( 'CREATE TABLE another_table (id INT)' );
$this->assertSame( '0', $this->driver->lastInsertId() );
}
public function test_last_insert_id_rejects_invalid_sequence_name(): void {
if ( PHP_VERSION_ID < 80000 ) {
$result = @$this->driver->lastInsertId( array() ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$this->assertFalse( $result );
$this->assertSame( 'PDO::lastInsertId() expects parameter 1 to be string, array given', error_get_last()['message'] );
return;
}
$this->expectException( TypeError::class );
$this->expectExceptionMessage( 'PDO::lastInsertId(): Argument #1 ($name) must be of type ?string, array given' );
$this->driver->lastInsertId( array() );
}
public function test_connection_error_information(): void {
$this->assertNull( $this->driver->errorCode() );
$this->assertSame( array( '', null, null ), $this->driver->errorInfo() );
$this->driver->query( 'SELECT 1' );
$this->assertSame( '00000', $this->driver->errorCode() );
$this->assertSame(
array( '00000', null, null ),
$this->driver->errorInfo()
);
}
public function test_silent_error_mode(): void {
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
$this->assertSame( '42S02', $this->driver->errorCode() );
$this->assertSame(
array( '42S02', 1146, "Table 'missing_table' doesn't exist" ),
$this->driver->errorInfo()
);
$this->assertFalse( $this->driver->exec( 'SELECT * FROM missing_table' ) );
$this->assertInstanceOf( PDOStatement::class, $this->driver->query( 'SELECT 1' ) );
$this->assertSame( '00000', $this->driver->errorCode() );
}
public function test_warning_error_mode(): void {
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$warning = null;
set_error_handler(
function ( $level, $message ) use ( &$warning ) {
if ( E_USER_WARNING === $level ) {
$warning = $message;
return true;
}
return false;
}
);
try {
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
} finally {
restore_error_handler();
}
$this->assertSame(
"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'missing_table' doesn't exist",
$warning
);
}
public function test_quote_matches_mysql_escaping(): void {
$backslash = chr( 92 );
$value = chr( 0 ) . "\n\r{$backslash}'\"" . chr( 26 ) . "\tƮềʂᴛ🙂";
$quoted = $this->driver->quote( $value );
$this->assertSame(
"'{$backslash}0{$backslash}n{$backslash}r"
. "{$backslash}{$backslash}{$backslash}'{$backslash}\"{$backslash}Z\tƮềʂᴛ🙂'",
$quoted
);
$this->assertSame( $value, $this->driver->query( "SELECT $quoted" )->fetchColumn() );
}
public function test_quote_supports_parameter_types(): void {
$value = "\0\n\r\\'\"" . chr( 26 ) . "\tƮềʂᴛ🙂";
$quoted = $this->driver->quote( $value );
$this->assertSame( $quoted, $this->driver->quote( $value, PDO::PARAM_STR ) );
$this->assertSame( 'N' . $quoted, $this->driver->quote( $value, PDO::PARAM_STR_NATL ) );
$this->assertSame( $quoted, $this->driver->quote( $value, PDO::PARAM_STR_CHAR ) );
$this->assertSame(
$quoted,
$this->driver->quote( $value, PDO::PARAM_STR_NATL | PDO::PARAM_STR_CHAR )
);
$this->assertSame( '_binary' . $quoted, $this->driver->quote( $value, PDO::PARAM_LOB ) );
$this->assertSame(
'_binary' . $quoted,
$this->driver->quote( $value, PDO::PARAM_LOB | PDO::PARAM_STR_NATL )
);
}
public function test_quote_rejects_non_stringable_values(): void {
$resource = fopen( 'php://memory', 'r' );
try {
foreach ( array( array(), $resource, new stdClass() ) as $value ) {
try {
$this->driver->quote( $value );
$this->fail( 'Expected quote() to throw a TypeError.' );
} catch ( TypeError $e ) {
$this->assertStringContainsString( 'must be of type string', $e->getMessage() );
}
}
} finally {
fclose( $resource );
}
}
public function test_quote_accepts_stringable_objects(): void {
$value = new class() {
public function __toString(): string {
return 'value';
}
};
$this->assertSame( "'value'", $this->driver->quote( $value ) );
}
public function test_begin_transaction(): void {
$result = $this->driver->beginTransaction();
$this->assertTrue( $result );
}
public function test_begin_transaction_already_active(): void {
$this->driver->beginTransaction();
$this->expectException( PDOException::class );
$this->expectExceptionMessage( 'There is already an active transaction' );
$this->expectExceptionCode( 0 );
$this->driver->beginTransaction();
}
public function test_commit(): void {
$this->driver->beginTransaction();
$result = $this->driver->commit();
$this->assertTrue( $result );
}
public function test_commit_no_active_transaction(): void {
$this->expectException( PDOException::class );
$this->expectExceptionMessage( 'There is no active transaction' );
$this->expectExceptionCode( 0 );
$this->driver->commit();
}
public function test_rollback(): void {
$this->driver->beginTransaction();
$result = $this->driver->rollBack();
$this->assertTrue( $result );
}
public function test_rollback_statement_as_first_transaction_on_connection(): void {
// Roll back the first transaction on the connection.
$this->driver->exec( 'START TRANSACTION' );
$this->driver->exec( 'ROLLBACK' );
$this->assertFalse( $this->driver->inTransaction() );
// Statements with empty result sets keep working after the rollback.
$this->driver->exec( 'START TRANSACTION' );
$this->driver->exec( 'CREATE TABLE t (id INT)' );
$this->driver->exec( 'ROLLBACK' );
$this->assertSame( 0, $this->driver->exec( 'CREATE TABLE t (id INT)' ) );
}
public function test_rollback_method_as_first_transaction_on_connection(): void {
// Roll back the first transaction on the connection.
$this->driver->beginTransaction();
$this->driver->exec( 'CREATE TABLE t (id INT)' );
$this->assertTrue( $this->driver->rollBack() );