Skip to content
Permalink
Browse files

Convert iterators into arrays before mucking about with them.

$data['options'] might be an iterator. Convert it to an array before
adding empty options or converting to option elements.
  • Loading branch information...
markstory committed Jan 8, 2014
1 parent 9a8f6db commit 462be3e36682128c7530e6069c486e9bce138d05
Showing with 28 additions and 0 deletions.
  1. +4 −0 Cake/View/Input/SelectBox.php
  2. +24 −0 Test/TestCase/View/Input/SelectBoxTest.php
@@ -99,6 +99,10 @@ protected function _renderContent($data) {
$out = [];
$options = $data['options'];
if ($options instanceof Traversable) {
$options = iterator_to_array($options);
}
if (!empty($data['empty'])) {
$value = $data['empty'] === true ? '' : $data['empty'];
$options = ['' => $value] + $options;
@@ -76,6 +76,30 @@ public function testRenderSimple() {
$this->assertTags($result, $expected);
}
/**
* test simple iterator rendering
*
* @return void
*/
public function testRenderSimpleIterator() {
$select = new SelectBox($this->templates);
$options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
$data = [
'name' => 'Birds[name]',
'options' => $options,
'empty' => true
];
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]'],
['option' => ['value' => '']], '/option',
['option' => ['value' => 'a']], 'Albatross', '/option',
['option' => ['value' => 'b']], 'Budgie', '/option',
'/select'
];
$this->assertTags($result, $expected);
}
/**
* test complex option rendering
*

0 comments on commit 462be3e

Please sign in to comment.
You can’t perform that action at this time.