Skip to content

Commit

Permalink
Start implementing checkbox widget.
Browse files Browse the repository at this point in the history
Basic features and attributes are working, next up is making
checked property smarter.
  • Loading branch information
markstory committed Jan 12, 2014
1 parent 39075a7 commit 32b75e8
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/View/Input/Checkbox.php
@@ -0,0 +1,64 @@
<?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\View\Input;

use Cake\View\StringTemplate;

/**
* Input widget for creating checkbox widgets.
*/
class Checkbox {

/**
* Template instance.
*
* @var Cake\View\StringTemplate
*/
protected $_templates;

/**
* Constructor
*
* @param Cake\View\StringTemplate $templates
*/
public function __construct($templates) {
$this->_templates = $templates;
}

/**
* Render a checkbox element.
*
* @param array $data The data to create a checkbox with.
*/
public function render($data) {
$data += [
'name' => '',
'value' => 1,
'checked' => false,
'disabled' => false,
];
$attrs = $this->_templates->formatAttributes(
$data,
['name', 'value']
);

return $this->_templates->format('checkbox', [
'name' => $data['name'],
'value' => $data['value'],
'attrs' => $attrs
]);
}

}
117 changes: 117 additions & 0 deletions tests/TestCase/View/Input/CheckboxTest.php
@@ -0,0 +1,117 @@
<?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\Checkbox;
use Cake\View\StringTemplate;

/**
* Checkbox test case
*/
class CheckboxTest extends TestCase {

public function setUp() {
parent::setUp();
$templates = [
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
];
$this->templates = new StringTemplate();
$this->templates->add($templates);
}

/**
* Test rendering simple checkboxes.
*
* @return void
*/
public function testRenderSimple() {
$checkbox = new Checkbox($this->templates);
$data = [
'name' => 'Comment[spam]',
];
$result = $checkbox->render($data);
$expected = [
'input' => [
'type' => 'checkbox',
'name' => 'Comment[spam]',
'value' => 1,
]
];
$this->assertTags($result, $expected);

$data = [
'name' => 'Comment[spam]',
'value' => 99
];
$result = $checkbox->render($data);
$expected = [
'input' => [
'type' => 'checkbox',
'name' => 'Comment[spam]',
'value' => 99,
]
];
$this->assertTags($result, $expected);
}

/**
* Test rendering disabled checkboxes.
*
* @return void
*/
public function testRenderDisabled() {
$checkbox = new Checkbox($this->templates);
$data = [
'name' => 'Comment[spam]',
'disabled' => true,
];
$result = $checkbox->render($data);
$expected = [
'input' => [
'type' => 'checkbox',
'name' => 'Comment[spam]',
'value' => 1,
'disabled' => 'disabled',
]
];
$this->assertTags($result, $expected);
}

/**
* Test rendering checked checkboxes.
*
* @return void
*/
public function testRenderChecked() {
$checkbox = new Checkbox($this->templates);
$data = [
'name' => 'Comment[spam]',
'value' => 1,
'checked' => 1,
];
$result = $checkbox->render($data);
$expected = [
'input' => [
'type' => 'checkbox',
'name' => 'Comment[spam]',
'value' => 1,
'checked' => 'checked',
]
];
$this->assertTags($result, $expected);
}

}

0 comments on commit 32b75e8

Please sign in to comment.