Mongolid\DataMapper\SchemaMapperTest::testShouldMapToFieldsOfSchema PHP Метод

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

    public function testShouldMapToFieldsOfSchema()
    {
        // Arrange
        $schema = m::mock(Schema::class);
        $converter = m::mock(Converter::class);
        $schema->fields = ['name' => 'string', 'age' => 'int', 'stuff' => 'schema.My\\Own\\Schema'];
        $schemaMapper = m::mock(SchemaMapper::class . '[clearDynamic,parseField]', [$schema]);
        $schemaMapper->shouldAllowMockingProtectedMethods();
        $data = ['name' => 'John', 'age' => 23, 'stuff' => 'fooBar'];
        // Act
        $schemaMapper->shouldReceive('clearDynamic')->once()->with($data);
        foreach ($schema->fields as $key => $value) {
            $schemaMapper->shouldReceive('parseField')->once()->with($data[$key], $value)->andReturn($data[$key] . '.PARSED');
        }
        Ioc::instance(Converter::class, $converter);
        $converter->shouldReceive('toMongoTypes')->once()->andReturnUsing(function ($data) {
            foreach ($data as $key => $value) {
                $data[$key] = str_replace('23', 'batata', $value);
            }
            return $data;
        });
        // Assert
        $this->assertEquals(['name' => 'John.PARSED', 'age' => 'batata.PARSED', 'stuff' => 'fooBar.PARSED'], $schemaMapper->map($data));
    }