Skip to content

Commit

Permalink
Adding another test for domain checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 23, 2014
1 parent 9bcb88c commit 706281d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/TestCase/ORM/DomainRulesIntegrationTest.php
Expand Up @@ -151,4 +151,49 @@ public function testSaveHasManyWithErrorsAtomic() {
$this->assertNotEmpty($entity->articles[1]->errors());
}

/**
* Tests that it is possible to continue saving hasMany associations
* even if any of the records fail validation when atomic is set
* to false
*
* @return void
*/
public function testSaveHasManyWithErrorsNonAtomic() {
$entity = new \Cake\ORM\Entity([
'name' => 'Jose'
]);
$entity->articles = [
new \Cake\ORM\Entity([
'title' => 'A title',
'body' => 'A body'
]),
new \Cake\ORM\Entity([
'title' => '1',
'body' => 'Another body'
])
];

$table = TableRegistry::get('authors');
$table->hasMany('articles');
$table->association('articles')
->target()
->domainRules()
->add(function (Entity $article) {
if (!is_numeric($article->title)) {
$article->errors('title', ['an error']);
return false;
}
return true;
});

$result = $table->save($entity, ['atomic' => false]);
$this->assertSame($entity, $result);
$this->assertFalse($entity->isNew());
$this->assertTrue($entity->articles[0]->isNew());
$this->assertFalse($entity->articles[1]->isNew());
$this->assertEquals(4, $entity->articles[1]->id);
$this->assertNull($entity->articles[0]->id);
$this->assertNotEmpty($entity->articles[0]->errors('title'));
}

}

0 comments on commit 706281d

Please sign in to comment.