Skip to content

Commit

Permalink
update validator and remove deprecation notice
Browse files Browse the repository at this point in the history
  • Loading branch information
rafrsr committed Feb 15, 2021
1 parent c93ed54 commit e769baf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/GenericApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,17 @@ protected function buildClient($clientOptions)
*/
protected function validate(ApiServiceInterface $service)
{
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$builder = Validation::createValidatorBuilder();

//since symfony/validator 5.2 Not passing true as first argument to "%s" is deprecated.
if (method_exists($builder, 'addDefaultDoctrineAnnotationReader')) {
$builder->enableAnnotationMapping(true);
$builder->addDefaultDoctrineAnnotationReader();
} else {
$builder->enableAnnotationMapping();
}

$validator = $builder->getValidator();
$violations = $validator->validate($service);
/** @var ConstraintViolation $violation */
foreach ($violations as $violation) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Sample/SampleAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Rafrsr\GenericApi\ApiInterface;
use Rafrsr\GenericApi\Exception\InvalidApiDataException;
use Rafrsr\SampleApi\Model\Post;
use Rafrsr\SampleApi\SampleAPI;

Expand Down Expand Up @@ -69,6 +70,18 @@ public function testCreatePost()
static::assertEquals(101, $postCreated->getId());
}

public function testCreatePostValidation()
{
$post = new Post();
$post->setBody('facere repellat provident occaecati excepturi optio reprehenderit');
$post->setUserId('1');

$this->expectException(InvalidApiDataException::class);
$this->expectExceptionMessage('Error in field "post.title": This value should not be blank.');

$this->api->posts()->create($post);
}

public function testDeletePost()
{
$post = $this->api->posts()->get(1);
Expand Down

0 comments on commit e769baf

Please sign in to comment.