1313use mysqli_stmt ;
1414use PDO ;
1515use ReflectionClass ;
16+ use stdClass ;
1617
1718class ActiveRecordMysqliTest extends \PHPUnit \Framework \TestCase
1819{
@@ -75,7 +76,7 @@ public function testQuery()
7576 $ mysqli = $ this ->createMock (mysqli::class);
7677 $ mysqli_stmt = $ this ->createMock (mysqli_stmt::class);
7778 $ mysqli_result = $ this ->createMock (mysqli_result::class);
78- $ mysqli_result ->method ('fetch_assoc ' )->willReturn ( ['id ' => 1 , 'name ' => 'demo ' , 'password ' => md5 ('demo ' )]);
79+ $ mysqli_result ->method ('fetch_assoc ' )->will ( $ this -> onConsecutiveCalls ( ['id ' => 1 , 'name ' => 'demo ' , 'password ' => md5 ('demo ' )], false ) );
7980 $ mysqli_stmt ->method ('execute ' )->willReturn (true );
8081 $ mysqli_stmt ->method ('bind_param ' )->willReturn (true );
8182 $ mysqli_stmt ->method ('get_result ' )->willReturn ($ mysqli_result );
@@ -96,6 +97,68 @@ public function lastInsertId()
9697 $ this ->assertSame ('demo ' , $ user ->name );
9798 }
9899
100+ public function testQueryBadResult ()
101+ {
102+ $ mysqli_stmt = $ this ->createMock (mysqli_stmt::class);
103+ $ mysqli_stmt ->method ('get_result ' )->willReturn (false );
104+ $ MysqliStatementAdapter = new class ($ mysqli_stmt ) extends MysqliStatementAdapter {
105+ protected function getErrorList (): array
106+ {
107+ return [ [ 'sqlstate ' => 'HY000 ' , 'errno ' => 1 , 'error ' => 'No results found ' ] ];
108+ }
109+ };
110+ $ this ->expectException (Exception::class);
111+ $ this ->expectExceptionMessage ('No results found ' );
112+ $ obj = new stdClass ();
113+ $ MysqliStatementAdapter ->fetch ($ obj );
114+ }
115+
116+ public function testQueryWithFindAll ()
117+ {
118+ $ mysqli = $ this ->createMock (mysqli::class);
119+ $ mysqli_stmt = $ this ->createMock (mysqli_stmt::class);
120+ $ mysqli_result = $ this ->createMock (mysqli_result::class);
121+ $ mysqli_result ->method ('fetch_assoc ' )->will ($ this ->onConsecutiveCalls (
122+ ['id ' => 1 , 'name ' => 'demo ' , 'password ' => md5 ('demo ' )],
123+ ['id ' => 2 , 'name ' => 'demo2 ' , 'password ' => md5 ('demo2 ' )],
124+ false
125+ ));
126+ $ mysqli_stmt ->method ('execute ' )->willReturn (true );
127+ $ mysqli_stmt ->method ('bind_param ' )->willReturn (true );
128+ $ mysqli_stmt ->method ('get_result ' )->willReturn ($ mysqli_result );
129+ $ mysqli ->method ('prepare ' )->willReturn ($ mysqli_stmt );
130+ $ connection = new class ($ mysqli ) extends MysqliAdapter {
131+ public function lastInsertId ()
132+ {
133+ return 1 ;
134+ }
135+ };
136+ $ user = new User ($ connection );
137+ $ users = $ user ->isNotNull ('id ' )->gt ('id ' , 0 )->findAll ();
138+ $ this ->assertCount (2 , $ users );
139+ $ this ->assertSame (1 , $ users [0 ]->id );
140+ $ this ->assertSame ('demo ' , $ users [0 ]->name );
141+ $ this ->assertSame (2 , $ users [1 ]->id );
142+ $ this ->assertSame ('demo2 ' , $ users [1 ]->name );
143+ }
144+
145+ public function testQueryWithFindAllNoResults ()
146+ {
147+ $ mysqli = $ this ->createMock (mysqli::class);
148+ $ mysqli_stmt = $ this ->createMock (mysqli_stmt::class);
149+ $ mysqli_result = $ this ->createMock (mysqli_result::class);
150+ $ mysqli_result ->method ('fetch_assoc ' )->willReturn (false );
151+ $ mysqli_stmt ->method ('execute ' )->willReturn (true );
152+ $ mysqli_stmt ->method ('bind_param ' )->willReturn (true );
153+ $ mysqli_stmt ->method ('get_result ' )->willReturn ($ mysqli_result );
154+ $ mysqli ->method ('prepare ' )->willReturn ($ mysqli_stmt );
155+ $ connection = new class ($ mysqli ) extends MysqliAdapter {
156+ };
157+ $ user = new User ($ connection );
158+ $ users = $ user ->isNotNull ('id ' )->gt ('id ' , 0 )->findAll ();
159+ $ this ->assertEmpty ($ users );
160+ }
161+
99162 public function testDelete ()
100163 {
101164 $ mysqli = $ this ->createMock (mysqli::class);
@@ -139,7 +202,7 @@ public function testFetchWithParams()
139202 {
140203 $ mysqli_stmt = $ this ->createMock (mysqli_stmt::class);
141204 $ mysqli_result = $ this ->createMock (mysqli_result::class);
142- $ mysqli_result ->method ('fetch_assoc ' )->willReturn ( ['id ' => 1 , 'name ' => 'demo ' , 'password ' => md5 ('demo ' )]);
205+ $ mysqli_result ->method ('fetch_assoc ' )->will ( $ this -> onConsecutiveCalls ( ['id ' => 1 , 'name ' => 'demo ' , 'password ' => md5 ('demo ' )], false ) );
143206 $ mysqli_stmt ->method ('get_result ' )->willReturn ($ mysqli_result );
144207
145208 $ MysqliStatementAdapter = new class ($ mysqli_stmt ) extends MysqliStatementAdapter {
0 commit comments