Browse Source

Start adding tests for InputRegistry.

mark_story 12 years ago
parent
commit
d264a825ad
2 changed files with 121 additions and 3 deletions
  1. 5 3
      src/View/Input/InputRegistry.php
  2. 116 0
      tests/TestCase/View/Input/InputRegistryTest.php

+ 5 - 3
src/View/Input/InputRegistry.php

@@ -56,7 +56,9 @@ class InputRegistry {
  */
 	public function __construct(StringTemplate $templates, array $widgets = null) {
 		$this->_templates = $templates;
-		$this->add($widgets);
+		if (!empty($widgets)) {
+			$this->add($widgets);
+		}
 	}
 
 /**
@@ -114,12 +116,12 @@ class InputRegistry {
  *   implement InputInterface.
  */
 	protected function _resolveWidget($widget) {
-		if (!is_array($widget)) {
+		if (is_object($widget)) {
 			return $widget;
 		}
 		$class = array_shift($widget);
 		$className = App::classname($class, 'View/Input');
-		if ($className === false) {
+		if ($className === false || !class_exists($className)) {
 			throw new \RuntimeException(sprintf('Unable to locate widget class "%s"', $class));
 		}
 		$reflection = new ReflectionClass($className);

+ 116 - 0
tests/TestCase/View/Input/InputRegistryTest.php

@@ -0,0 +1,116 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         CakePHP(tm) v3.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\View\Input;
+
+use Cake\TestSuite\TestCase;
+use Cake\View\Input\InputRegistry;
+use Cake\View\StringTemplate;
+
+/**
+ * InputRegistry test case
+ */
+class InputRegistryTestCase extends TestCase {
+
+/**
+ * setup method
+ *
+ * @return void
+ */
+	public function setUp() {
+		parent::setUp();
+		$this->templates = new StringTemplate();
+	}
+
+/**
+ * Test adding new widgets.
+ *
+ * @return void
+ */
+	public function testAdd() {
+		$this->markTestIncomplete();
+	}
+
+/**
+ * Test getting registered widgets.
+ *
+ * @return void
+ */
+	public function testGet() {
+		$this->markTestIncomplete();
+	}
+
+/**
+ * Test getting fallback widgets.
+ *
+ * @return void
+ */
+	public function testGetFallback() {
+		$this->markTestIncomplete();
+	}
+
+/**
+ * Test getting errors
+ *
+ * @expectedException RuntimeException
+ * @expectedExceptionMessage Unknown widget "foo"
+ * @return void
+ */
+	public function testGetNoFallbackError() {
+		$inputs = new InputRegistry($this->templates);
+		$inputs->get('foo');
+	}
+
+/**
+ * Test getting resolve dependency
+ *
+ * @return void
+ */
+	public function testGetResolveDependency() {
+		$inputs = new InputRegistry($this->templates);
+		$inputs->add([
+			'label' => ['Cake\View\Input\Label'],
+			'multicheckbox' => ['Cake\View\Input\MultiCheckbox', 'label']
+		]);
+		$result = $inputs->get('multicheckbox');
+		$this->assertInstanceOf('CakeView\Input\MultiCheckbox', $result);
+	}
+
+/**
+ * Test getting resolve dependency missing class
+ *
+ * @expectedException RuntimeException
+ * @expectedExceptionMessage Unable to locate widget class "TestApp\View\Derp"
+ * @return void
+ */
+	public function testGetResolveDependencyMissingClass() {
+		$inputs = new InputRegistry($this->templates);
+		$inputs->add(['test' => ['TestApp\View\Derp']]);
+		$inputs->get('test');
+	}
+
+/**
+ * Test getting resolve dependency missing dependency
+ *
+ * @expectedException RuntimeException
+ * @expectedExceptionMessage Unknown widget "label"
+ * @return void
+ */
+	public function testGetResolveDependencyMissingDependency() {
+		$inputs = new InputRegistry($this->templates);
+		$inputs->add(['multicheckbox' => ['Cake\View\Input\MultiCheckbox', 'label']]);
+		$inputs->get('multicheckbox');
+	}
+
+}