Skip to content
Permalink
Browse files

Start implementing ArrayContext and some tests.

  • Loading branch information...
markstory committed Jan 30, 2014
1 parent 0c0a491 commit 68c539637f2dd0cda3ceaaccff10259620732156
Showing with 121 additions and 1 deletion.
  1. +31 −1 src/View/Form/ArrayContext.php
  2. +90 −0 tests/TestCase/View/Form/ArrayContextTest.php
@@ -9,7 +9,7 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @since CakePHP(tm) v 3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Form;
@@ -35,11 +35,33 @@
*/
class ArrayContext {
/**
* The request object.
*
* @var Cake\Network\Request
*/
protected $_request;
/**
* Context data for this object.
*
* @var array
*/
protected $_context;
/**
* Constructor.
*
* @param Cake\Network\Request
* @param array
*/
public function __construct(Request $request, array $context) {
$this->_request = $request;
$context += [
'schema' => [],
'required' => [],
'defaults' => [],
];
$this->_context = $context;
}
@@ -54,6 +76,14 @@ public function __construct(Request $request, array $context) {
* @return mixed
*/
public function val($field) {
$val = $this->_request->data($field);
if ($val !== null) {
return $val;
}
if (empty($this->_context['defaults']) || !is_array($this->_context['defaults'])) {
return null;
}
return Hash::get($this->_context['defaults'], $field);
}
/**
@@ -0,0 +1,90 @@
<?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) v 3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\View\Form;
use Cake\Network\Request;
use Cake\TestSuite\TestCase;
use Cake\View\Form\ArrayContext;
/**
* Array context test case.
*/
class ArrayContextTest extends TestCase {
/**
* setup method.
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->request = new Request();
}
/**
* Test reading values from the request & defaults.
*/
public function testValPresent() {
$this->request->data = [
'Articles' => [
'title' => 'New title',
'body' => 'My copy',
]
];
$context = new ArrayContext($this->request, [
'defaults' => [
'Articles' => [
'title' => 'Default value',
'published' => 0
]
]
]);
$this->assertEquals('New title', $context->val('Articles.title'));
$this->assertEquals('My copy', $context->val('Articles.body'));
$this->assertEquals(0, $context->val('Articles.published'));
$this->assertNull($context->val('Articles.nope'));
}
/**
* Test getting values when the request and defaults are missing.
*
* @return void
*/
public function testValMissing() {
$context = new ArrayContext($this->request, []);
$this->assertNull($context->val('Comments.field'));
}
public function testIsRequired() {
$this->markTestIncomplete();
}
public function testIsRequiredUndefined() {
$this->markTestIncomplete();
}
public function testIsType() {
$this->markTestIncomplete();
}
public function testIsTypeUndefined() {
$this->markTestIncomplete();
}
public function testAttributes() {
$this->markTestIncomplete();
}
}

0 comments on commit 68c5396

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