New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entity::errors() will append errors and will merge the new given errors #840
Entity::errors() will append errors and will merge the new given errors #840
Conversation
} | ||
if ($value === null && isset($this->_errors[$field])) { | ||
return $this->_errors[$field]; | ||
} | ||
if ($value !== null) { | ||
if (array_key_exists($field, $this->_errors)) { | ||
$current = is_array($this->_errors[$field]) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is breaking over multiple lines, it should probably just be an if
block.
Hey, looks like one of the new tests is broken. |
Yeah... My bad.. Didn't run all tests so, there were places where it was expected not to append errors but to get the last error. Thanks Travis o/ |
Attaboy Travis! |
Is there a way for clearing all errors ? |
Hmm, I thought it was possible to pass On Feb 27, 2013, at 20:23, Simon JAILLET notifications@github.com wrote:
|
There is no reset functionality. I thought about adding what @nateabele said because of the explicit null check (maybe it was on a lost todo list or something) but i confess i never saw it implemented and a quick history search proved. But if you want it, i'm your man :-D |
Squashed the reset trigger. |
if (!is_array($value)) { | ||
$value = array($value); | ||
} | ||
return ($this->_errors[$field] = Set::merge($current, $value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ẁith return ($this->_errors[$field] = Set::merge((array) $current, (array) $value));
you don't need the if (!is_array(..))
above imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is Set::merge
is realy needed here ? array_merge
is not enough ?
Imo it need at least a |
Thanks for the input @jails o/ those a couple of lame examples of lazy development... |
Woos me with beautiful code @rapzo and I'll tell you if I'm attracted ;-) |
Squashed. |
When you write unit tests @rapzo, you need to write tests only for one function logic at a time. And when possible split each tests to independant functions. If I understand well this should be the tests I would do for public function testErrors() {
$entity = new Entity();
$errors = array('foo' => 'Something bad happened.');
$this->assertEqual(array(), $entity->errors());
$entity->errors($errors);
$this->assertEqual($errors, $entity->errors());
$this->assertEqual('Something bad happened.', $entity->errors('foo'));
$errors += array('bar' => 'Something really bad happened.');
$entity->errors($errors);
$this->assertEqual($errors, $entity->errors());
$this->assertCount(2, $entity->errors());
$this->assertEqual('Something bad happened.', $entity->errors('foo'));
$this->assertEqual('Something really bad happened.', $entity->errors('bar'));
$entity->errors(false);
$this->assertEmpty($entity->errors());
}
public function testAppendingErrors() {
$entity = new Entity();
$expected = array(
'Something bad happened.',
'Something really bad happened.'
);
$entity->errors('foo', $expected[0]);
$entity->errors('foo', $expected[1]);
$this->assertCount(1, $entity->errors());
$this->assertEqual($expected, $entity->errors('foo'));
}
public function testAppendingErrorsWithArraySyntax() {
$entity = new Entity();
$expected = array(
'Something bad happened.',
'Something really bad happened.'
);
$entity->errors(array('foo' => $expected[0]));
$entity->errors(array('foo' => $expected[1]));
$this->assertCount(1, $entity->errors());
$this->assertEqual($expected, $entity->errors('foo'));
}
public function testAppendingErrorsWithMixedSyntax() {
$entity = new Entity();
$expected = array(
'Something bad happened.',
'Something really bad happened.'
);
$entity->errors('foo', $expected[0]);
$entity->errors(array('foo' => $expected[1]));
$this->assertCount(1, $entity->errors());
$this->assertEqual($expected, $entity->errors('foo'));
} And to check errors are correctly cleared beetween two validates, this is the test I would do: public function testErrorsIsClearedOnEachValidates() {
$post = MockPostForValidates::create(array('title' => 'new post'));
$result = $post->validates();
$this->assertFalse($result);
$result = $post->errors();
$this->assertNotEmpty($result);
$post->email = 'contact@lithify.me';
$result = $post->validates();
$this->assertTrue($result);
$result = $post->errors();
$this->assertEmpty($result);
} A lot of above tests will fail imo. But maybe I don't understand the behavior you're expecting for |
WOW! Now this is something!to dwell about. |
The validates test should be in the ModelTest. Entity should only test that it can set,get,reset errors. On Mar 1, 2013, at 7:23 PM, Simon JAILLET notifications@github.com wrote:
|
Hey @rapzo, sorry for all the back and forth. :-P Just ping me when you've incorporated those last two comments. |
Sorry for the long delay fellaz, i've been flooded and to be honest didn't have one opportunity to pick this up in the last 15 days. P.S.: rebased to current dev but now that i think of it maybe it wasn't such a good idea... If it screws history i'll close this one and will make a new PR |
$this->assertTrue($post->validates()); | ||
|
||
$expected = array('title' => $errors); | ||
$this->assertEqual($expected, $post->errors()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once validates()
pass, errors()
should returns an empty array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Erm, that's the thing. Should Entity::errors() be that flat for Model::validates()? Because if we take a look at the Model::validates()
if ($errors = $validator::check($entity->data(), $rules, $options)) {
$entity->errors($errors);
}
return empty($errors);
Reseting the array would defeat the only thing, don't you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure to understand what you mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Model::validates()
only check for errors from checking the rules defined in Model::$validates
it should erase any errors added previously to validation? IMO it should only check if its valid for the current defined rules.
For example, I find this useful specially when validating nested documents.
Imo you can safely replace added tests in Of course to make it pass you should add |
|
||
$errors = array('cheesy title'); | ||
$post->errors('title', $errors[0]); | ||
$this->assertEqual(array('title' => 'cheesy title'), $post->errors()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to tests appended errors here since it has been tested in EntityTest
.
with the previous ones already stored in the Entity's $_errors variable. Also, added a reset functionality to clear all errors. Entity errors are cleared before the Model::validation() filter function to prevent validation (and consequent write data) with a not empty errors array. Removed &$self from the filter since it was not used. ixed tests. Replacing tests with a more simple, direct approach (from Jails).
This one is fine for me ;-) ! |
Is everything ok here? Do you guys need me to do anything else? |
Entity::errors() will append errors and will merge the new given errors
Merged! Thanks for the prod. :-) |
with the previous ones already stored in the Entity's $_errors variable.