@@ -16,9 +16,91 @@ public function test_connection(): void {
1616 }
1717
1818 public function test_query (): void {
19+ // No extra parameters.
1920 $ result = $ this ->driver ->query ( 'SELECT 1 ' );
2021 $ this ->assertInstanceOf ( PDOStatement::class, $ result );
21- $ this ->assertEquals ( 1 , $ result ->fetchColumn () );
22+ $ this ->assertSame ( '1 ' , $ result ->fetchColumn () ); // TODO: This should be int (without ATTR_STRINGIFY_FETCHES).
23+ }
24+
25+ public function test_query_fetch_mode (): void {
26+ // With FETCH_ASSOC fetch mode.
27+ $ result = $ this ->driver ->query ( 'SELECT 1 AS col ' , PDO ::FETCH_ASSOC );
28+ $ this ->assertSame ( array ( array ( 'col ' => '1 ' ) ), $ result ->fetchAll () ); // TODO: This should be int (without ATTR_STRINGIFY_FETCHES).
29+
30+ // With FETCH_COLUMN mode.
31+ $ result = $ this ->driver ->query ( 'SELECT 1 AS col ' , PDO ::FETCH_COLUMN , 0 );
32+ $ this ->assertSame ( array ( '1 ' ), $ result ->fetchAll () ); // TODO: This should be int (without ATTR_STRINGIFY_FETCHES).
33+
34+ // With FETCH_COLUMN mode and column number.
35+ $ result = $ this ->driver ->query ( 'SELECT 1, 2 ' , PDO ::FETCH_COLUMN , 1 );
36+ //$this->assertSame( array( '2' ), $result->fetchAll() ); // TODO: This should be int (without ATTR_STRINGIFY_FETCHES).
37+ }
38+
39+ public function test_query_fetch_mode_invalid_arg_count (): void {
40+ $ this ->expectException ( ArgumentCountError::class );
41+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 2 arguments for the fetch mode provided, 3 given ' );
42+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_ASSOC , 0 );
43+ }
44+
45+ public function test_query_fetch_default_mode_allow_any_args (): void {
46+ // TODO: All the results below should be int (without ATTR_STRINGIFY_FETCHES).
47+
48+ $ result = $ this ->driver ->query ( 'SELECT 1 ' );
49+ $ this ->assertSame ( array ( '1 ' ), $ result ->fetchAll () );
50+
51+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null );
52+ $ this ->assertSame ( array ( '1 ' ), $ result ->fetchAll () );
53+
54+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 1 );
55+ $ this ->assertSame ( array ( '1 ' ), $ result ->fetchAll () );
56+
57+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 'abc ' );
58+ $ this ->assertSame ( array ( '1 ' ), $ result ->fetchAll () );
59+
60+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 1 , 2 , 'abc ' , array (), true );
61+ $ this ->assertSame ( array ( '1 ' ), $ result ->fetchAll () );
62+ }
63+
64+ public function test_query_fetch_column_invalid_arg_count (): void {
65+ $ this ->expectException ( ArgumentCountError::class );
66+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 3 arguments for the fetch mode provided, 2 given ' );
67+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_COLUMN );
68+ }
69+
70+ public function test_query_fetch_column_invalid_colno_type (): void {
71+ $ this ->expectException ( TypeError::class );
72+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type int, string given ' );
73+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_COLUMN , '0 ' );
74+ }
75+
76+ public function test_query_fetch_class_not_enough_args (): void {
77+ $ this ->expectException ( ArgumentCountError::class );
78+ $ this ->expectExceptionMessage ( 'PDO::query() expects at least 3 arguments for the fetch mode provided, 2 given ' );
79+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS );
80+ }
81+
82+ public function test_query_fetch_class_too_many_args (): void {
83+ $ this ->expectException ( ArgumentCountError::class );
84+ $ this ->expectExceptionMessage ( 'PDO::query() expects at most 4 arguments for the fetch mode provided, 5 given ' );
85+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , '\stdClass ' , array (), array () );
86+ }
87+
88+ public function test_query_fetch_class_invalid_class_type (): void {
89+ $ this ->expectException ( TypeError::class );
90+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type string, int given ' );
91+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 1 );
92+ }
93+
94+ public function test_query_fetch_class_invalid_class_name (): void {
95+ $ this ->expectException ( TypeError::class );
96+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be a valid class ' );
97+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 'non-existent-class ' );
98+ }
99+
100+ public function test_query_fetch_class_invalid_constructor_args_type (): void {
101+ $ this ->expectException ( TypeError::class );
102+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #4 must be of type ?array, int given ' );
103+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 'stdClass ' , 1 );
22104 }
23105
24106 public function test_exec (): void {
0 commit comments