/** * @test */ public function convertFromConvertsDateTimeImmutableObjects() { $date = new \DateTimeImmutable('1980-12-13'); $propertyMappingConfiguration = new PropertyMappingConfiguration(); $propertyMappingConfiguration->setTypeConverterOption(StringConverter::class, StringConverter::CONFIGURATION_DATE_FORMAT, 'd.m.Y'); $this->assertEquals('13.12.1980', $this->converter->convertFrom($date, 'string', [], $propertyMappingConfiguration)); }
/** * @test */ public function setTypeConverterOptionShouldOverrideAlreadySetOptions() { $mockTypeConverterClass = $this->getMockClass(TypeConverterInterface::class); $this->propertyMappingConfiguration->setTypeConverterOptions($mockTypeConverterClass, ['k1' => 'v1', 'k2' => 'v2']); $this->propertyMappingConfiguration->setTypeConverterOption($mockTypeConverterClass, 'k1', 'v3'); $this->assertEquals('v3', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k1')); $this->assertEquals('v2', $this->propertyMappingConfiguration->getConfigurationValue($mockTypeConverterClass, 'k2')); }
/** * @test */ public function convertFromProperlyConvertsArrayWithDateAsArray() { $source = ['day' => '13', 'month' => '10', 'year' => '2010']; $mappingConfiguration = new PropertyMappingConfiguration(); $mappingConfiguration->setTypeConverterOption(DateTimeConverter::class, DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'Y-m-d'); $date = $this->converter->convertFrom($source, 'DateTime', [], $mappingConfiguration); $actualResult = $date->format('Y-m-d'); $this->assertSame('2010-10-13', $actualResult); }
/** * Data provider with invalid configuration for target type overrides * * @return array */ public function invalidTypeConverterConfigurationsForOverridingTargetTypes() { $configurationWithNoSetting = new PropertyMappingConfiguration(); $configurationWithOverrideOff = new PropertyMappingConfiguration(); $configurationWithOverrideOff->setTypeConverterOption(ObjectConverter::class, ObjectConverter::CONFIGURATION_OVERRIDE_TARGET_TYPE_ALLOWED, false); return [[null], [$configurationWithNoSetting], [$configurationWithOverrideOff]]; }
/** * Modify the passed $propertyMappingConfiguration according to the $propertyConfiguration which * has been generated by Fluid. In detail, if the $propertyConfiguration contains * an __identity field, we allow modification of objects; else we allow creation. * * All other properties are specified as allowed properties. * * @param array $propertyConfiguration * @param PropertyMappingConfiguration $propertyMappingConfiguration * @return void */ protected function modifyPropertyMappingConfiguration($propertyConfiguration, PropertyMappingConfiguration $propertyMappingConfiguration) { if (!is_array($propertyConfiguration)) { return; } if (isset($propertyConfiguration['__identity'])) { $propertyMappingConfiguration->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED, true); unset($propertyConfiguration['__identity']); } else { $propertyMappingConfiguration->setTypeConverterOption(PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, true); } foreach ($propertyConfiguration as $innerKey => $innerValue) { if (is_array($innerValue)) { $this->modifyPropertyMappingConfiguration($innerValue, $propertyMappingConfiguration->forProperty($innerKey)); } $propertyMappingConfiguration->allowProperties($innerKey); } }
/** * @test * @expectedException \Neos\Flow\I18n\Exception\InvalidLocaleIdentifierException */ public function convertFromThrowsExceptionIfLocaleIsInvalid() { $configuration = new PropertyMappingConfiguration(); $configuration->setTypeConverterOption(FloatConverter::class, 'locale', 'some-non-existent-locale-identifier'); $this->converter->convertFrom('84,42', 'float', [], $configuration); }
/** * Parses the request body according to the media type. * * @param HttpRequest $httpRequest * @return array */ protected function parseRequestBody(HttpRequest $httpRequest) { $requestBody = $httpRequest->getContent(); if ($requestBody === null || $requestBody === '') { return []; } $mediaTypeConverter = $this->objectManager->get(MediaTypeConverterInterface::class); $propertyMappingConfiguration = new PropertyMappingConfiguration(); $propertyMappingConfiguration->setTypeConverter($mediaTypeConverter); $propertyMappingConfiguration->setTypeConverterOption(MediaTypeConverterInterface::class, MediaTypeConverterInterface::CONFIGURATION_MEDIA_TYPE, $httpRequest->getHeader('Content-Type')); $arguments = $this->propertyMapper->convert($requestBody, 'array', $propertyMappingConfiguration); return $arguments; }
/** * @param PropertyMappingConfiguration $propertyMappingConfiguration * @param string $typeConverterClass * @param string $dataType * @return void */ protected function setTypeConverterOptionsForType(PropertyMappingConfiguration $propertyMappingConfiguration, $typeConverterClass, $dataType) { if (!isset($this->typesConfiguration[$dataType]['typeConverterOptions']) || !is_array($this->typesConfiguration[$dataType]['typeConverterOptions'])) { return; } foreach ($this->typesConfiguration[$dataType]['typeConverterOptions'] as $option => $value) { $propertyMappingConfiguration->setTypeConverterOption($typeConverterClass, $option, $value); } }
/** * @test */ public function convertFromReturnsNullIfResourcePropertyIsNotConverted() { $this->mockObjectManager->expects($this->any())->method('getClassNameByObjectName')->will($this->returnCallback(function ($objectType) { return $objectType; })); $configuration = new PropertyMappingConfiguration(); $configuration->setTypeConverterOption(ImageInterfaceConverter::class, PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, true); $this->assertNull($this->converter->convertFrom(array(), Image::class, array(), $configuration)); }