Skip to content

Commit

Permalink
Adds tests for iterator readers
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 27, 2015
1 parent 89ab731 commit 7c263de
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Reader/CountableIteratorReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class CountableIteratorReader extends IteratorReader implements CountableReader
*/
public function count()
{
if ($this->iterator instanceof \Countable) {
return count($this->iterator);
$iterator = $this->getInnerIterator();

if ($iterator instanceof \Countable) {
return count($iterator);
}

return iterator_count($this->iterator);
return iterator_count($iterator);
}
}
44 changes: 44 additions & 0 deletions tests/Reader/CountableIteratorReaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ddeboer\DataImport\Tests\Reader;

use Ddeboer\DataImport\Reader\CountableIteratorReader;

/**
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class CountableIteratorReaderTest extends \PHPUnit_Framework_TestCase
{
public function testCount()
{
$iterator = new \ArrayIterator([
[
'id' => 1,
'username' => 'john.doe',
'name' => 'John Doe',
],
]);

$reader = new CountableIteratorReader($iterator);

// We need to rewind the iterator
$reader->rewind();

$this->assertEquals(1, $reader->count());
}

public function testIteratorCount()
{
$reader = new CountableIteratorReader(new CountableIterator);

// We need to rewind the iterator
$reader->rewind();

$this->assertEquals(0, $reader->count());
}
}

class CountableIterator extends \EmptyIterator
{

}
32 changes: 32 additions & 0 deletions tests/Reader/IteratorReaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Ddeboer\DataImport\Tests\Reader;

use Ddeboer\DataImport\Reader\IteratorReader;

/**
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class IteratorReaderTest extends \PHPUnit_Framework_TestCase
{
public function testGetFields()
{
$iterator = new \ArrayIterator([
[
'id' => 1,
'username' => 'john.doe',
'name' => 'John Doe',
],
]);

$reader = new IteratorReader($iterator);

// We need to rewind the iterator
$reader->rewind();

$fields = $reader->getFields();

$this->assertInternalType('array', $fields);
$this->assertEquals(['id', 'username', 'name'], $fields);
}
}

0 comments on commit 7c263de

Please sign in to comment.