Skip to content

Commit

Permalink
Improving Code Coverage for the Behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Yost <[email protected]>
  • Loading branch information
justinyost committed Jun 23, 2016
1 parent 3896d85 commit b15f0d8
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Model/Behavior/CreatorModifierBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
151 changes: 138 additions & 13 deletions tests/TestCase/Model/Behavior/CreatorModifierBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
/**
* Tests for the CreatorModifierBehavior Class.
*/
namespace CreatorModifier\Test\TestCase\Model\Behavior;

use Cake\Event\Event;
Expand All @@ -8,10 +11,27 @@
use CreatorModifier\Model\Behavior\CreatorModifierBehavior;

/**
* CreatorModifierBehaviorTest Class
* \CreatorModifier\Test\TestCase\Model\Behavior\TestCreatorModifierBehavior
*
* Expose the protected methods for the CreatorModifierBehavior class for unit
* testing.
*/
class CreatorModifierBehaviorTest extends TestCase {
class TestCreatorModifierBehavior extends CreatorModifierBehavior {
public function updateField(Entity $entity, $field) {
return parent::updateField($entity, $field);
}

public function sessionUserId() {
return parent::sessionUserId();
}
}

/**
* \CreatorModifier\Test\TestCase\Model\Behavior\CreatorModifierBehaviorTest
*
* Tests for the CreatorModifierBehavior class.
*/
class CreatorModifierBehaviorTest extends TestCase {
/**
* Mocked User UUID Value
*
Expand All @@ -24,9 +44,7 @@ class CreatorModifierBehaviorTest extends TestCase {
*
* @var array
*/
public $fixtures = [
//'CreatorModifier.users'
];
public $fixtures = [];

/**
* Setup the Tests
Expand Down Expand Up @@ -136,7 +154,8 @@ public function testCreatorIdNotNew() {
}

/**
* Test when the modifier id is absent
* Test what happens when everything works and a modifier_id value for the
* Entity is not yet set.
*
* @return void
* @triggers Model.beforeSave
Expand All @@ -156,7 +175,8 @@ public function testModifierIdAbsent() {
}

/**
* testModifiedPresent
* Test what happens when everything works and a modifier_id value for the
* Entity is already set.
*
* @return void
* @triggers Model.beforeSave
Expand All @@ -180,7 +200,7 @@ public function testModifierIdPresent() {
}

/**
* testInvalidEventConfig
* Test what happens when we setup invalid configuration for an Event.
*
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid
Expand All @@ -198,7 +218,7 @@ public function testInvalidEventConfig() {
}

/**
* testGetTimestamp
* Test the method ::getUserId, should return our mocked return.
*
* @return void
*/
Expand All @@ -213,7 +233,7 @@ public function testGetUserId() {
}

/**
* test that get user id persists
* Test that getUserId persists and returns the same value for multiple calls.
*
* @depends testGetTimestamp
* @return void
Expand All @@ -230,7 +250,7 @@ public function testGetUserIdPersists($behavior) {
}

/**
* testCreatedOrModified
* Test the Behavior in the event everything works normally.
*
* @return void
*/
Expand All @@ -249,7 +269,38 @@ public function testCreatedOrModified() {
}

/**
* testTouchNoop
* Test the Behavior when an event is fired that doesn't have any data set
* for it from the configs.
*
* @return void
*/
public function testCreatedOrModifiedEmptyEvent() {
$table = $this->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
*/
Expand Down Expand Up @@ -280,7 +331,7 @@ public function testCreatedOrModifiedNoop() {
}

/**
* testTouchCustomEvent
* Test handling a custom fired event.
*
* @return void
*/
Expand Down Expand Up @@ -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.'
);
}
}

0 comments on commit b15f0d8

Please sign in to comment.