From aa41bc6524bb5d88ac9188d20c0de75f02168a2b Mon Sep 17 00:00:00 2001 From: Zizaco Zizuini Date: Sat, 9 Apr 2016 11:01:20 -0300 Subject: [PATCH] Added new test to DataMapper in order to make sure that an Event handler response can bail out database operations (save, update, insert and delete) --- tests/Mongolid/DataMapper/DataMapperTest.php | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/Mongolid/DataMapper/DataMapperTest.php b/tests/Mongolid/DataMapper/DataMapperTest.php index 1afd0a2d..11cdce92 100644 --- a/tests/Mongolid/DataMapper/DataMapperTest.php +++ b/tests/Mongolid/DataMapper/DataMapperTest.php @@ -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 @@ -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 [