Skip to content

Commit

Permalink
Added new test to DataMapper in order to make sure that an Event hand…
Browse files Browse the repository at this point in the history
…ler response can bail out database operations (save, update, insert and delete)
  • Loading branch information
Zizaco committed Apr 9, 2016
1 parent a085dff commit aa41bc6
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/Mongolid/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ public function testShouldDelete()
$this->assertTrue($mapper->delete($object));
}

/**
* @dataProvider eventsToBailOperations
*/
public function testDatabaseOperationsShouldBailOutIfTheEventHandlerReturnsFalse($operation, $dbOperation, $eventName)
{
// Arrange
$connPool = m::mock('Mongolid\Connection\Pool');
$mapper = m::mock('Mongolid\DataMapper\DataMapper[parseToDocument,getCollection]', [$connPool]);
$collection = m::mock('MongoDB\Collection');
$object = m::mock();

// Act
$mapper->shouldAllowMockingProtectedMethods();

$mapper->shouldReceive('parseToDocument')
->with($object)
->never();

$mapper->shouldReceive('getCollection')
->andReturn($collection);

$collection->shouldReceive($dbOperation)
->never();

/* "Mocks" the fireEvent to return false and bail the operation */
$this->expectEvent($eventName, $object, true, false);

// Assert
$this->assertFalse($mapper->$operation($object));
}

public function testShouldGetWithWhereQuery()
{
// Arrange
Expand Down Expand Up @@ -495,6 +526,35 @@ protected function expectEvent($event, $entity, bool $halt, $return = true)
->andReturn($return);
}

public function eventsToBailOperations()
{
return [
'Saving event' => [
'operation' => 'save',
'dbOperation' => 'updateOne',
'eventName' => 'saving'
],
// ------------------------
'Inserting event' => [
'operation' => 'insert',
'dbOperation' => 'insertOne',
'eventName' => 'inserting'
],
// ------------------------
'Updating event' => [
'operation' => 'update',
'dbOperation' => 'updateOne',
'eventName' => 'updating'
],
// ------------------------
'Deleting event' => [
'operation' => 'delete',
'dbOperation' => 'deleteOne',
'eventName' => 'deleting'
],
];
}

public function queryValueScenarios()
{
return [
Expand Down

0 comments on commit aa41bc6

Please sign in to comment.