From cc5bb8e2f8392b58040108387ae52906b2707124 Mon Sep 17 00:00:00 2001 From: Dominic Tubach Date: Fri, 5 Jul 2024 16:03:14 +0200 Subject: [PATCH] Extract interface of `ValidationResult` This allows to use mocks in tests. The constructor of `ValidationResult` is only useful when a real validation was performed. --- .../Validation/ValidationResult.php | 10 ++-- .../Validation/ValidationResultInterface.php | 47 +++++++++++++++++++ .../JsonSchema/Validation/Validator.php | 2 +- .../Validation/ValidatorInterface.php | 2 +- 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 Civi/RemoteTools/JsonSchema/Validation/ValidationResultInterface.php diff --git a/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php b/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php index 66a7dc3..3cd4e9a 100644 --- a/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php +++ b/Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php @@ -26,7 +26,7 @@ use Systopia\JsonSchema\Translation\NullTranslator; use Systopia\JsonSchema\Translation\TranslatorInterface; -final class ValidationResult { +final class ValidationResult implements ValidationResultInterface { /** * @var array @@ -67,14 +67,14 @@ public function getTaggedData(): TaggedDataContainerInterface { } /** - * @return array> + * @return array> */ public function getErrorMessages(): array { return $this->mapErrorsToMessages($this->errorCollector->getErrors()); } /** - * @return array> + * @return array> */ public function getLeafErrorMessages(): array { return $this->mapErrorsToMessages($this->errorCollector->getLeafErrors()); @@ -89,9 +89,9 @@ public function isValid(): bool { } /** - * @param array> $errors + * @param array> $errors * - * @return array> + * @return array> */ private function mapErrorsToMessages(array $errors): array { return array_map( diff --git a/Civi/RemoteTools/JsonSchema/Validation/ValidationResultInterface.php b/Civi/RemoteTools/JsonSchema/Validation/ValidationResultInterface.php new file mode 100644 index 0000000..e3a9e7e --- /dev/null +++ b/Civi/RemoteTools/JsonSchema/Validation/ValidationResultInterface.php @@ -0,0 +1,47 @@ +. + */ + +declare(strict_types = 1); + +namespace Civi\RemoteTools\JsonSchema\Validation; + +use Systopia\JsonSchema\Tags\TaggedDataContainerInterface; + +interface ValidationResultInterface { + + /** + * @return array + */ + public function getData(): array; + + public function getTaggedData(): TaggedDataContainerInterface; + + /** + * @return array> + */ + public function getErrorMessages(): array; + + /** + * @return array> + */ + public function getLeafErrorMessages(): array; + + public function hasErrors(): bool; + + public function isValid(): bool; + +} diff --git a/Civi/RemoteTools/JsonSchema/Validation/Validator.php b/Civi/RemoteTools/JsonSchema/Validation/Validator.php index 5f3eed8..d6280f2 100644 --- a/Civi/RemoteTools/JsonSchema/Validation/Validator.php +++ b/Civi/RemoteTools/JsonSchema/Validation/Validator.php @@ -41,7 +41,7 @@ public function __construct(TranslatorInterface $translator, OpisValidator $vali * @inheritDoc * @throws \JsonException */ - public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResult { + public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResultInterface { $validationData = JsonConverter::toStdClass($data); $errorCollector = new ErrorCollector(); $taggedDataContainer = new TaggedDataContainer(); diff --git a/Civi/RemoteTools/JsonSchema/Validation/ValidatorInterface.php b/Civi/RemoteTools/JsonSchema/Validation/ValidatorInterface.php index 245518c..af2aa5f 100644 --- a/Civi/RemoteTools/JsonSchema/Validation/ValidatorInterface.php +++ b/Civi/RemoteTools/JsonSchema/Validation/ValidatorInterface.php @@ -26,6 +26,6 @@ interface ValidatorInterface { /** * @phpstan-param array $data JSON serializable. */ - public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResult; + public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResultInterface; }