Mongolid\DataMapper\DataMapperTest::testShouldInsert PHP Метод

testShouldInsert() публичный Метод

public testShouldInsert ( $entity, $writeConcern, $shouldFireEventAfter, $expected )
    public function testShouldInsert($entity, $writeConcern, $shouldFireEventAfter, $expected)
    {
        // Arrange
        $connPool = m::mock(Pool::class);
        $mapper = m::mock(DataMapper::class . '[parseToDocument,getCollection]', [$connPool]);
        $options = ['writeConcern' => new WriteConcern($writeConcern)];
        $collection = m::mock(Collection::class);
        $parsedObject = ['_id' => 123];
        $operationResult = m::mock();
        $entity->_id = null;
        // Act
        $mapper->shouldAllowMockingProtectedMethods();
        $mapper->shouldReceive('parseToDocument')->once()->with($entity)->andReturn($parsedObject);
        $mapper->shouldReceive('getCollection')->once()->andReturn($collection);
        $collection->shouldReceive('insertOne')->once()->with($parsedObject, ['writeConcern' => new WriteConcern($writeConcern)])->andReturn($operationResult);
        $operationResult->shouldReceive('isAcknowledged')->once()->andReturn((bool) $writeConcern);
        $operationResult->shouldReceive('getInsertedCount')->andReturn(1);
        if ($entity instanceof AttributesAccessInterface) {
            $entity->shouldReceive('syncOriginalAttributes')->once()->with();
        }
        $this->expectEventToBeFired('inserting', $entity, true);
        if ($shouldFireEventAfter) {
            $this->expectEventToBeFired('inserted', $entity, false);
        } else {
            $this->expectEventNotToBeFired('inserted', $entity);
        }
        // Assert
        $this->assertEquals($expected, $mapper->insert($entity, $options));
    }