Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dominik Zogg
committed
Dec 24, 2019
1 parent
4f766fa
commit 79b4947
Showing
5 changed files
with
190 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "3.6-dev" | ||
"dev-master": "3.7-dev" | ||
} | ||
}, | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'], | ||
// ], | ||
// ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
]); | ||
} | ||
} |