Skip to content
Permalink
Browse files

OXDEV-1287 Remove BadMetaData event

* Introduced BadMetaData event;
* Throw exception if schema is wrong.
  • Loading branch information...
MantasVaitkunas authored and iegupov committed Jun 27, 2019
1 parent db948b9 commit c4a181fe5f5ce74ed5521cc15a5276055c0309ec
Showing with 260 additions and 633 deletions.
  1. +0 −1 CHANGELOG.md
  2. +0 −58 source/Internal/Module/MetaData/Event/BadMetaDataFoundEvent.php
  3. +15 −0 source/Internal/Module/MetaData/Exception/ModuleIdNotValidException.php
  4. +10 −34 source/Internal/Module/MetaData/Service/MetaDataNormalizer.php
  5. +1 −34 source/Internal/Module/MetaData/Service/MetaDataProvider.php
  6. +2 −12 source/Internal/Module/MetaData/Service/MetaDataSchemataProvider.php
  7. +9 −17 source/Internal/Module/MetaData/Validator/MetaDataSchemaValidator.php
  8. +28 −0 source/Internal/Module/MetaData/Validator/ModuleIdValidator.php
  9. +6 −2 source/Internal/Module/MetaData/bootstrap-services.yaml
  10. +45 −100 tests/Integration/Internal/Module/MetaData/MetaDataMapperTest.php
  11. +9 −38 tests/Integration/Internal/Module/MetaData/Service/MetaDataProviderTest.php
  12. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData20/Article.php
  13. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData20/Controller.php
  14. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData20/OtherController.php
  15. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData20/Payment.php
  16. +17 −10 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData20/metadata.php
  17. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData21/Article.php
  18. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData21/Controller.php
  19. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData21/OtherController.php
  20. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData21/Payment.php
  21. +15 −8 tests/Integration/Internal/Module/MetaData/TestData/TestModuleMetaData21/metadata.php
  22. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithPartialMetaData/Article.php
  23. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithPartialMetaData/Payment.php
  24. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithSurplusData/Article.php
  25. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithSurplusData/Controller.php
  26. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithSurplusData/OtherController.php
  27. +0 −12 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithSurplusData/Payment.php
  28. +1 −1 tests/Integration/Internal/Module/MetaData/TestData/TestModuleWithSurplusData/metadata.php
  29. +3 −95 tests/Unit/Internal/Module/MetaData/Service/MetaDataNormalizerTest.php
  30. +14 −14 tests/Unit/Internal/Module/MetaData/Service/MetaDataSchemataProviderTest.php
  31. +24 −41 tests/Unit/Internal/Module/MetaData/Validator/MetaDataSchemaValidatorTest.php
  32. +61 −0 tests/Unit/Internal/Module/MetaData/Validator/ModuleIdValidatorTest.php
@@ -46,7 +46,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- New parameter in config file to change database connection charset - `dbCharset` [PR-670](https://github.com/OXID-eSales/oxideshop_ce/pull/670)

- Events:
- `\OxidEsales\EshopCommunity\Internal\Module\MetaData\Event\BadMetaDataFoundEvent`
- `\OxidEsales\EshopCommunity\Internal\Module\Setup\Event\BeforeModuleDeactivationEvent`
- `\OxidEsales\EshopCommunity\Internal\Module\Setup\Event\FinalizingModuleActivationEvent`
- `\OxidEsales\EshopCommunity\Internal\Module\Setup\Event\FinalizingModuleDeactivationEvent`

This file was deleted.

@@ -0,0 +1,15 @@
<?php declare(strict_types=1);
/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/
namespace OxidEsales\EshopCommunity\Internal\Module\MetaData\Exception;
/**
* @internal
*/
class ModuleIdNotValidException extends \Exception
{
}
@@ -22,11 +22,10 @@ class MetaDataNormalizer implements MetaDataNormalizerInterface
*/
public function normalizeData(array $data): array
{
$normalizedMetaData = [];
$normalizedMetaData = $data;
foreach ($data as $key => $value) {
$normalizedKey = strtolower($key);
$normalizedValue = $this->normalizeValues($normalizedKey, $value);
$normalizedMetaData[$normalizedKey] = $normalizedValue;
$normalizedValue = $this->lowerCaseFileClassesNames($key, $value);
$normalizedMetaData[$key] = $normalizedValue;
}
if (isset($normalizedMetaData[MetaDataProvider::METADATA_SETTINGS])) {
@@ -85,39 +84,16 @@ private function normalizeMultiLanguageField(array $normalizedMetaData, string $
*
* @return mixed
*/
private function normalizeValues($key, $value)
private function lowerCaseFileClassesNames($key, $value)
{
$subItemsToNormalize = [
MetaDataProvider::METADATA_DESCRIPTION,
MetaDataProvider::METADATA_BLOCKS,
MetaDataProvider::METADATA_SETTINGS,
MetaDataProvider::METADATA_FILES
];
if (\is_array($value) && in_array($key, $subItemsToNormalize, true)) {
$normalizedValue = $this->lowerCaseArrayKeysRecursive($value);
} else {
$normalizedValue = $value;
$normalizedValue = $value;
if (\is_array($value) && $key === MetaDataProvider::METADATA_FILES) {
$normalizedValue = [];
foreach ($value as $className => $path) {
$normalizedValue[strtolower($className)] = $path;
}
}
return $normalizedValue;
}
/**
* @param array $array
*
* @return array
*/
private function lowerCaseArrayKeysRecursive(array $array): array
{
return array_map(
function ($item) {
if (\is_array($item)) {
$item = $this->lowerCaseArrayKeysRecursive($item);
}
return $item;
},
array_change_key_case($array)
);
}
}
@@ -8,7 +8,6 @@
use OxidEsales\EshopCommunity\Internal\Application\Utility\BasicContextInterface;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Converter\MetaDataConverterInterface;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Event\BadMetaDataFoundEvent;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Exception\InvalidMetaDataException;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator\MetaDataValidatorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -35,7 +34,7 @@ class MetaDataProvider implements MetaDataProviderInterface
const METADATA_EVENTS = 'events';
const METADATA_TEMPLATES = 'templates';
const METADATA_SETTINGS = 'settings';
const METADATA_SMARTY_PLUGIN_DIRECTORIES = 'smartyplugindirectories';
const METADATA_SMARTY_PLUGIN_DIRECTORIES = 'smartyPluginDirectories';
const METADATA_FILEPATH = 'metaDataFilePath';
const METADATA_FILES = 'files';
@@ -69,20 +68,17 @@ class MetaDataProvider implements MetaDataProviderInterface
private $metaDataConverter;
/**
* @param EventDispatcherInterface $eventDispatcher
* @param MetaDataNormalizerInterface $metaDataNormalizer
* @param BasicContextInterface $context
* @param MetaDataValidatorInterface $metaDataValidator
* @param MetaDataConverterInterface $metaDataConverter
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
MetaDataNormalizerInterface $metaDataNormalizer,
BasicContextInterface $context,
MetaDataValidatorInterface $metaDataValidator,
MetaDataConverterInterface $metaDataConverter
) {
$this->eventDispatcher = $eventDispatcher;
$this->metaDataNormalizer = $metaDataNormalizer;
$this->context = $context;
$this->metaDataValidatorService = $metaDataValidator;
@@ -127,7 +123,6 @@ private function getNormalizedMetaDataFileContent(): array
$moduleData = $this->metaDataConverter->convert($moduleData);
$normalizedMetaData = $this->metaDataNormalizer->normalizeData($moduleData);
$normalizedMetaData[static::METADATA_ID] = $this->sanitizeMetaDataId($normalizedMetaData);
if (isset($normalizedMetaData[static::METADATA_EXTEND])) {
$normalizedMetaData[static::METADATA_EXTEND] = $this->sanitizeExtendedClasses($normalizedMetaData);
}
@@ -150,34 +145,6 @@ private function addFilePathToData(array $normalizedMetaData): array
return $normalizedMetaData;
}
/**
* @return string
*/
private function getModuleDirectoryName(): string
{
return trim(basename(\dirname($this->filePath)), DIRECTORY_SEPARATOR);
}
/**
* @param array $metaData
*
* @return string
*/
private function sanitizeMetaDataId(array $metaData): string
{
$metaDataId = $metaData[static::METADATA_ID] ?? '';
if ('' === $metaDataId) {
$message = 'No metadata key "id" was not found in ' . $this->filePath;
$event = new BadMetaDataFoundEvent($this->filePath, $message);
$this->eventDispatcher->dispatch($event::NAME, $event);
$metaDataId = trim(basename($this->getModuleDirectoryName()), DIRECTORY_SEPARATOR);
}
return $metaDataId;
}
/**
* @param mixed $metaDataVersion
* @param mixed $moduleData
@@ -81,22 +81,12 @@ private function arrayFlipRecursive(array $metaDataVersion): array
foreach ($metaDataVersion as $key => $item) {
if (is_numeric($key) && \is_string($item)) {
$transposedArray[$this->convertKeyToLowerCase($item)] = $key;
$transposedArray[$item] = $key;
} elseif (\is_string($key) && \is_array($item)) {
$transposedArray[$this->convertKeyToLowerCase($key)] = $this->arrayFlipRecursive($item);
$transposedArray[$key] = $this->arrayFlipRecursive($item);
}
}
return $transposedArray;
}
/**
* @param string $key
*
* @return string
*/
private function convertKeyToLowerCase(string $key): string
{
return strtolower($key);
}
}
@@ -6,12 +6,10 @@
namespace OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Event\BadMetaDataFoundEvent;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Exception\UnsupportedMetaDataKeyException;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Exception\UnsupportedMetaDataValueTypeException;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Service\MetaDataProvider;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Service\MetaDataSchemataProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* @internal
@@ -39,11 +37,6 @@ class MetaDataSchemaValidator implements MetaDataSchemaValidatorInterface
*/
private $currentValidationMetaDataVersion;
/**
* @var EventDispatcher
*/
private $eventDispatcher;
/**
* @var string
*/
@@ -53,12 +46,10 @@ class MetaDataSchemaValidator implements MetaDataSchemaValidatorInterface
* MetaDataValidator constructor.
*
* @param MetaDataSchemataProviderInterface $metaDataSchemataProvider
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(MetaDataSchemataProviderInterface $metaDataSchemataProvider, EventDispatcherInterface $eventDispatcher)
public function __construct(MetaDataSchemataProviderInterface $metaDataSchemataProvider)
{
$this->metaDataSchemataProvider = $metaDataSchemataProvider;
$this->eventDispatcher = $eventDispatcher;
}
/**
@@ -88,17 +79,18 @@ public function validate(string $metaDataFilePath, string $metaDataVersion, arra
}
/**
* @param array $supportedMetaDataKeys
* @param array $supportedMetaDataKeys
*
* @param string $metaDataKey
* @throws UnsupportedMetaDataKeyException
*/
private function validateMetaDataKey(array $supportedMetaDataKeys, string $metaDataKey)
private function validateMetaDataKey(array $supportedMetaDataKeys, string $metaDataKey): void
{
if (false === array_key_exists($metaDataKey, $supportedMetaDataKeys)) {
$message = 'The metadata key "' . $metaDataKey . '" is not supported in metadata version "' . $this->currentValidationMetaDataVersion . '"';
$event = new BadMetaDataFoundEvent($this->metaDataFilePath, $message);
$this->eventDispatcher->dispatch($event::NAME, $event);
throw new UnsupportedMetaDataKeyException(
'The metadata key "' . $metaDataKey . '" is not supported in metadata version "'
. $this->currentValidationMetaDataVersion . '".'
);
}
}
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/
namespace OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Exception\ModuleIdNotValidException;
use OxidEsales\EshopCommunity\Internal\Module\MetaData\Service\MetaDataProvider;
/**
* @internal
*/
class ModuleIdValidator implements MetaDataValidatorInterface
{
/**
* @param array $metaData
* @throws ModuleIdNotValidException
*/
public function validate(array $metaData): void
{
$metaDataId = $metaData[MetaDataProvider::METADATA_ID] ?? '';
if ($metaDataId === '') {
throw new ModuleIdNotValidException('Module ID is not provided in metadata file.');
}
}
}
@@ -90,15 +90,19 @@ services:
OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator\MetaDataValidatorInterface:
class: OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator\MetaDataValidatorAggregate
arguments:
- '@oxid_esales.module.meta_data.validator.module_id_validator'
- '@oxid_esales.module.meta_data.validator.shop_module_setting_boolean_validator'

oxid_esales.module.meta_data.validator.shop_module_setting_boolean_validator:
class: OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator\ShopModuleSettingBooleanValidator

oxid_esales.module.meta_data.validator.module_id_validator:
class: OxidEsales\EshopCommunity\Internal\Module\MetaData\Validator\ModuleIdValidator

OxidEsales\EshopCommunity\Internal\Module\MetaData\Converter\MetaDataConverterInterface:
class: OxidEsales\EshopCommunity\Internal\Module\MetaData\Converter\MetaDataConverterAggregate
arguments:
- '@oxid_esales.module.setup.converter.shop_module_settings_boolean_converter'
- '@oxid_esales.module.meta_data.converter.shop_module_settings_boolean_converter'

oxid_esales.module.setup.converter.shop_module_settings_boolean_converter:
oxid_esales.module.meta_data.converter.shop_module_settings_boolean_converter:
class: OxidEsales\EshopCommunity\Internal\Module\MetaData\Converter\ShopModuleSettingsBooleanConverter

0 comments on commit c4a181f

Please sign in to comment.
You can’t perform that action at this time.