diff --git a/library/Icinga/File/Csv.php b/library/Icinga/File/Csv.php index 448327806..f1008d611 100644 --- a/library/Icinga/File/Csv.php +++ b/library/Icinga/File/Csv.php @@ -3,15 +3,17 @@ namespace Icinga\File; +use Traversable; + class Csv { protected $query; protected function __construct() {} - public static function fromQuery($query) + public static function fromQuery(Traversable $query) { - $csv = new Csv(); + $csv = new static(); $csv->query = $query; return $csv; } @@ -26,7 +28,7 @@ class Csv { $first = true; $csv = ''; - foreach ($this->query->fetchAll() as $row) { + foreach ($this->query as $row) { if ($first) { $csv .= implode(',', array_keys((array) $row)) . "\r\n"; $first = false; diff --git a/test/php/library/Icinga/File/CsvTest.php b/test/php/library/Icinga/File/CsvTest.php index 6eaf6bad0..048e3b073 100644 --- a/test/php/library/Icinga/File/CsvTest.php +++ b/test/php/library/Icinga/File/CsvTest.php @@ -3,7 +3,7 @@ namespace Tests\Icinga\File; -use Mockery; +use Icinga\Data\DataArray\ArrayDatasource; use Icinga\File\Csv; use Icinga\Test\BaseTestCase; @@ -11,19 +11,15 @@ class CsvTest extends BaseTestCase { public function testWhetherValidCsvIsRendered() { - $queryMock = Mockery::mock( - 'Icinga\Data\SimpleQuery', - array( - 'fetchAll' => array( - array('col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3', 'col4' => 'val4'), - array('col1' => 'val5', 'col2' => 'val6', 'col3' => 'val7', 'col4' => 'val8') - ) - ) - ); - $csv = Csv::fromQuery($queryMock); + $data = new ArrayDatasource(array( + array('col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3', 'col4' => 'val4'), + array('col1' => 'val5', 'col2' => 'val6', 'col3' => 'val7', 'col4' => 'val8') + )); + + $csv = Csv::fromQuery($data->select()); $this->assertEquals( - join( + implode( "\r\n", array( 'col1,col2,col3,col4',