@@ -88,6 +88,44 @@ public function testCountTwice() {
8888 self ::assertCount ($ count , $ resultSet );
8989 }
9090
91+ public function testGetFieldList () {
92+ $ resultSet = new ResultSet ($ this ->getStatementMock ());
93+ self ::assertSame (
94+ ["id " , "name " , "timestamp " , "date " ],
95+ $ resultSet ->getFieldList ()
96+ );
97+ }
98+
99+ public function testGetFieldListFallbackToFirstRow () {
100+ $ resultSet = new ResultSet ($ this ->getStatementMock (false ));
101+ self ::assertSame (
102+ ["id " , "name " , "timestamp " , "date " ],
103+ $ resultSet ->getFieldList ()
104+ );
105+ }
106+
107+ public function testGetFieldListNoRows () {
108+ $ statement = $ this ->createMock (PDOStatement::class);
109+ $ statement ->method ("fetch " )->willReturn (null );
110+ $ statement ->method ("execute " )->willReturn (true );
111+ $ statement ->method ("columnCount " )->willReturn (0 );
112+ $ resultSet = new ResultSet ($ statement );
113+
114+ self ::assertSame ([], $ resultSet ->getFieldList ());
115+ }
116+
117+ public function testGetFieldListMidIteration () {
118+ $ resultSet = new ResultSet ($ this ->getStatementMock ());
119+ $ resultSet ->rewind ();
120+ $ resultSet ->next ();
121+
122+ self ::assertSame (
123+ ["id " , "name " , "timestamp " , "date " ],
124+ $ resultSet ->getFieldList ()
125+ );
126+ self ::assertEquals (2 , $ resultSet ->current ()->id );
127+ }
128+
91129 public function testAccessByRowIndex () {
92130 $ resultSet = new ResultSet ($ this ->getStatementMock ());
93131
@@ -137,12 +175,25 @@ public function testAsDateTime() {
137175 self ::assertEquals ("1988-04-05 17:24 " , $ row ->getDateTime ("timestamp " )->format ("Y-m-d H:i " ));
138176 }
139177
140- private function getStatementMock ():PDOStatement {
178+ private function getStatementMock (bool $ withMetadata = true ):PDOStatement {
141179 $ statement = $ this ->createMock (PDOStatement::class);
142180 $ statement ->method ("fetch " )
143181 ->will (self ::returnCallback ([$ this , "getNextFakeData " ]));
144182 $ statement ->method ("execute " )
145183 ->willReturnCallback ([$ this , "rewindFakeData " ]);
184+ if ($ withMetadata ) {
185+ $ statement ->method ("columnCount " )
186+ ->willReturn (count (self ::FAKE_DATA [0 ]));
187+ $ statement ->method ("getColumnMeta " )
188+ ->willReturnCallback (function (int $ index ):array {
189+ $ name = array_keys (self ::FAKE_DATA [0 ])[$ index ] ?? null ;
190+ return ["name " => $ name ];
191+ });
192+ }
193+ else {
194+ $ statement ->method ("columnCount " )
195+ ->willReturn (0 );
196+ }
146197
147198 return $ statement ;
148199 }
0 commit comments