Skip to content

Commit

Permalink
#22 Add cuurent_field, fetch_field_direct, and fetch_fields
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
sbuberl committed May 27, 2019
1 parent 8c3c558 commit 1786d60
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public function field_count()
return count($this->columnNames);
}

public function current_field()
{
return $this->columnsIndex;
}

public function fetch_field()
{
$pos = $this->columnsIndex;
Expand All @@ -127,6 +132,27 @@ public function fetch_field()
return $field;
}

public function fetch_field_direct($fieldPos)
{
if (!isset($this->columnNames[$fieldPos])) {
return false;
}
$field = new \stdClass();
$field->name = $this->columnNames[$fieldPos];
return $field;
}

public function fetch_fields()
{
$fields = [];
foreach ($this->columnNames as $fieldName) {
$field = new \stdClass();
$field->name = $fieldName;
$fields[] = $field;
}
return $fields;
}

public function field_seek($i)
{
if (!isset($this->columnNames[$i])) {
Expand Down
34 changes: 33 additions & 1 deletion tests/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function testNumRows()
$this->assertEquals(count(self::$entries), $set->num_rows());
}

public function testNumFields()
public function testFieldCount()
{
$set = new ResultSet(self::$columns, self::$entries);
$this->assertEquals(count(self::$columns), $set->field_count());
Expand All @@ -157,6 +157,38 @@ public function testFetchField()
$this->assertEquals(count(self::$columns), $i);
}

public function testFetchFieldDirect()
{
$results = new ResultSet(self::$columns, self::$entries);

$field = $results->fetch_field_direct(2);
$this->assertTrue(is_object($field));
$this->assertEquals(self::$columns[2], $field->name);
}

public function testFetchFields()
{
$results = new ResultSet(self::$columns, self::$entries);

$fields = $results->fetch_fields();

$expected = [];
while (($field = $results->fetch_field()) !== false) {
$expected[] = $field;
}
$this->assertEquals($expected, $fields);
}

public function testCurrentField()
{
$results = new ResultSet(self::$columns, self::$entries);

$i = 1;
while (($field = $results->fetch_field()) !== false) {
$this->assertEquals($i++, $results->current_field());
}
}

public function testFieldSeekBad()
{
$results = new ResultSet(self::$columns, self::$entries);
Expand Down

0 comments on commit 1786d60

Please sign in to comment.