Browse Source

Fixed SelectBox raising exception with null options

Nicolas Rod 11 years ago
parent
commit
032b26c322
2 changed files with 38 additions and 1 deletions
  1. 1 1
      src/View/Widget/SelectBox.php
  2. 37 0
      tests/TestCase/View/Widget/SelectBoxTest.php

+ 1 - 1
src/View/Widget/SelectBox.php

@@ -164,7 +164,7 @@ class SelectBox implements WidgetInterface {
 
 		if (!empty($data['empty'])) {
 			$value = $data['empty'] === true ? '' : $data['empty'];
-			$options = ['' => $value] + $options;
+			$options = ['' => $value] + (array)$options;
 		}
 		if (empty($options)) {
 			return [];

+ 37 - 0
tests/TestCase/View/Widget/SelectBoxTest.php

@@ -661,4 +661,41 @@ class SelectBoxTest extends TestCase {
 		$this->assertHtml($expected, $result);
 	}
 
+/**
+ * test render with null options
+ *
+ * @return void
+ */
+	public function testRenderNullOptions() {
+		$select = new SelectBox($this->templates);
+		$data = [
+			'id' => 'BirdName',
+			'name' => 'Birds[name]',
+			'options' => null
+		];
+		$result = $select->render($data, $this->context);
+		$expected = [
+			'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
+			'/select'
+		];
+		$this->assertHtml($expected, $result);
+
+		$data['empty'] = true;
+		$result = $select->render($data, $this->context);
+		$expected = [
+			'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
+			['option' => ['value' => '']], '/option',
+			'/select'
+		];
+		$this->assertHtml($expected, $result);
+
+		$data['empty'] = 'empty';
+		$result = $select->render($data, $this->context);
+		$expected = [
+			'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
+			['option' => ['value' => '']], 'empty', '/option',
+			'/select'
+		];
+		$this->assertHtml($expected, $result);
+	}
 }