From 3f3461b00206bdef187598c02425054bd44c4774 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Thu, 16 Apr 2015 14:50:50 -0700 Subject: [PATCH] Add and improve test coverage --- .gitignore | 1 + phpunit.xml | 10 +++ src/Spout/Common/Escaper/CSV.php | 4 ++ .../Common/Helper/GlobalFunctionsHelper.php | 2 + src/Spout/Reader/Helper/XLSX/CellHelper.php | 2 +- src/Spout/Writer/AbstractWriter.php | 2 + .../Reader/Helper/XLSX/CellHelperTest.php | 60 ++++++++++++++++++ tests/Spout/Reader/ReaderFactoryTest.php | 21 ++++++ tests/Spout/Reader/XLSXTest.php | 15 ++++- tests/Spout/Writer/CSVTest.php | 10 +++ tests/Spout/Writer/WriterFactoryTest.php | 21 ++++++ ...et_with_preserve_space_shared_strings.xlsx | Bin 0 -> 3637 bytes 12 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 tests/Spout/Reader/Helper/XLSX/CellHelperTest.php create mode 100644 tests/Spout/Reader/ReaderFactoryTest.php create mode 100644 tests/Spout/Writer/WriterFactoryTest.php create mode 100644 tests/resources/xlsx/sheet_with_preserve_space_shared_strings.xlsx diff --git a/.gitignore b/.gitignore index f8600d9..2760edd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /tests/resources/generated +/tests/coverage /vendor composer.lock /.idea diff --git a/phpunit.xml b/phpunit.xml index 975acc6..5de1e0f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,4 +14,14 @@ + + + + + + + src/ + + + diff --git a/src/Spout/Common/Escaper/CSV.php b/src/Spout/Common/Escaper/CSV.php index efaebd6..4bc2d1a 100644 --- a/src/Spout/Common/Escaper/CSV.php +++ b/src/Spout/Common/Escaper/CSV.php @@ -13,6 +13,8 @@ class CSV implements EscaperInterface /** * Escapes the given string to make it compatible with CSV * + * @codeCoverageIgnore + * * @param string $string The string to escape * @return string The escaped string */ @@ -24,6 +26,8 @@ class CSV implements EscaperInterface /** * Unescapes the given string to make it compatible with CSV * + * @codeCoverageIgnore + * * @param string $string The string to unescape * @return string The unescaped string */ diff --git a/src/Spout/Common/Helper/GlobalFunctionsHelper.php b/src/Spout/Common/Helper/GlobalFunctionsHelper.php index 8ce7ab5..5f22ed6 100644 --- a/src/Spout/Common/Helper/GlobalFunctionsHelper.php +++ b/src/Spout/Common/Helper/GlobalFunctionsHelper.php @@ -6,6 +6,8 @@ namespace Box\Spout\Common\Helper; * Class GlobalFunctionsHelper * This class wraps global functions to facilitate testing * + * @codeCoverageIgnore + * * @package Box\Spout\Common\Helper */ class GlobalFunctionsHelper diff --git a/src/Spout/Reader/Helper/XLSX/CellHelper.php b/src/Spout/Reader/Helper/XLSX/CellHelper.php index 5802bff..7125ded 100644 --- a/src/Spout/Reader/Helper/XLSX/CellHelper.php +++ b/src/Spout/Reader/Helper/XLSX/CellHelper.php @@ -25,7 +25,7 @@ class CellHelper { $existingIndexes = array_keys($dataArray); - $newIndexes = array_fill_keys(range(min($existingIndexes), max($existingIndexes)), $fillValue); + $newIndexes = array_fill_keys(range(0, max($existingIndexes)), $fillValue); $dataArray += $newIndexes; ksort($dataArray); diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index 64f7440..68d452f 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -89,6 +89,8 @@ abstract class AbstractWriter implements WriterInterface * Inits the writer and opens it to accept data. * By using this method, the data will be outputted directly to the browser. * + * @codeCoverageIgnore + * * @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept * @return \Box\Spout\Writer\AbstractWriter * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened diff --git a/tests/Spout/Reader/Helper/XLSX/CellHelperTest.php b/tests/Spout/Reader/Helper/XLSX/CellHelperTest.php new file mode 100644 index 0000000..8851b33 --- /dev/null +++ b/tests/Spout/Reader/Helper/XLSX/CellHelperTest.php @@ -0,0 +1,60 @@ + 1, 3 => 3]; + $filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL'); + + $expectedFilledArray = ['FILL', 1, 'FILL', 3]; + $this->assertEquals($expectedFilledArray, $filledArray); + } + + /** + * @return array + */ + public function dataProviderForTestGetColumnIndexFromCellIndex() + { + return [ + ['A1', 0], + ['Z3', 25], + ['AA5', 26], + ['AB24', 27], + ['BC5', 54], + ['BCZ99', 1455], + ]; + } + + /** + * @dataProvider dataProviderForTestGetColumnIndexFromCellIndex + * + * @param string $cellIndex + * @param int $expectedColumnIndex + * @return void + */ + public function testGetColumnIndexFromCellIndex($cellIndex, $expectedColumnIndex) + { + $this->assertEquals($expectedColumnIndex, CellHelper::getColumnIndexFromCellIndex($cellIndex)); + } + + /** + * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException + * + * @return void + */ + public function testGetColumnIndexFromCellIndexShouldThrowIfInvalidCellIndex() + { + CellHelper::getColumnIndexFromCellIndex('InvalidCellIndex'); + } +} diff --git a/tests/Spout/Reader/ReaderFactoryTest.php b/tests/Spout/Reader/ReaderFactoryTest.php new file mode 100644 index 0000000..57a0b55 --- /dev/null +++ b/tests/Spout/Reader/ReaderFactoryTest.php @@ -0,0 +1,21 @@ +assertEquals($expectedRows, $allRows); } + /** + * @return void + */ + public function testReadShouldPreserveSpaceIfSpecified() + { + $allRows = $this->getAllRowsForFile('sheet_with_preserve_space_shared_strings.xlsx'); + + $expectedRows = [ + [' s1--A1', 's1--B1 ', ' s1--C1 '], + ]; + $this->assertEquals($expectedRows, $allRows); + } + /** * @return void */ @@ -171,7 +184,7 @@ class XLSXTest extends \PHPUnit_Framework_TestCase { $allRows = $this->getAllRowsForFile('billion_laughs_test_file.xlsx'); - $expectedMaxMemoryUsage = 10 * 1024 * 1024; // 10MB + $expectedMaxMemoryUsage = 20 * 1024 * 1024; // 20MB $this->assertLessThan($expectedMaxMemoryUsage, memory_get_peak_usage(true), 'Entities should not be expanded and therefore consume all the memory.'); $expectedFirstRow = ['s1--A1', 's1--B1', 's1--C1', 's1--D1', 's1--E1']; diff --git a/tests/Spout/Writer/CSVTest.php b/tests/Spout/Writer/CSVTest.php index 2d1b207..ef71b87 100644 --- a/tests/Spout/Writer/CSVTest.php +++ b/tests/Spout/Writer/CSVTest.php @@ -49,6 +49,16 @@ class CSVTest extends \PHPUnit_Framework_TestCase $writer->close(); } + /** + * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException + */ + public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays() + { + $writer = WriterFactory::create(Type::CSV); + $writer->addRows(['csv--11', 'csv--12']); + $writer->close(); + } + /** * @return void */ diff --git a/tests/Spout/Writer/WriterFactoryTest.php b/tests/Spout/Writer/WriterFactoryTest.php new file mode 100644 index 0000000..528eff3 --- /dev/null +++ b/tests/Spout/Writer/WriterFactoryTest.php @@ -0,0 +1,21 @@ +qnGwdyFm3kZQDg-d5dNtf<E%0*aH7gp5Vez;I6_ zPhh8HX$?3tT{=btTIt=Y%r_{dqLZuT9^36#!TOd}=yn^9&D0Q|yi{37DvqD9d6E+# z>Y?beS8spukaq&v$2vd={DW z<$Qt<>z;^=+{NrUB+c)=CL+6f;0pO8TyC#M(KG5}g<6nj&0${L+*chLAy6#3Er=YP5q@ z@D>7mDYhx~N-$@(Lo{zkk9(iW9YUW(z!x*3$CuUSY-s6>tG~jtpW6i#EQ-#0@D9-7 zNXa8I>N(@dX|-6`nV?zS-n+MBV!ee#>hAb?WoJ_#@H#(nTyo90MpVmF2%D37ni9>& zPRF6Sq>9RXF(g~Y@5g=yHLA!P`CWw=XMV9fzNen6gk;>d!(%@)dVq(uy_LR`y~AE? z&;h93+Wn5$ zilKmNKb<&A7qohJsla8irVP@&;SD^#8H(FuWIY${J!m7~aA!ru!D5T>G~`@Bjy zcGA(ce_e%YNwd^z#NN~+&ZN5I{+CE!!_Nv{CoPfUAJ zvB?K&CM_>#K@&oIhtHlSu!J`dE+w;KKJXhNyqrO%4;^zx?%pYcbg2d_oQxc7vugj^3F zOa7D;ABPa*@~WC;t8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