From b15f0d8ed37422a8dd93b1c336ecb19b0d8212ae Mon Sep 17 00:00:00 2001 From: Justin Yost Date: Thu, 23 Jun 2016 09:25:30 -0700 Subject: [PATCH] Improving Code Coverage for the Behavior Signed-off-by: Justin Yost --- .../Behavior/CreatorModifierBehavior.php | 1 + .../Behavior/CreatorModifierBehaviorTest.php | 151 ++++++++++++++++-- 2 files changed, 139 insertions(+), 13 deletions(-) diff --git a/src/Model/Behavior/CreatorModifierBehavior.php b/src/Model/Behavior/CreatorModifierBehavior.php index ab53ddf..6f2b14c 100644 --- a/src/Model/Behavior/CreatorModifierBehavior.php +++ b/src/Model/Behavior/CreatorModifierBehavior.php @@ -163,6 +163,7 @@ protected function updateField(Entity $entity, $field) { * Factory method for the Request object. * * @return \Cake\Network\Request New instance of the Request object. + * @codeCoverageIgnore Don't test PHP's ability to use new. */ protected function newRequest() { return new Request(); diff --git a/tests/TestCase/Model/Behavior/CreatorModifierBehaviorTest.php b/tests/TestCase/Model/Behavior/CreatorModifierBehaviorTest.php index 258cfe6..1b57efd 100644 --- a/tests/TestCase/Model/Behavior/CreatorModifierBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/CreatorModifierBehaviorTest.php @@ -1,4 +1,7 @@ getMock('Cake\ORM\Table'); + $config = [ + 'events' => [ + 'Model.beforeSave' => [], + ], + ]; + + $this->Behavior = $this->getMock( + '\CreatorModifier\Model\Behavior\CreatorModifierBehavior', + ['sessionUserId'], + [$table, $config] + ); + $this->Behavior->expects($this->any()) + ->method('sessionUserId') + ->will($this->returnValue($this->mockedUserUUID)); + + $entity = new Entity(['username' => 'timestamp test']); + $return = $this->Behavior->createdOrModifed($entity); + $this->assertFalse($return, 'createdOrModifed is expected to do nothing and return false'); + $this->assertNull($entity->modifier_id, 'modifier_id field is NOT expected to change'); + $this->assertNull($entity->creator_id, 'creator_id field is NOT expected to change'); + } + + /** + * Test the CreatedModifiedBehavior in the event that nothing is expected to + * happen. * * @return void */ @@ -280,7 +331,7 @@ public function testCreatedOrModifiedNoop() { } /** - * testTouchCustomEvent + * Test handling a custom fired event. * * @return void */ @@ -314,4 +365,78 @@ public function testCreatedOrModifiedCustomEvent() { 'Creator_id field is NOT expected to change' ); } + + /** + * Test the ::updateField method when the field is marked as dirty. + * + * @return void + */ + public function testUpdateFieldIsDirty() { + $existingValue = '54108b70-a178-4590-9df4-1a900a00020f'; + $field = 'modifier_id'; + $entity = $this->getMock( + '\Cake\ORM\Entity', + ['dirty'], + [['name' => 'Foo', $field => $existingValue]] + ); + $entity->expects($this->once()) + ->method('dirty') + ->with($field) + ->will($this->returnValue(true)); + + $table = $this->getMock('Cake\ORM\Table'); + $behavior = $this->getMock( + '\CreatorModifier\Test\TestCase\Model\Behavior\TestCreatorModifierBehavior', + ['getUserId'], + [$table] + ); + $behavior->expects($this->never()) + ->method('getUserId') + ->will($this->returnValue($this->mockedUserUUID)); + + $return = $behavior->updateField($entity, 'modifier_id'); + $this->assertNull( + $return, + 'When attempting to update a field marked already dirty, ::updateField returns null and does nothing.' + ); + } + + /** + * Test the ::updateField method when the field is marked as dirty. + * + * @return void + */ + public function testUpdateFieldIsClean() { + $existingValue = '54108b70-a178-4590-9df4-1a900a00020f'; + $field = 'modifier_id'; + $entity = $this->getMock( + '\Cake\ORM\Entity', + ['dirty', 'set'], + [['name' => 'Foo', $field => $existingValue]] + ); + $entity->expects($this->once()) + ->method('dirty') + ->with($field) + ->will($this->returnValue(false)); + $entity->expects($this->once()) + ->method('set') + ->with($field, $this->mockedUserUUID) + ->will($this->returnValue(true)); + + $table = $this->getMock('Cake\ORM\Table'); + $behavior = $this->getMock( + '\CreatorModifier\Test\TestCase\Model\Behavior\TestCreatorModifierBehavior', + ['getUserId'], + [$table] + ); + $behavior->expects($this->once()) + ->method('getUserId') + ->will($this->returnValue($this->mockedUserUUID)); + + $return = $behavior->updateField($entity, 'modifier_id'); + $this->assertNull( + $return, + 'When attempting to update a field marked clean, ::updateField returns null and sets the return from ::getUserId to the field for the Entity.' + ); + } }