Skip to content

Commit

Permalink
Build remaining methods on ArrayContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 30, 2014
1 parent 68c5396 commit 669cd69
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/View/Form/ArrayContext.php
Expand Up @@ -95,6 +95,11 @@ public function val($field) {
* @return boolean
*/
public function isRequired($field) {
if (!is_array($this->_context['required'])) {
return false;
}
$required = Hash::get($this->_context['required'], $field);
return (bool)$required;
}

/**
Expand All @@ -105,7 +110,11 @@ public function isRequired($field) {
* @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;
}

/**
Expand All @@ -115,7 +124,12 @@ public function type($field) {
* @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);
}

}
72 changes: 66 additions & 6 deletions tests/TestCase/View/Form/ArrayContextTest.php
Expand Up @@ -67,24 +67,84 @@ public function testValMissing() {
$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'));
}

}

0 comments on commit 669cd69

Please sign in to comment.