Skip to content

Commit

Permalink
Start adding tests for InputRegistry.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 18, 2014
1 parent 944faad commit d264a82
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/View/Input/InputRegistry.php
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -114,12 +116,12 @@ public function get($name) {
* 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);
Expand Down
116 changes: 116 additions & 0 deletions 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');
}

}

0 comments on commit d264a82

Please sign in to comment.