Browse Source

Start implementing ArrayContext and some tests.

mark_story 12 years ago
parent
commit
68c539637f
2 changed files with 121 additions and 1 deletions
  1. 31 1
      src/View/Form/ArrayContext.php
  2. 90 0
      tests/TestCase/View/Form/ArrayContextTest.php

+ 31 - 1
src/View/Form/ArrayContext.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 @@ use Cake\Utility\Hash;
  */
 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 @@ class ArrayContext {
  * @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);
 	}
 
 /**

+ 90 - 0
tests/TestCase/View/Form/ArrayContextTest.php

@@ -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();
+	}
+
+}