[icinga-checkins] icinga.org: icingaweb2/master: Use the query's iterator instead fetchAll() on CSV export

git at icinga.org git at icinga.org
Tue Oct 11 09:54:39 CEST 2016


Module: icingaweb2
Branch: master
Commit: 529ba8ed8ae0752fa4623f3a46c492dc974982e0
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=529ba8ed8ae0752fa4623f3a46c492dc974982e0

Author: Eric Lippmann <eric.lippmann at netways.de>
Date:   Tue Oct 11 09:53:21 2016 +0200

Use the query's iterator instead fetchAll() on CSV export

fixes #12723

---

 library/Icinga/File/Csv.php              |    8 +++++---
 test/php/library/Icinga/File/CsvTest.php |   20 ++++++++------------
 2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/library/Icinga/File/Csv.php b/library/Icinga/File/Csv.php
index 4483278..f1008d6 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 6eaf6ba..048e3b0 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',



More information about the icinga-checkins mailing list