@@ -7,7 +7,8 @@ class WP_PDO_MySQL_On_SQLite_PDO_API_Tests extends TestCase {
77 private $ driver ;
88
99 public function setUp (): void {
10- $ this ->driver = new WP_PDO_MySQL_On_SQLite ( 'mysql-on-sqlite:path=:memory:;dbname=WordPress; ' );
10+ $ this ->driver = new WP_PDO_MySQL_On_SQLite ( 'mysql-on-sqlite:path=:memory:;dbname=wp; ' );
11+ $ this ->driver ->setAttribute ( PDO ::ATTR_STRINGIFY_FETCHES , false );
1112 }
1213
1314 public function test_connection (): void {
@@ -16,9 +17,128 @@ public function test_connection(): void {
1617 }
1718
1819 public function test_query (): void {
19- $ result = $ this ->driver ->query ( ' SELECT 1 ' );
20+ $ result = $ this ->driver ->query ( " SELECT 1, 'abc' " );
2021 $ this ->assertInstanceOf ( PDOStatement::class, $ result );
21- $ this ->assertEquals ( 1 , $ result ->fetchColumn () );
22+ $ this ->assertSame (
23+ array (
24+ 1 => 1 ,
25+ 0 => 1 ,
26+ 'abc ' => 'abc ' ,
27+ ),
28+ $ result ->fetch ( PDO ::FETCH_BOTH )
29+ );
30+ }
31+
32+ /**
33+ * @dataProvider data_pdo_fetch_methods
34+ */
35+ public function test_query_with_fetch_mode ( $ query , $ mode , $ expected ): void {
36+ $ stmt = $ this ->driver ->query ( $ query , $ mode );
37+ $ result = $ stmt ->fetch ();
38+ if ( is_object ( $ expected ) ) {
39+ $ this ->assertInstanceOf ( get_class ( $ expected ), $ result );
40+ $ this ->assertEquals ( $ expected , $ result );
41+ } else {
42+ $ this ->assertSame ( $ expected , $ result );
43+ }
44+
45+ $ this ->assertFalse ( $ stmt ->fetch () );
46+ }
47+
48+ public function test_query_fetch_mode_not_set (): void {
49+ $ result = $ this ->driver ->query ( 'SELECT 1 ' );
50+ $ this ->assertSame (
51+ array (
52+ '1 ' => 1 ,
53+ 0 => 1 ,
54+ ),
55+ $ result ->fetch ()
56+ );
57+ $ this ->assertFalse ( $ result ->fetch () );
58+ }
59+
60+ public function test_query_fetch_mode_invalid_arg_count (): void {
61+ $ this ->expectException ( ArgumentCountError::class );
62+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 2 arguments for the fetch mode provided, 3 given ' );
63+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_ASSOC , 0 );
64+ }
65+
66+ public function test_query_fetch_default_mode_allow_any_args (): void {
67+ $ expected_result = array (
68+ array (
69+ 1 => 1 ,
70+ 0 => 1 ,
71+ ),
72+ );
73+
74+ $ result = $ this ->driver ->query ( 'SELECT 1 ' );
75+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
76+
77+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null );
78+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
79+
80+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 1 );
81+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
82+
83+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 'abc ' );
84+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
85+
86+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 1 , 2 , 'abc ' , array (), true );
87+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
88+ }
89+
90+ public function test_query_fetch_column_invalid_arg_count (): void {
91+ $ this ->expectException ( ArgumentCountError::class );
92+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 3 arguments for the fetch mode provided, 2 given ' );
93+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_COLUMN );
94+ }
95+
96+ public function test_query_fetch_column_invalid_colno_type (): void {
97+ $ this ->expectException ( TypeError::class );
98+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type int, string given ' );
99+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_COLUMN , '0 ' );
100+ }
101+
102+ public function test_query_fetch_class_not_enough_args (): void {
103+ $ this ->expectException ( ArgumentCountError::class );
104+ $ this ->expectExceptionMessage ( 'PDO::query() expects at least 3 arguments for the fetch mode provided, 2 given ' );
105+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS );
106+ }
107+
108+ public function test_query_fetch_class_too_many_args (): void {
109+ $ this ->expectException ( ArgumentCountError::class );
110+ $ this ->expectExceptionMessage ( 'PDO::query() expects at most 4 arguments for the fetch mode provided, 5 given ' );
111+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , '\stdClass ' , array (), array () );
112+ }
113+
114+ public function test_query_fetch_class_invalid_class_type (): void {
115+ $ this ->expectException ( TypeError::class );
116+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type string, int given ' );
117+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 1 );
118+ }
119+
120+ public function test_query_fetch_class_invalid_class_name (): void {
121+ $ this ->expectException ( TypeError::class );
122+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be a valid class ' );
123+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 'non-existent-class ' );
124+ }
125+
126+ public function test_query_fetch_class_invalid_constructor_args_type (): void {
127+ $ this ->expectException ( TypeError::class );
128+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #4 must be of type ?array, int given ' );
129+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 'stdClass ' , 1 );
130+ }
131+
132+ public function test_query_fetch_into_invalid_arg_count (): void {
133+ $ this ->expectException ( ArgumentCountError::class );
134+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 3 arguments for the fetch mode provided, 2 given ' );
135+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_INTO );
136+ }
137+
138+ public function test_query_fetch_into_invalid_object_type (): void {
139+ $ this ->expectException ( TypeError::class );
140+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type object, int given ' );
141+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_INTO , 1 );
22142 }
23143
24144 public function test_exec (): void {
@@ -92,4 +212,94 @@ public function test_rollback_no_active_transaction(): void {
92212 $ this ->expectExceptionCode ( 0 );
93213 $ this ->driver ->rollBack ();
94214 }
215+
216+ public function test_fetch_default (): void {
217+ // Default fetch mode is PDO::FETCH_BOTH.
218+ $ result = $ this ->driver ->query ( "SELECT 1, 'abc', 2 " );
219+ $ this ->assertSame (
220+ array (
221+ 1 => 1 ,
222+ 0 => 1 ,
223+ 'abc ' => 'abc ' ,
224+ '2 ' => 2 ,
225+ ),
226+ $ result ->fetch ()
227+ );
228+ }
229+
230+ /**
231+ * @dataProvider data_pdo_fetch_methods
232+ */
233+ public function test_fetch ( $ query , $ mode , $ expected ): void {
234+ $ stmt = $ this ->driver ->query ( $ query );
235+ $ result = $ stmt ->fetch ( $ mode );
236+ if ( is_object ( $ expected ) ) {
237+ $ this ->assertInstanceOf ( get_class ( $ expected ), $ result );
238+ $ this ->assertEquals ( $ expected , $ result );
239+ } else {
240+ $ this ->assertSame ( $ expected , $ result );
241+ }
242+ }
243+
244+ public function test_fetch_column_invalid_colno_value (): void {
245+ $ stmt = $ this ->driver ->query ( 'SELECT 1 ' );
246+ $ this ->expectException ( ValueError::class );
247+ $ this ->expectExceptionMessage ( 'Invalid column index ' );
248+ $ stmt ->fetchColumn ( 1 );
249+ }
250+
251+ public function data_pdo_fetch_methods (): Generator {
252+ // PDO::FETCH_BOTH
253+ yield 'PDO::FETCH_BOTH ' => array (
254+ "SELECT 1, 'abc', 2, 'two' as `2` " ,
255+ PDO ::FETCH_BOTH ,
256+ array (
257+ 1 => 1 ,
258+ 0 => 1 ,
259+ 'abc ' => 'abc ' ,
260+ '2 ' => 'two ' ,
261+ '3 ' => 'two ' ,
262+ ),
263+ );
264+
265+ // PDO::FETCH_NUM
266+ yield 'PDO::FETCH_NUM ' => array (
267+ "SELECT 1, 'abc', 2, 'two' as `2` " ,
268+ PDO ::FETCH_NUM ,
269+ array ( 1 , 'abc ' , 2 , 'two ' ),
270+ );
271+
272+ // PDO::FETCH_ASSOC
273+ yield 'PDO::FETCH_ASSOC ' => array (
274+ "SELECT 1, 'abc', 2, 'two' as `2` " ,
275+ PDO ::FETCH_ASSOC ,
276+ array (
277+ '1 ' => 1 ,
278+ 'abc ' => 'abc ' ,
279+ '2 ' => 'two ' ,
280+ ),
281+ );
282+
283+ // PDO::FETCH_NAMED
284+ yield 'PDO::FETCH_NAMED ' => array (
285+ "SELECT 1, 'abc', 2, 'two' as `2` " ,
286+ PDO ::FETCH_NAMED ,
287+ array (
288+ '1 ' => 1 ,
289+ 'abc ' => 'abc ' ,
290+ '2 ' => array ( 2 , 'two ' ),
291+ ),
292+ );
293+
294+ // PDO::FETCH_OBJ
295+ yield 'PDO::FETCH_OBJ ' => array (
296+ "SELECT 1, 'abc', 2, 'two' as `2` " ,
297+ PDO ::FETCH_OBJ ,
298+ (object ) array (
299+ '1 ' => 1 ,
300+ 'abc ' => 'abc ' ,
301+ '2 ' => 'two ' ,
302+ ),
303+ );
304+ }
95305}
0 commit comments