Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add valid() failure handling with test

  • Loading branch information...
commit faef64dcfd855881449a752a3b3ce45cadcc5f84 1 parent c858256
@zombor authored
Showing with 70 additions and 7 deletions.
  1. +7 −1 classes/automodeler/model.php
  2. +63 −6 tests/automodeler/model.php
View
8 classes/automodeler/model.php
@@ -135,6 +135,12 @@ public function valid(Validation $validation = NULL, Validation $default_validat
{
return TRUE;
}
-
+ else
+ {
+ return array(
+ 'status' => FALSE,
+ 'errors' => $data->errors($this->_lang)
+ );
+ }
}
}
View
69 tests/automodeler/model.php
@@ -155,9 +155,11 @@ public function test_set_model_properties()
}
/**
- * Tests that we can successfuly validate a model
+ * Gets a standard model with validation for validation tests
+ *
+ * @return AutoModeler_Model
*/
- public function test_successfuly_validate_model()
+ protected function get_model_with_validation()
{
$model = new AutoModeler_Model(
array(
@@ -181,9 +183,19 @@ public function test_successfuly_validate_model()
$model->foo = 'bar';
$model->bar = 1;
+ return $model;
+ }
+
+ /**
+ * Gets a standard validation object
+ *
+ * @return Validation
+ */
+ protected function get_default_validation_object()
+ {
$validation = $this->getMock(
'Validation',
- array('copy', 'as_array', 'rules', 'check', 'bind')
+ array('copy', 'as_array', 'rules', 'check', 'bind', 'errors')
);
$validation->expects($this->any())
->method('as_array')
@@ -191,16 +203,61 @@ public function test_successfuly_validate_model()
$this->returnValue(array())
);
$validation->expects($this->any())
- ->method('check')
- ->will($this->returnValue(TRUE));
- $validation->expects($this->any())
->method('copy')
->will(
$this->returnValue($validation)
);
+ return $validation;
+ }
+
+ /**
+ * Tests that we can successfuly validate a model
+ */
+ public function test_successfuly_validate_model()
+ {
+ $model = $this->get_model_with_validation();
+
+ $validation = $this->get_default_validation_object();
+ $validation->expects($this->any())
+ ->method('check')
+ ->will($this->returnValue(TRUE));
+
$status = $model->valid($validation);
$this->assertSame($status, TRUE);
}
+
+ /**
+ * Tests that we can successfuly validation an invalid model
+ */
+ public function test_error_validate_model()
+ {
+ $model = $this->get_model_with_validation();
+ $model->bar = 'test';
+
+ $validation = $this->get_default_validation_object();
+
+ $validation->expects($this->any())
+ ->method('check')
+ ->will($this->returnValue(FALSE));
+ $validation->expects($this->any())
+ ->method('errors')
+ ->will(
+ $this->returnValue(
+ array(
+ 'foo' => 'foo must not be empty',
+ 'bar' => 'bar must be numeric',
+ )
+ )
+ );
+
+ $status = $model->valid($validation);
+
+ $this->assertTrue(is_array($status), $status);
+ $this->assertTrue(array_key_exists('status', $status));
+ $this->assertFalse($status['status']);
+ $this->assertTrue(array_key_exists('errors', $status));
+ $this->assertTrue(is_array($status['errors']));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.