From e769baff0ec68e080fe0ff0cded758fe3b3c1794 Mon Sep 17 00:00:00 2001 From: Rafael Santos Date: Mon, 15 Feb 2021 13:36:04 -0500 Subject: [PATCH] update validator and remove deprecation notice --- src/GenericApi.php | 12 +++++++++++- tests/Sample/SampleAPITest.php | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/GenericApi.php b/src/GenericApi.php index 03df2e3..b9c8d9a 100644 --- a/src/GenericApi.php +++ b/src/GenericApi.php @@ -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) { diff --git a/tests/Sample/SampleAPITest.php b/tests/Sample/SampleAPITest.php index e8159bb..f7d0051 100644 --- a/tests/Sample/SampleAPITest.php +++ b/tests/Sample/SampleAPITest.php @@ -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; @@ -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);