From 2a12b22c6b53cffd3ea923ef77b82c70d9dd3ba5 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Tue, 17 Dec 2013 20:58:16 +0100 Subject: [PATCH] Adding unit tests for BelongsToMany::save() when combined with the replace strategy --- .../ORM/Association/BelongsToManyTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php index 89e8e57198b..539577c5426 100644 --- a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php @@ -1057,4 +1057,56 @@ public function testReplaceLinkSuccess() { $this->assertFalse($entity->dirty('tags')); } +/** + * Tests saving with replace strategy returning true + * + * @return void + */ + public function testSaveWithReplace() { + $assoc = $this->getMock( + '\Cake\ORM\Association\BelongsToMany', + ['replaceLinks'], + ['tags'] + ); + $entity = new Entity([ + 'id' =>1, + 'tags' => [ + new Entity(['name' => 'foo']) + ] + ]); + + $options = ['foo' => 'bar']; + $assoc->saveStrategy(BelongsToMany::SAVE_REPLACE); + $assoc->expects($this->once())->method('replaceLinks') + ->with($entity, $entity->tags, $options) + ->will($this->returnValue(true)); + $this->assertSame($entity, $assoc->save($entity, $options)); + } + +/** + * Tests saving with replace strategy returning true + * + * @return void + */ + public function testSaveWithReplaceReturnFalse() { + $assoc = $this->getMock( + '\Cake\ORM\Association\BelongsToMany', + ['replaceLinks'], + ['tags'] + ); + $entity = new Entity([ + 'id' =>1, + 'tags' => [ + new Entity(['name' => 'foo']) + ] + ]); + + $options = ['foo' => 'bar']; + $assoc->saveStrategy(BelongsToMany::SAVE_REPLACE); + $assoc->expects($this->once())->method('replaceLinks') + ->with($entity, $entity->tags, $options) + ->will($this->returnValue(false)); + $this->assertSame(false, $assoc->save($entity, $options)); + } + }