Skip to content

Commit

Permalink
chore: improve compatibility with Symfony 6
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Dec 24, 2021
1 parent 57ff81f commit db027c8
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 89 deletions.
14 changes: 9 additions & 5 deletions composer.json
Expand Up @@ -28,12 +28,12 @@
},
"require-dev": {
"behat/behat": "^3.1",
"behat/mink": "^1.7",
"behat/mink": "^1.9@dev",
"doctrine/annotations": "^1.7",
"doctrine/cache": "^1.11 || ^2.1",
"doctrine/common": "^2.11 || ^3.0",
"doctrine/data-fixtures": "^1.2.2",
"doctrine/dbal": "^2.6",
"doctrine/dbal": "^2.6 || ^3.0",
"doctrine/doctrine-bundle": "^1.12 || ^2.0",
"doctrine/mongodb-odm": "^2.2",
"doctrine/mongodb-odm-bundle": "^4.0",
Expand All @@ -55,7 +55,7 @@
"psr/log": "^1.0 || ^2.0 || ^3.0",
"ramsey/uuid": "^3.7 || ^4.0",
"ramsey/uuid-doctrine": "^1.4",
"soyuka/contexts": "^3.3.1",
"soyuka/contexts": "dev-main",
"soyuka/stubs-mongodb": "^1.0",
"symfony/asset": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/browser-kit": "^4.4 || ^5.1 || ^6.0",
Expand All @@ -75,7 +75,7 @@
"symfony/http-client": "^4.4 || ^5.1 || ^6.0",
"symfony/mercure-bundle": "*",
"symfony/messenger": "^4.4 || ^5.1 || ^6.0",
"symfony/phpunit-bridge": "^5.4@dev",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"symfony/routing": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/security-bundle": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/security-core": "^4.4 || ^5.1 || ^6.0",
Expand Down Expand Up @@ -123,7 +123,11 @@
"preferred-install": {
"*": "dist"
},
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"phpstan/extension-installer": true
}
},
"extra": {
"branch-alias": {
Expand Down
4 changes: 2 additions & 2 deletions src/HttpCache/EventListener/AddHeadersListener.php
Expand Up @@ -97,11 +97,11 @@ public function onKernelResponse(ResponseEvent $event): void
}

if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
$response->headers->addCacheControlDirective('stale-while-revalidate', $staleWhileRevalidate);
$response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
}

if (null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
$response->headers->addCacheControlDirective('stale-if-error', $staleIfError);
$response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
}
}
}
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Bridge\Doctrine\EventListener\PurgeHttpCacheListener;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use ApiPlatform\Core\HttpCache\PurgerInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyNoGetOperation;
Expand Down Expand Up @@ -67,6 +68,7 @@ public function testOnFlush()
$iriConverterProphecy->getIriFromItem($toDelete1)->willReturn('/dummies/3')->shouldBeCalled();
$iriConverterProphecy->getIriFromItem($toDelete2)->willReturn('/dummies/4')->shouldBeCalled();
$iriConverterProphecy->getIriFromItem($toDeleteNoPurge)->shouldNotBeCalled();
$iriConverterProphecy->getIriFromItem(Argument::any())->willThrow(new ItemNotFoundException());

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(Argument::type(Dummy::class))->willReturn(Dummy::class)->shouldBeCalled();
Expand Down
10 changes: 5 additions & 5 deletions tests/Bridge/Doctrine/EventListener/WriteListenerTest.php
Expand Up @@ -49,7 +49,7 @@ public function testOnKernelViewWithControllerResultAndPostMethod()
$request = new Request();
$request->setMethod('POST');
$request->attributes->set('_api_resource_class', 'Dummy');
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy);
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $dummy);

$writeListener->onKernelView($event);
}
Expand All @@ -73,7 +73,7 @@ public function testOnKernelViewWithControllerResultAndDeleteMethod()
$request->setMethod('DELETE');
$request->attributes->set('_api_resource_class', 'Dummy');

$event = new ViewEvent($this->prophesize(HttpKernelInterface::class)->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy);
$event = new ViewEvent($this->prophesize(HttpKernelInterface::class)->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $dummy);
$writeListener->onKernelView($event);
}

Expand All @@ -95,7 +95,7 @@ public function testOnKernelViewWithSafeMethod()
$request = new Request();
$request->setMethod('HEAD');
$request->attributes->set('_api_resource_class', 'Dummy');
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy);
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $dummy);

$writeListener->onKernelView($event);
}
Expand All @@ -117,7 +117,7 @@ public function testOnKernelViewWithNoResourceClass()
$httpKernelProphecy = $this->prophesize(HttpKernelInterface::class);
$request = new Request();
$request->setMethod('POST');
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy);
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $dummy);

$writeListener->onKernelView($event);
}
Expand All @@ -139,7 +139,7 @@ public function testOnKernelViewWithNoManager()
$request = new Request();
$request->setMethod('DELETE');
$request->attributes->set('_api_resource_class', 'Dummy');
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy);
$event = new ViewEvent($httpKernelProphecy->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $dummy);

$writeListener->onKernelView($event);
}
Expand Down
Expand Up @@ -51,7 +51,7 @@ public function testNotAnApiPlatformRequest()

$listener = new ValidateListener($validator, $resourceMetadataFactory);

$event = new ViewEvent($this->prophesize(HttpKernelInterface::class)->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, []);
$event = new ViewEvent($this->prophesize(HttpKernelInterface::class)->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, []);
$listener->onKernelView($event);
}

Expand All @@ -60,8 +60,10 @@ public function testValidatorIsCalled()
$data = new DummyEntity();
$expectedValidationGroups = ['a', 'b', 'c'];

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$constraintViolationList = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationList->count()->willReturn(0);

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$validatorProphecy->validate($data, null, $expectedValidationGroups)->willReturn($constraintViolationList)->shouldBeCalled();
$validator = $validatorProphecy->reveal();

Expand All @@ -79,8 +81,10 @@ public function testGetGroupsFromCallable()
$data = new DummyEntity();
$expectedValidationGroups = ['a', 'b', 'c'];

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$constraintViolationList = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationList->count()->willReturn(0);

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$validatorProphecy->validate($data, null, $expectedValidationGroups)->willReturn($constraintViolationList)->shouldBeCalled();
$validator = $validatorProphecy->reveal();

Expand All @@ -98,8 +102,10 @@ public function testGetGroupsFromService()
{
$data = new DummyEntity();

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$constraintViolationList = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationList->count()->willReturn(0);

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$validatorProphecy->validate($data, null, ['a', 'b', 'c'])->willReturn($constraintViolationList)->shouldBeCalled();
$validator = $validatorProphecy->reveal();

Expand All @@ -124,8 +130,10 @@ public function testValidatorWithScalarGroup()
$data = new DummyEntity();
$expectedValidationGroups = ['foo'];

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$constraintViolationList = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationList->count()->willReturn(0);

$validatorProphecy = $this->prophesize(ValidatorInterface::class);
$validatorProphecy->validate($data, null, $expectedValidationGroups)->willreturn($constraintViolationList)->shouldBeCalled();

$containerProphecy = $this->prophesize(ContainerInterface::class);
Expand Down Expand Up @@ -197,7 +205,7 @@ private function createEventObject($expectedValidationGroups, $data, bool $recei
]);

$request->setMethod('POST');
$event = new ViewEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $data);
$event = new ViewEvent($kernel, $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $data);

return [$resourceMetadataFactory, $event];
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\ConstraintViolationList;

Expand All @@ -34,11 +35,15 @@ class ValidationExceptionListenerTest extends TestCase

public function testNotValidationException()
{
if (!class_exists(Countries::class)) {
$this->markTestSkipped('symfony/intl not installed');
}

$listener = new ValidationExceptionListener(
$this->prophesize(SerializerInterface::class)->reveal(),
['hydra' => ['application/ld+json']]);

$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), HttpKernelInterface::MASTER_REQUEST, new \Exception());
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new \Exception());
$listener->onKernelException($event);
$this->assertNull($event->getResponse());
}
Expand All @@ -52,7 +57,7 @@ public function testValidationException()
$serializerProphecy->serialize($list, 'hydra')->willReturn($exceptionJson)->shouldBeCalled();

$listener = new ValidationExceptionListener($serializerProphecy->reveal(), ['hydra' => ['application/ld+json']]);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), HttpKernelInterface::MASTER_REQUEST, new ValidationException($list));
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new ValidationException($list));
$listener->onKernelException($event);

$response = $event->getResponse();
Expand All @@ -73,7 +78,7 @@ public function testValidationFilterException()
$serializerProphecy->serialize($exception, 'hydra')->willReturn($exceptionJson)->shouldBeCalled();

$listener = new ValidationExceptionListener($serializerProphecy->reveal(), ['hydra' => ['application/ld+json']]);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), HttpKernelInterface::MASTER_REQUEST, $exception);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $exception);
$listener->onKernelException($event);

$response = $event->getResponse();
Expand Down
Expand Up @@ -56,6 +56,10 @@ public function testSupports(Constraint $constraint, PropertyMetadata $propertyM

public function supportsProvider(): \Generator
{
if (!class_exists(AtLeastOneOf::class)) {
return;
}

yield 'supported' => [new AtLeastOneOf(['constraints' => []]), new PropertyMetadata(), true];

yield 'not supported' => [new Positive(), new PropertyMetadata(), false];
Expand Down
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Core\Tests\ProphecyTrait;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Countries;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Sequentially;
Expand Down Expand Up @@ -199,6 +200,10 @@ public function testCreateWithRequiredByDecorated()

public function testCreateWithPropertyWithValidationConstraints()
{
if (!class_exists(Countries::class)) {
$this->markTestSkipped('symfony/intl not installed');
}

$validatorClassMetadata = new ClassMetadata(DummyIriWithValidationEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);

Expand Down
8 changes: 8 additions & 0 deletions tests/Bridge/Symfony/Validator/ValidatorTest.php
Expand Up @@ -35,6 +35,8 @@ public function testValid()
$data = new DummyEntity();

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->count()->willReturn(0);

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, null, null)->willReturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
$symfonyValidator = $symfonyValidatorProphecy->reveal();
Expand Down Expand Up @@ -68,6 +70,8 @@ public function testGetGroupsFromCallable()
$expectedValidationGroups = ['a', 'b', 'c'];

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->count()->willReturn(0);

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, null, $expectedValidationGroups)->willReturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
$symfonyValidator = $symfonyValidatorProphecy->reveal();
Expand All @@ -83,6 +87,7 @@ public function testValidateGetGroupsFromService(): void
$data = new DummyEntity();

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->count()->willReturn(0);

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, null, ['a', 'b', 'c'])->willReturn($constraintViolationListProphecy)->shouldBeCalled();
Expand All @@ -109,6 +114,7 @@ public function testValidateGetGroupsFromLegacyService(): void
$data = new DummyEntity();

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->count()->willReturn(0);

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, null, ['a', 'b', 'c'])->willReturn($constraintViolationListProphecy);
Expand All @@ -132,6 +138,8 @@ public function testValidatorWithScalarGroup()
$expectedValidationGroups = ['foo'];

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->count()->willReturn(0);

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, null, $expectedValidationGroups)->willreturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
$symfonyValidator = $symfonyValidatorProphecy->reveal();
Expand Down
6 changes: 3 additions & 3 deletions tests/EventListener/ExceptionListenerTest.php
Expand Up @@ -39,7 +39,7 @@ public function testOnKernelException(Request $request)
$kernel->handle(Argument::type(Request::class), HttpKernelInterface::SUB_REQUEST, false)->willReturn(new Response())->shouldBeCalled();

$listener = new ExceptionListener('foo:bar', null, false, class_exists(ErrorListener::class) ? $this->prophesize(ErrorListener::class)->reveal() : null);
$event = new ExceptionEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, new \Exception());
$event = new ExceptionEvent($kernel->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new \Exception());
$listener->onKernelException($event);

$this->assertInstanceOf(Response::class, $event->getResponse());
Expand All @@ -56,7 +56,7 @@ public function getRequest()
public function testDoNothingWhenNotAnApiCall()
{
$listener = new ExceptionListener('foo:bar', null, false, class_exists(ErrorListener::class) ? $this->prophesize(ErrorListener::class)->reveal() : null);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), HttpKernelInterface::MASTER_REQUEST, new \Exception());
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new \Exception());
$listener->onKernelException($event);

$this->assertNull($event->getResponse());
Expand All @@ -68,7 +68,7 @@ public function testDoNothingWhenHtmlRequested()
$request->setRequestFormat('html');

$listener = new ExceptionListener('foo:bar', null, false, class_exists(ErrorListener::class) ? $this->prophesize(ErrorListener::class)->reveal() : null);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, new \Exception());
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), $request, \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new \Exception());
$listener->onKernelException($event);

$this->assertNull($event->getResponse());
Expand Down

0 comments on commit db027c8

Please sign in to comment.