/** * @test */ public function mergeShouldMergeTwoResults() { $notice1 = $this->getMockMessage('Notice'); $notice2 = $this->getMockMessage('Notice'); $notice3 = $this->getMockMessage('Notice'); $warning1 = $this->getMockMessage('Warning'); $warning2 = $this->getMockMessage('Warning'); $warning3 = $this->getMockMessage('Warning'); $error1 = $this->getMockMessage('Error'); $error2 = $this->getMockMessage('Error'); $error3 = $this->getMockMessage('Error'); $otherResult = new \TYPO3\CMS\Extbase\Error\Result(); $otherResult->addNotice($notice1); $otherResult->forProperty('foo.bar')->addNotice($notice2); $this->result->forProperty('foo')->addNotice($notice3); $otherResult->addWarning($warning1); $this->result->addWarning($warning2); $this->result->addWarning($warning3); $otherResult->forProperty('foo')->addError($error1); $otherResult->forProperty('foo')->addError($error2); $otherResult->addError($error3); $this->result->merge($otherResult); $this->assertSame(array($notice1), $this->result->getNotices(), 'Notices are not merged correctly without recursion'); $this->assertSame(array($notice3), $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.'); $this->assertSame(array($notice2), $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.'); $this->assertSame(array($warning2, $warning3, $warning1), $this->result->getWarnings()); $this->assertSame(array($error3), $this->result->getErrors()); $this->assertSame(array($error1, $error2), $this->result->forProperty('foo')->getErrors()); }
/** * Add an error with message and code to the property errors * * @param string $propertyName name of the property to add the error to * @param string $message Message to be shwon * @param string $code Error code to identify the error * @return void */ protected function ___addErrorForProperty($propertyName, $message, $code) { if (!$this->result->forProperty($propertyName)->hasErrors()) { /** @var $error \TYPO3\CMS\Extbase\Validation\PropertyError */ $error = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Validation\\PropertyError', $propertyName); $error->addErrors(array($this->objectManager->get('TYPO3\\CMS\\Extbase\\Validation\\Error', $message, $code))); $this->result->addError($error); } }
/** * Internal function which actually does the property mapping. * * @param mixed $source the source data to map. MUST be a simple type, NO object allowed! * @param string $targetType The type of the target; can be either a class name or a simple type. * @param PropertyMappingConfigurationInterface $configuration Configuration for the property mapping. * @param array &$currentPropertyPath The property path currently being mapped; used for knowing the context in case an exception is thrown. * @throws Exception\TypeConverterException * @throws Exception\InvalidPropertyMappingConfigurationException * @return mixed an instance of $targetType */ protected function doMapping($source, $targetType, PropertyMappingConfigurationInterface $configuration, &$currentPropertyPath) { if (is_object($source)) { $targetType = $this->parseCompositeType($targetType); if ($source instanceof $targetType) { return $source; } } if ($source === null) { $source = ''; } $typeConverter = $this->findTypeConverter($source, $targetType, $configuration); $targetType = $typeConverter->getTargetTypeForSource($source, $targetType, $configuration); if (!is_object($typeConverter) || !$typeConverter instanceof \TYPO3\CMS\Extbase\Property\TypeConverterInterface) { throw new Exception\TypeConverterException('Type converter for "' . $source . '" -> "' . $targetType . '" not found.'); } $convertedChildProperties = array(); foreach ($typeConverter->getSourceChildPropertiesToBeConverted($source) as $sourcePropertyName => $sourcePropertyValue) { $targetPropertyName = $configuration->getTargetPropertyName($sourcePropertyName); if ($configuration->shouldSkip($targetPropertyName)) { continue; } if (!$configuration->shouldMap($targetPropertyName)) { if ($configuration->shouldSkipUnknownProperties()) { continue; } throw new Exception\InvalidPropertyMappingConfigurationException('It is not allowed to map property "' . $targetPropertyName . '". You need to use $propertyMappingConfiguration->allowProperties(\'' . $targetPropertyName . '\') to enable mapping of this property.', 1355155913); } $targetPropertyType = $typeConverter->getTypeOfChildProperty($targetType, $targetPropertyName, $configuration); $subConfiguration = $configuration->getConfigurationFor($targetPropertyName); $currentPropertyPath[] = $targetPropertyName; $targetPropertyValue = $this->doMapping($sourcePropertyValue, $targetPropertyType, $subConfiguration, $currentPropertyPath); array_pop($currentPropertyPath); if (!$targetPropertyValue instanceof \TYPO3\CMS\Extbase\Error\Error) { $convertedChildProperties[$targetPropertyName] = $targetPropertyValue; } } $result = $typeConverter->convertFrom($source, $targetType, $convertedChildProperties, $configuration); if ($result instanceof \TYPO3\CMS\Extbase\Error\Error) { $this->messages->forProperty(implode('.', $currentPropertyPath))->addError($result); } return $result; }