Skip to content

Commit

Permalink
Expand tests and remove unreachable code.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 7, 2014
1 parent bbbd505 commit 4a6f598
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/View/Form/EntityContext.php
Expand Up @@ -150,9 +150,6 @@ public function val($field) {
}
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return null;
}
return $entity->get(array_pop($parts));
}

Expand Down Expand Up @@ -187,7 +184,10 @@ protected function _getEntity($path) {
}
$entity = $next;
}
return [false, false];
throw \RuntimeException(sprintf(
'Unable to fetch property "%s"',
implode(".", $path)
));
}

/**
Expand Down Expand Up @@ -220,15 +220,8 @@ public function isRequired($field) {
}
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return false;
}

$validator = $this->_getValidator($prop);
if (!$validator) {
return false;
}

$field = array_pop($parts);
if (!$validator->hasField($field)) {
return false;
Expand All @@ -242,7 +235,7 @@ public function isRequired($field) {
* conventions.
*
* @param string $entity The entity name to get a validator for.
* @return Validator|false
* @return Validator
*/
protected function _getValidator($entity) {
$table = $this->_getTable($entity);
Expand Down Expand Up @@ -291,9 +284,6 @@ protected function _getTable($prop) {
public function type($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return null;
}
$table = $this->_getTable($prop);
$column = array_pop($parts);
return $table->schema()->columnType($column);
Expand All @@ -308,9 +298,6 @@ public function type($field) {
public function attributes($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return [];
}
$table = $this->_getTable($prop);
$column = $table->schema()->column(array_pop($parts));
$whitelist = ['length' => null, 'precision' => null];
Expand All @@ -336,9 +323,6 @@ public function hasError($field) {
public function error($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return [];
}
return $entity->errors(array_pop($parts));
}

Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -53,6 +53,19 @@ public function setUp() {
$this->request = new Request();
}

/**
* Test an invalid table scope throws an error.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to find table class for current entity
*/
public function testInvalidTable() {
$row = new \StdClass();
$context = new EntityContext($this->request, [
'entity' => $row,
]);
}

/**
* Test operations that lack a table argument.
*
Expand Down Expand Up @@ -295,6 +308,7 @@ public function testIsRequiredStringValidator() {

$this->assertFalse($context->isRequired('Herp.derp.derp'));
$this->assertFalse($context->isRequired('nope'));
$this->assertFalse($context->isRequired(''));
}

/**
Expand Down Expand Up @@ -326,6 +340,8 @@ public function testIsRequiredAssociatedHasMany() {

$this->assertTrue($context->isRequired('comments.0.user_id'));
$this->assertFalse($context->isRequired('comments.0.other'));
$this->assertFalse($context->isRequired('user.0.other'));
$this->assertFalse($context->isRequired(''));
}

/**
Expand Down

0 comments on commit 4a6f598

Please sign in to comment.