Skip to content

Commit

Permalink
[BUGFIX] Ensure valid arguments for array_chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
bjo3rnf committed Jul 20, 2014
1 parent ae8df67 commit dbed821
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Classes/ViewHelpers/Iterator/ChunkViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
98 changes: 98 additions & 0 deletions Tests/Unit/ViewHelpers/Iterator/ChunkViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Iterator;

/***************************************************************
* Copyright notice
*
* (c) 2014 Claus Due <claus@namelesscoder.net>
*
* 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 <fromme@dreipunktnull.com>
* @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);
}

}

0 comments on commit dbed821

Please sign in to comment.