Skip to content

Commit

Permalink
add ApiProblemErrorMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Dec 24, 2019
1 parent 4f766fa commit 79b4947
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 30 deletions.
60 changes: 31 additions & 29 deletions README.md
Expand Up @@ -27,7 +27,7 @@ A simple validation.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-validation][1].

```sh
composer require chubbyphp/chubbyphp-validation "^3.6"
composer require chubbyphp/chubbyphp-validation "^3.7"
```

## Usage
Expand Down Expand Up @@ -69,19 +69,20 @@ composer require chubbyphp/chubbyphp-validation "^3.6"

### Error

* [Error][4]
* [ErrorMessages][5]
* [NestedErrorMessages][6]
* [ApiProblemErrorMessages][4]
* [Error][5]
* [ErrorMessages][6]
* [NestedErrorMessages][7]

### Mapping

* [CallableValidationMappingProvider][7]
* [LazyValidationMappingProvider][8]
* [ValidationClassMapping][9]
* [ValidationClassMappingBuilder][10]
* [ValidationMappingProviderRegistry][11]
* [ValidationPropertyMapping][12]
* [ValidationPropertyMappingBuilder][13]
* [CallableValidationMappingProvider][8]
* [LazyValidationMappingProvider][9]
* [ValidationClassMapping][10]
* [ValidationClassMappingBuilder][11]
* [ValidationMappingProviderRegistry][12]
* [ValidationPropertyMapping][13]
* [ValidationPropertyMappingBuilder][14]

#### ValidationMappingProvider

Expand Down Expand Up @@ -158,16 +159,16 @@ final class ModelValidationMappingProvider implements ValidationMappingProviderI

### ServiceFactory

* [ValidationServiceFactory][14]
* [ValidationServiceFactory][15]

### ServiceProvider

* [ValidationServiceProvider][15]
* [ValidationServiceProvider][16]

### Validator

* [ValidatorContext][16]
* [ValidatorContextBuilder][17]
* [ValidatorContext][17]
* [ValidatorContextBuilder][18]

```php
<?php
Expand Down Expand Up @@ -234,20 +235,21 @@ Dominik Zogg 2018

[121]: doc/Constraint/Symfony/ConstraintAdapter.md

[4]: doc/Error/Error.md
[5]: doc/Error/ErrorMessages.md
[6]: doc/Error/NestedErrorMessages.md
[4]: doc/Error/ApiProblemErrorMessages.md
[5]: doc/Error/Error.md
[6]: doc/Error/ErrorMessages.md
[7]: doc/Error/NestedErrorMessages.md

[7]: doc/Mapping/CallableValidationMappingProvider.md
[8]: doc/Mapping/LazyValidationMappingProvider.md
[9]: doc/Mapping/ValidationClassMapping.md
[10]: doc/Mapping/ValidationClassMappingBuilder.md
[11]: doc/Mapping/ValidationMappingProviderRegistry.md
[12]: doc/Mapping/ValidationPropertyMapping.md
[13]: doc/Mapping/ValidationPropertyMappingBuilder.md
[8]: doc/Mapping/CallableValidationMappingProvider.md
[9]: doc/Mapping/LazyValidationMappingProvider.md
[10]: doc/Mapping/ValidationClassMapping.md
[11]: doc/Mapping/ValidationClassMappingBuilder.md
[12]: doc/Mapping/ValidationMappingProviderRegistry.md
[13]: doc/Mapping/ValidationPropertyMapping.md
[14]: doc/Mapping/ValidationPropertyMappingBuilder.md

[14]: doc/ServiceFactory/ValidationServiceFactory.md
[15]: doc/ServiceProvider/ValidationServiceProvider.md
[15]: doc/ServiceFactory/ValidationServiceFactory.md
[16]: doc/ServiceProvider/ValidationServiceProvider.md

[16]: doc/ValidatorContext.md
[17]: doc/ValidatorContextBuilder.md
[17]: doc/ValidatorContext.md
[18]: doc/ValidatorContextBuilder.md
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -41,7 +41,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.6-dev"
"dev-master": "3.7-dev"
}
},
"scripts": {
Expand Down
25 changes: 25 additions & 0 deletions doc/Error/ApiProblemErrorMessages.md
@@ -0,0 +1,25 @@
# ApiProblemErrorMessages

```php
<?php

use Chubbyphp\Validation\Error\Error;
use Chubbyphp\Validation\Error\ApiProblemErrorMessages;

$error = new Error(
'path.to.property',
'constraint.constraint.invalidtype',
['type' => 'array']
);

$errorMessages = new ApiProblemErrorMessages([$error]);

$errorMessages->getMessages();
// [
// [
// 'name' => 'path.to.property',
// 'reason' => 'constraint.constraint.invalidtype',
// 'details' => ['type' => 'array'],
// ],
// ]
```
53 changes: 53 additions & 0 deletions src/Error/ApiProblemErrorMessages.php
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Validation\Error;

final class ApiProblemErrorMessages implements ErrorMessagesInterface
{
/**
* @var array<ErrorInterface>
*/
private $errors;

/**
* @var array<int, array<string, string|array>>
*/
private $errorMessages;

/**
* @param array<ErrorInterface> $errors
*/
public function __construct(array $errors)
{
$this->errors = [];
foreach ($errors as $error) {
$this->addError($error);
}
}

/**
* @return array<int, array<string, string|array>>
*/
public function getMessages(): array
{
if (null === $this->errorMessages) {
$this->errorMessages = [];
foreach ($this->errors as $error) {
$this->errorMessages[] = [
'name' => $error->getPath(),
'reason' => $error->getKey(),
'details' => $error->getArguments(),
];
}
}

return $this->errorMessages;
}

private function addError(ErrorInterface $error): void
{
$this->errors[] = $error;
}
}
80 changes: 80 additions & 0 deletions tests/Unit/Error/ApiProblemErrorMessagesTest.php
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Tests\Validation\Unit\Error;

use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Validation\Error\ApiProblemErrorMessages;
use Chubbyphp\Validation\Error\ErrorInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @covers \Chubbyphp\Validation\Error\ApiProblemErrorMessages
*
* @internal
*/
final class ApiProblemErrorMessagesTest extends TestCase
{
use MockByCallsTrait;

public function testWithoutMessages(): void
{
$errorMessages = new ApiProblemErrorMessages([]);

self::assertEquals([], $errorMessages->getMessages());
}

public function testWithMessages(): void
{
$errors = [
$this->getError('collection[_all]', 'constraint.collection.all'),
$this->getError('collection[0].field1', 'constraint.collection0.constraint1', ['key' => 'value']),
$this->getError('collection[0].field1', 'constraint.collection0.constraint2'),
$this->getError('collection[1].field1', 'constraint.collection1.constraint1'),
$this->getError('collection[1].field1', 'constraint.collection1.constraint2'),
];

$errorMessages = new ApiProblemErrorMessages($errors);

self::assertEquals([
[
'name' => 'collection[_all]',
'reason' => 'constraint.collection.all',
'details' => [],
],
[
'name' => 'collection[0].field1',
'reason' => 'constraint.collection0.constraint1',
'details' => ['key' => 'value'],
],
[
'name' => 'collection[0].field1',
'reason' => 'constraint.collection0.constraint2',
'details' => [],
],
[
'name' => 'collection[1].field1',
'reason' => 'constraint.collection1.constraint1',
'details' => [],
],
[
'name' => 'collection[1].field1',
'reason' => 'constraint.collection1.constraint2',
'details' => [],
],
], $errorMessages->getMessages());
}

private function getError(string $path, string $key, array $arguments = []): ErrorInterface
{
/* @var ErrorInterface|MockObject $error */
return $this->getMockByCalls(ErrorInterface::class, [
Call::create('getPath')->with()->willReturn($path),
Call::create('getKey')->with()->willReturn($key),
Call::create('getArguments')->with()->willReturn($arguments),
]);
}
}

0 comments on commit 79b4947

Please sign in to comment.