Browse Source

Build remaining methods on ArrayContext.

mark_story 12 years ago
parent
commit
669cd696c1
2 changed files with 82 additions and 8 deletions
  1. 16 2
      src/View/Form/ArrayContext.php
  2. 66 6
      tests/TestCase/View/Form/ArrayContextTest.php

+ 16 - 2
src/View/Form/ArrayContext.php

@@ -95,6 +95,11 @@ class ArrayContext {
  * @return boolean
  */
 	public function isRequired($field) {
+		if (!is_array($this->_context['required'])) {
+			return false;
+		}
+		$required = Hash::get($this->_context['required'], $field);
+		return (bool)$required;
 	}
 
 /**
@@ -105,7 +110,11 @@ class ArrayContext {
  * @see Cake\Database\Type
  */
 	public function type($field) {
-
+		if (!is_array($this->_context['schema'])) {
+			return false;
+		}
+		$schema = Hash::get($this->_context['schema'], $field);
+		return isset($schema['type']) ? $schema['type'] : null;
 	}
 
 /**
@@ -115,7 +124,12 @@ class ArrayContext {
  * @return array An array of data describing the additional attributes on a field.
  */
 	public function attributes($field) {
-
+		if (!is_array($this->_context['schema'])) {
+			return [];
+		}
+		$schema = Hash::get($this->_context['schema'], $field);
+		$whitelist = ['length' => null, 'precision' => null];
+		return array_intersect_key($schema, $whitelist);
 	}
 
 }

+ 66 - 6
tests/TestCase/View/Form/ArrayContextTest.php

@@ -67,24 +67,84 @@ class ArrayContextTest extends TestCase {
 		$this->assertNull($context->val('Comments.field'));
 	}
 
+/**
+ * Test isRequired
+ *
+ * @return void
+ */
 	public function testIsRequired() {
-		$this->markTestIncomplete();
+		$context = new ArrayContext($this->request, [
+			'required' => [
+				'Comments' => [
+					'required' => true,
+					'nope' => false
+				]
+			]
+		]);
+		$this->assertTrue($context->isRequired('Comments.required'));
+		$this->assertFalse($context->isRequired('Comments.nope'));
+		$this->assertFalse($context->isRequired('Articles.id'));
 	}
 
+/**
+ * Test isRequired when the required key is omitted
+ *
+ * @return void
+ */
 	public function testIsRequiredUndefined() {
-		$this->markTestIncomplete();
+		$context = new ArrayContext($this->request, []);
+		$this->assertFalse($context->isRequired('Comments.field'));
 	}
 
-	public function testIsType() {
-		$this->markTestIncomplete();
+/**
+ * Test the type method.
+ *
+ * @return void
+ */
+	public function testType() {
+		$context = new ArrayContext($this->request, [
+			'schema' => [
+				'Comments' => [
+					'id' => ['type' => 'integer'],
+					'comment' => ['length' => 255]
+				]
+			]
+		]);
+		$this->assertNull($context->type('Comments.undefined'));
+		$this->assertEquals('integer', $context->type('Comments.id'));
+		$this->assertNull($context->type('Comments.comment'));
 	}
 
+/**
+ * Test the type method when the data is missing.
+ *
+ * @return void
+ */
 	public function testIsTypeUndefined() {
-		$this->markTestIncomplete();
+		$context = new ArrayContext($this->request, []);
+		$this->assertNull($context->type('Comments.undefined'));
 	}
 
+/**
+ * Test fetching attributes.
+ *
+ * @return void
+ */
 	public function testAttributes() {
-		$this->markTestIncomplete();
+		$context = new ArrayContext($this->request, [
+			'schema' => [
+				'Comments' => [
+					'id' => ['type' => 'integer'],
+					'comment' => ['type' => 'string', 'length' => 255],
+					'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
+					'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
+				]
+			]
+		]);
+		$this->assertEquals([], $context->attributes('Comments.id'));
+		$this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
+		$this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
+		$this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
 	}
 
 }