diff --git a/Classes/ViewHelpers/Iterator/ChunkViewHelper.php b/Classes/ViewHelpers/Iterator/ChunkViewHelper.php index d95f2e78f..525ff98aa 100644 --- a/Classes/ViewHelpers/Iterator/ChunkViewHelper.php +++ b/Classes/ViewHelpers/Iterator/ChunkViewHelper.php @@ -55,10 +55,16 @@ public function render($count, $fixed = FALSE, $subject = NULL, $as = NULL) { } elseif (FALSE === is_array($subject)) { throw new \Exception('Cannot get values of unsupported type: ' . gettype($subject), 1357098192); } + $output = array(); + if (0 >= $count) { + return $output; + } if (TRUE === (boolean) $fixed) { $subjectSize = count($subject); - $chunkSize = ceil($subjectSize / $count); - $output = array_chunk($subject, $chunkSize); + if (0 < $subjectSize) { + $chunkSize = ceil($subjectSize / $count); + $output = array_chunk($subject, $chunkSize); + } // Fill the resulting array with empty items to get the desired element count $elementCount = count($output); if ($elementCount < $count) { diff --git a/Tests/Unit/ViewHelpers/Iterator/ChunkViewHelperTest.php b/Tests/Unit/ViewHelpers/Iterator/ChunkViewHelperTest.php new file mode 100644 index 000000000..4b641ca0d --- /dev/null +++ b/Tests/Unit/ViewHelpers/Iterator/ChunkViewHelperTest.php @@ -0,0 +1,98 @@ + + * + * All rights reserved + * + * This script is part of the TYPO3 project. The TYPO3 project is + * free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * The GNU General Public License can be found at + * http://www.gnu.org/copyleft/gpl.html. + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + * ************************************************************* */ +use FluidTYPO3\Vhs\ViewHelpers\AbstractViewHelperTest; + +/** + * @protection on + * @author Björn Fromme + * @package Vhs + */ +class ChunkViewHelperTest extends AbstractViewHelperTest { + + /** + * @test + */ + public function returnsConfiguredItemNumberIfFixed() { + $arguments = array( + 'count' => 5, + 'fixed' => TRUE, + 'subject' => array('a', 'b', 'c', 'd', 'e'), + ); + $result = $this->executeViewHelper($arguments); + $this->assertCount(5, $result); + } + + /** + * @test + */ + public function returnsConfiguredItemNumberIfFixedAndSubjectIsEmpty() { + $arguments = array( + 'count' => 5, + 'fixed' => TRUE, + 'subject' => array(), + ); + $result = $this->executeViewHelper($arguments); + $this->assertCount(5, $result); + } + + /** + * @test + */ + public function returnsExpectedItemNumberIfNotFixed() { + $arguments = array( + 'count' => 4, + 'subject' => array('a', 'b', 'c', 'd', 'e'), + ); + $result = $this->executeViewHelper($arguments); + $this->assertCount(2, $result); + } + + /** + * @test + */ + public function returnsEmptyResultForEmptySubjectAndNotFixed() { + $arguments = array( + 'count' => 5, + 'subject' => array(), + ); + $result = $this->executeViewHelper($arguments); + $this->assertCount(0, $result); + } + + /** + * @test + */ + public function returnsEmptyResultForZeroCount() { + $arguments = array( + 'count' => 0, + 'subject' => array('a', 'b', 'c', 'd', 'e'), + ); + $result = $this->executeViewHelper($arguments); + $this->assertCount(0, $result); + } + +}