Skip to content
Permalink
Browse files

Fix saveOrFail() to include useful messages on output, e.g. CLI/tests.

  • Loading branch information...
dereuromark committed Jan 23, 2018
1 parent 5b92235 commit c6be0ab87b429afbb9ad4b5ccb4f5a2cb40e64bc
Showing with 51 additions and 0 deletions.
  1. +29 −0 src/ORM/Exception/PersistenceFailedException.php
  2. +22 −0 tests/TestCase/ORM/TableTest.php
@@ -45,9 +45,37 @@ class PersistenceFailedException extends Exception
public function __construct(EntityInterface $entity, $message, $code = null, $previous = null)
{
$this->_entity = $entity;
if (is_array($message)) {
$errors = [];
foreach ($entity->getErrors() as $field => $error) {
$errors[] = $field . ': "' . $this->buildError($error) . '"';
}
if ($errors) {
$message[] = implode(', ', $errors);
$this->_messageTemplate = 'Entity %s failure (%s).';
}
}
parent::__construct($message, $code, $previous);
}
/**
* @param string|array $error Error message.
* @return string
*/
protected function buildError($error)
{
if (!is_array($error)) {
return $error;
}
$errors = [];
foreach ($error as $key => $message) {
$errors[] = is_int($key) ? $message : $key;
}
return implode(', ', $errors);
}
/**
* Get the passed in entity
*
@@ -57,4 +85,5 @@ public function getEntity()
{
return $this->_entity;
}
}
@@ -6518,6 +6518,28 @@ public function testSaveOrFail()
$this->assertSame([], $row->toArray());
}
/**
* Tests that saveOrFail displays useful messages on output, especially in tests for CLI.
*
* @return void
*/
public function testSaveOrFailErrorDisplay()
{
$this->expectException(\Cake\ORM\Exception\PersistenceFailedException::class);
$this->expectExceptionMessage('Entity save failure (field: "Some message", multiple: "one, two")');
$entity = new Entity([
'foo' => 'bar'
]);
$entity->setError('field', 'Some message');
$entity->setError('multiple', ['one' => 'One', 'two' => 'Two']);
$table = TableRegistry::get('users');
$table->saveOrFail($entity);
$row = $table->find('all')->where(['foo' => 'bar'])->toArray();
$this->assertSame([], $row->toArray());
}
/**
* Tests that saveOrFail returns the right entity
*

0 comments on commit c6be0ab

Please sign in to comment.
You can’t perform that action at this time.