@@ -32,6 +32,106 @@ public function test_query(): void {
3232 );
3333 }
3434
35+ /**
36+ * @dataProvider data_pdo_fetch_methods
37+ */
38+ public function test_query_with_fetch_mode ( $ query , $ mode , $ expected ): void {
39+ $ stmt = $ this ->driver ->query ( $ query , $ mode );
40+ $ result = $ stmt ->fetch ();
41+ if ( is_object ( $ expected ) ) {
42+ $ this ->assertInstanceOf ( get_class ( $ expected ), $ result );
43+ $ this ->assertEquals ( $ expected , $ result );
44+ } else {
45+ $ this ->assertSame ( $ expected , $ result );
46+ }
47+
48+ $ this ->assertFalse ( $ stmt ->fetch () );
49+ }
50+
51+ public function test_query_fetch_mode_not_set (): void {
52+ $ result = $ this ->driver ->query ( 'SELECT 1 ' );
53+ $ this ->assertSame (
54+ array (
55+ '1 ' => 1 ,
56+ 0 => 1 ,
57+ ),
58+ $ result ->fetch ()
59+ );
60+ $ this ->assertFalse ( $ result ->fetch () );
61+ }
62+
63+ public function test_query_fetch_mode_invalid_arg_count (): void {
64+ $ this ->expectException ( ArgumentCountError::class );
65+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 2 arguments for the fetch mode provided, 3 given ' );
66+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_ASSOC , 0 );
67+ }
68+
69+ public function test_query_fetch_default_mode_allow_any_args (): void {
70+ $ expected_result = array (
71+ array (
72+ 1 => 1 ,
73+ 0 => 1 ,
74+ ),
75+ );
76+
77+ $ result = $ this ->driver ->query ( 'SELECT 1 ' );
78+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
79+
80+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null );
81+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
82+
83+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 1 );
84+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
85+
86+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 'abc ' );
87+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
88+
89+ $ result = $ this ->driver ->query ( 'SELECT 1 ' , null , 1 , 2 , 'abc ' , array (), true );
90+ $ this ->assertSame ( $ expected_result , $ result ->fetchAll () );
91+ }
92+
93+ public function test_query_fetch_class_not_enough_args (): void {
94+ $ this ->expectException ( ArgumentCountError::class );
95+ $ this ->expectExceptionMessage ( 'PDO::query() expects at least 3 arguments for the fetch mode provided, 2 given ' );
96+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS );
97+ }
98+
99+ public function test_query_fetch_class_too_many_args (): void {
100+ $ this ->expectException ( ArgumentCountError::class );
101+ $ this ->expectExceptionMessage ( 'PDO::query() expects at most 4 arguments for the fetch mode provided, 5 given ' );
102+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , '\stdClass ' , array (), array () );
103+ }
104+
105+ public function test_query_fetch_class_invalid_class_type (): void {
106+ $ this ->expectException ( TypeError::class );
107+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type string, int given ' );
108+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 1 );
109+ }
110+
111+ public function test_query_fetch_class_invalid_class_name (): void {
112+ $ this ->expectException ( TypeError::class );
113+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be a valid class ' );
114+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 'non-existent-class ' );
115+ }
116+
117+ public function test_query_fetch_class_invalid_constructor_args_type (): void {
118+ $ this ->expectException ( TypeError::class );
119+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #4 must be of type ?array, int given ' );
120+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_CLASS , 'stdClass ' , 1 );
121+ }
122+
123+ public function test_query_fetch_into_invalid_arg_count (): void {
124+ $ this ->expectException ( ArgumentCountError::class );
125+ $ this ->expectExceptionMessage ( 'PDO::query() expects exactly 3 arguments for the fetch mode provided, 2 given ' );
126+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_INTO );
127+ }
128+
129+ public function test_query_fetch_into_invalid_object_type (): void {
130+ $ this ->expectException ( TypeError::class );
131+ $ this ->expectExceptionMessage ( 'PDO::query(): Argument #3 must be of type object, int given ' );
132+ $ this ->driver ->query ( 'SELECT 1 ' , PDO ::FETCH_INTO , 1 );
133+ }
134+
35135 public function test_exec (): void {
36136 $ result = $ this ->driver ->exec ( 'SELECT 1 ' );
37137 $ this ->assertEquals ( 0 , $ result );
0 commit comments