Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Symfony 5 #136

Merged
merged 4 commits into from Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions .travis.yml
@@ -1,21 +1,14 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
GwendolenLynch marked this conversation as resolved.
Show resolved Hide resolved
- 7.2
- 7.3

sudo: false

before_install:
- COMPOSER_MEMORY_LIMIT=-1 composer update

script: |
if [[ $TRAVIS_PHP_VERSION = 7.* ]]; then
SYMFONY_PHPUNIT_VERSION=6.4 ./vendor/bin/simple-phpunit
else
./vendor/bin/simple-phpunit
fi
./vendor/bin/simple-phpunit
16 changes: 8 additions & 8 deletions EventListener/CorsListener.php
Expand Up @@ -11,13 +11,13 @@

namespace Nelmio\CorsBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Nelmio\CorsBundle\Options\ResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Nelmio\CorsBundle\Options\ResolverInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Adds CORS headers and handles pre-flight requests
Expand Down Expand Up @@ -47,7 +47,7 @@ public function __construct(EventDispatcherInterface $dispatcher, ResolverInterf
$this->configurationResolver = $configurationResolver;
}

public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(RequestEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
Expand Down Expand Up @@ -83,7 +83,7 @@ public function onKernelRequest(GetResponseEvent $event)
$this->dispatcher->addListener('kernel.response', array($this, 'onKernelResponse'), 0);
}

public function onKernelResponse(FilterResponseEvent $event)
public function onKernelResponse(ResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
Expand All @@ -104,7 +104,7 @@ public function onKernelResponse(FilterResponseEvent $event)
}
}

public function forceAccessControlAllowOriginHeader(FilterResponseEvent $event)
public function forceAccessControlAllowOriginHeader(ResponseEvent $event)
{
if (!$options = $this->configurationResolver->getOptions($request = $event->getRequest())) {
return;
Expand Down
40 changes: 20 additions & 20 deletions Tests/CorsListenerTest.php
Expand Up @@ -11,18 +11,18 @@

namespace Nelmio\CorsBundle\Tests;

use Mockery as m;
use Nelmio\CorsBundle\EventListener\CorsListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class CorsListenerTest extends TestCase
{
public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down Expand Up @@ -65,7 +65,7 @@ public function testPreflightedRequest()
$req->headers->set('Access-Control-Request-Headers', 'Foo, BAR');

$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
Expand All @@ -89,9 +89,9 @@ public function testPreflightedRequest()
$callback = $cb;
});

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$event = new FilterResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, new Response());
$event = new ResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, new Response());
$this->getListener($dispatcher, $options)->onKernelResponse($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
Expand All @@ -114,7 +114,7 @@ public function testPreflightedRequestLinkFirefox()
$req->headers->set('Access-Control-Request-Method', 'Link');

$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
Expand All @@ -141,9 +141,9 @@ public function testPreflightedRequestWithForcedAllowOriginValue()
$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->shouldReceive('addListener')->once()->with('kernel.response', m::type('callable'), -1);

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$event = new FilterResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, $event->getResponse());
$event = new ResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, $event->getResponse());
$this->getListener($dispatcher, $options)->forceAccessControlAllowOriginHeader($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
Expand All @@ -167,9 +167,9 @@ public function testPreflightedRequestWithForcedAllowOriginValue()
$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->shouldReceive('addListener')->once()->with('kernel.response', m::type('callable'), -1);

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$event = new FilterResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, $event->getResponse());
$event = new ResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, $event->getResponse());
$this->getListener($dispatcher, $options)->forceAccessControlAllowOriginHeader($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
Expand All @@ -193,7 +193,7 @@ public function testSameHostRequest()

$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);

$this->assertNull($event->getResponse());
Expand All @@ -213,7 +213,7 @@ public function testRequestWithOriginButNo()
$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->shouldReceive('addListener')->times(0);

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);

$this->assertNull($event->getResponse());
Expand All @@ -235,9 +235,9 @@ public function testRequestWithForcedAllowOriginValue()
$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->shouldReceive('addListener')->twice()->with('kernel.response', m::type('callable'), m::type('integer'));

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$event = new FilterResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, new Response());
$event = new ResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, new Response());
$this->getListener($dispatcher, $options)->onKernelResponse($event);
$this->getListener($dispatcher, $options)->forceAccessControlAllowOriginHeader($event);
$resp = $event->getResponse();
Expand All @@ -256,9 +256,9 @@ public function testRequestWithForcedAllowOriginValue()
$dispatcher = m::mock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher->shouldReceive('addListener')->once()->with('kernel.response', m::type('callable'), -1);

$event = new GetResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$event = new RequestEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST);
$this->getListener($dispatcher, $options)->onKernelRequest($event);
$event = new FilterResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, new Response());
$event = new ResponseEvent(m::mock('Symfony\Component\HttpKernel\HttpKernelInterface'), $req, HttpKernelInterface::MASTER_REQUEST, new Response());
$this->getListener($dispatcher, $options)->forceAccessControlAllowOriginHeader($event);
$resp = $event->getResponse();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $resp);
Expand Down
66 changes: 30 additions & 36 deletions Tests/DependencyInjection/CorsConfigurationProviderPassTest.php
Expand Up @@ -9,51 +9,45 @@
*/
namespace Nelmio\Tests\DependencyInjection;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Fixtures\ProviderMock;
use Nelmio\CorsBundle\DependencyInjection\Compiler\CorsConfigurationProviderPass;
use Nelmio\CorsBundle\DependencyInjection\NelmioCorsExtension;
use Nelmio\CorsBundle\Options\ConfigProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Nelmio\CorsBundle\DependencyInjection\Compiler\CorsConfigurationProviderPass;
use Symfony\Component\DependencyInjection\Reference;

class CorsConfigurationProviderPassTest extends AbstractCompilerPassTestCase
class CorsConfigurationProviderPassTest extends TestCase
{
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new CorsConfigurationProviderPass());
}

public function testCollectProviders()
{
$configurationResolver = new Definition();
$this->setDefinition('nelmio_cors.options_resolver', $configurationResolver);
$container = $this->getContainerBuilder();
$container->compile();

$configurationProvider = new Definition();
$configurationProvider->addTag('nelmio_cors.options_provider');
$this->setDefinition('cors.options_provider.test1', $configurationProvider);
$arguments = $container->getDefinition('nelmio_cors.options_resolver')->getArguments();

$configurationProvider = new Definition();
$configurationProvider->addTag('nelmio_cors.options_provider', array('priority' => 10));
$this->setDefinition('cors.options_provider.test2', $configurationProvider);

$configurationProvider = new Definition();
$configurationProvider->addTag('nelmio_cors.options_provider', array('priority' => 5));
$this->setDefinition('cors.options_provider.test3', $configurationProvider);

$configurationProvider = new Definition();
$configurationProvider->addTag('nelmio_cors.options_provider', array('priority' => 5));
$this->setDefinition('cors.options_provider.test4', $configurationProvider);
static::assertCount(4, $arguments[0] ?? []);
static::assertSame(ConfigProvider::class, (string) $arguments[0][0]->getClass());
static::assertSame('cors.options_provider.test3', (string) $arguments[0][1]);
static::assertSame('cors.options_provider.test4', (string) $arguments[0][2]);
static::assertSame('cors.options_provider.test2', (string) $arguments[0][3]);
}

$this->compile();
protected function getContainerBuilder(): ContainerBuilder
{
$extension = new NelmioCorsExtension();
$container = new ContainerBuilder();
$optionProviders = [
'cors.options_provider.test1' => (new Definition(ProviderMock::class))->setPublic(true),
'cors.options_provider.test2' => (new Definition(ProviderMock::class))->setPublic(true)->addTag('nelmio_cors.options_provider', ['priority' => 10]),
'cors.options_provider.test3' => (new Definition(ProviderMock::class))->setPublic(true)->addTag('nelmio_cors.options_provider', ['priority' => 5]),
'cors.options_provider.test4' => (new Definition(ProviderMock::class))->setPublic(true)->addTag('nelmio_cors.options_provider', ['priority' => 5]),
];
$container->addDefinitions($optionProviders);
$container->addCompilerPass(new CorsConfigurationProviderPass());
$extension->load([], $container);
$container->getDefinition('nelmio_cors.options_resolver')->setPublic(true);

$this->assertContainerBuilderHasServiceDefinitionWithArgument(
'nelmio_cors.options_resolver',
0,
array(
new Reference('cors.options_provider.test1'),
new Reference('cors.options_provider.test3'),
new Reference('cors.options_provider.test4'),
new Reference('cors.options_provider.test2')
)
);
return $container;
}
}
15 changes: 15 additions & 0 deletions Tests/Fixtures/ProviderMock.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Fixtures;

use Nelmio\CorsBundle\Options\ProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class ProviderMock implements ProviderInterface
{
public function __construct() {}

public function getOptions(Request $request) {}
}
2 changes: 1 addition & 1 deletion Tests/Options/ResolverTest.php
Expand Up @@ -38,7 +38,7 @@ class ResolverTest extends TestCase
*/
protected $extraProviderValue;

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
11 changes: 5 additions & 6 deletions composer.json
@@ -1,6 +1,6 @@
{
"name": "nelmio/cors-bundle",
"description": "Adds CORS (Cross-Origin Resource Sharing) headers support in your Symfony2 application",
"description": "Adds CORS (Cross-Origin Resource Sharing) headers support in your Symfony application",
"keywords": ["cors", "crossdomain", "api"],
"type": "symfony-bundle",
"license": "MIT",
Expand All @@ -15,20 +15,19 @@
}
],
"require": {
"symfony/framework-bundle": "^2.7 || ^3.0 || ^4.0"
"symfony/framework-bundle": "^4.3 || ^5.0"
},
"require-dev": {
"matthiasnoback/symfony-dependency-injection-test": "^1.0 || ^2.0",
"mockery/mockery": "^0.9 || ^1.0",
"symfony/phpunit-bridge": "^2.7 || ^3.0 || ^4.0"
"mockery/mockery": "^1.2",
"symfony/phpunit-bridge": "^4.3 || ^5.0"
},
"autoload": {
"psr-4": { "Nelmio\\CorsBundle\\": "" },
"exclude-from-classmap": ["/Tests/"]
},
"extra": {
"branch-alias": {
"dev-master": "1.5.x-dev"
"dev-master": "2.0.x-dev"
}
}
}
8 changes: 7 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -8,9 +8,15 @@
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "Tests/bootstrap.php">

<php>
<ini name="error_reporting" value="-1" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0" />
<server name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
</php>

<testsuites>
<testsuite name="NelmioCorsBundle Test Suite">
<directory>Tests</directory>
Expand Down