diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 1f4044a..b260cd7 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -1,4 +1,4 @@ -name: Build and test +name: Build on: push: diff --git a/README.md b/README.md index 47482b4..d58880c 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # LmcCors -[![Build Status](https://travis-ci.com/LM-Commons/lmccors.svg?branch=master)](https://travis-ci.com/LM-Commons/lmccors) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/LM-Commons/LmcCors/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/LM-Commons/LmcCors/?branch=master) +![Build Status](https://github.com/lm-commons/lmccors/actions/workflows/build-test.yml/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/LM-Commons/LmcCors/badge.svg?branch=master)](https://coveralls.io/github/LM-Commons/LmcCors?branch=master) [![Latest Stable Version](https://poser.pugx.org/lm-commons/lmc-cors/v)](//packagist.org/packages/lm-commons/lmc-cors) [![License](https://poser.pugx.org/lm-commons/lmc-cors/license)](//packagist.org/packages/lm-commons/lmc-cors) @@ -20,10 +19,11 @@ builds HTTP responses that follow the CORS documentation. Install the module by typing (or add it to your `composer.json` file): ```sh -$ php composer.phar require lm-commons/lmc-cors +$ composer require lm-commons/lmc-cors ``` Then, enable it by adding "LmcCors" in your `application.config.php` or `modules.config.php` file. +Alternatively, the module will be added to the configuration during installation by the Laminas Component Installer By default, LmcCors is configured to deny every CORS requests. To change that, you need to copy the [`config/lmc_cors.global.php.dist`](config/lmc_cors.global.php.dist) file to your `autoload` folder @@ -36,7 +36,7 @@ the [`config/lmc_cors.global.php.dist`](config/lmc_cors.global.php.dist) file to CORS is a mechanism that allows to perform cross-origin requests from your browser. For instance, let's say that your website is hosted in the domain `http://example.com`. -By default, user agents won't be allowed to perform AJAX requests to another domain for security +By default, user agents will not be allowed to perform AJAX requests to another domain for security reasons (for instance `http://funny-domain.com`). With CORS, you can allow your server to reply to such requests. @@ -49,7 +49,7 @@ You can find better documentation on how CORS works on the web: ### Event registration LmcCors registers the `LmcCors\Mvc\CorsRequestListener` with the `MvcEvent::EVENT_ROUTE` event, with a priority -of -1. This means that this listener is executed AFTER the route has been matched. +of 2. This means that this listener is executed BEFORE the route has been matched. ### Configuring the module @@ -70,7 +70,7 @@ As by default, all the various options are set globally for all routes: some browsers do not implement this feature correctly. - `allowed_credentials`: (boolean) If true, it allows the browser to send cookies along with the request. -If you want to configure specific routes, you can add `ZfrCors\Options\CorsOptions::ROUTE_PARAM` to your route configuration: +If you want to configure specific routes, you can add `LmcCors\Options\CorsOptions::ROUTE_PARAM` to your route configuration: ```php */ -class CorsOptionsFactory +class CorsOptionsFactory implements FactoryInterface { /** * @param ContainerInterface $container + * @param $requestedName + * @param array|null $options * @return CorsOptions + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): CorsOptions { /* @var $config array */ $config = $container->has('config') ? $container->get('config') : []; - $config = isset($config['lmc_cors']) ? $config['lmc_cors'] : []; + $config = $config['lmc_cors'] ?? []; return new CorsOptions($config); } diff --git a/src/Factory/CorsRequestListenerFactory.php b/src/Factory/CorsRequestListenerFactory.php index b8f6e8d..163938c 100644 --- a/src/Factory/CorsRequestListenerFactory.php +++ b/src/Factory/CorsRequestListenerFactory.php @@ -18,9 +18,12 @@ namespace LmcCors\Factory; -use Interop\Container\ContainerInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; use LmcCors\Mvc\CorsRequestListener; use LmcCors\Service\CorsService; +use Psr\Container\NotFoundExceptionInterface; /** * CorsRequestListenerFactory @@ -28,17 +31,20 @@ * @license MIT * @author Florent Blaison */ -class CorsRequestListenerFactory +class CorsRequestListenerFactory implements FactoryInterface { /** - * {@inheritDoc} - * + * @param ContainerInterface $container + * @param $requestedName + * @param $options * @return CorsRequestListener + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, $options = null) + public function __invoke(ContainerInterface $container, $requestedName, $options = null): CorsRequestListener { /* @var $corsService CorsService */ - $corsService = $container->get('LmcCors\Service\CorsService'); + $corsService = $container->get(CorsService::class); return new CorsRequestListener($corsService); } diff --git a/src/Factory/CorsServiceFactory.php b/src/Factory/CorsServiceFactory.php index e1edd80..9485865 100644 --- a/src/Factory/CorsServiceFactory.php +++ b/src/Factory/CorsServiceFactory.php @@ -19,8 +19,11 @@ namespace LmcCors\Factory; use Interop\Container\ContainerInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; use LmcCors\Options\CorsOptions; use LmcCors\Service\CorsService; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * CorsServiceFactory @@ -28,17 +31,20 @@ * @license MIT * @author Florent Blaison */ -class CorsServiceFactory +class CorsServiceFactory implements FactoryInterface { /** - * {@inheritDoc} - * + * @param ContainerInterface $container + * @param $requestedName + * @param $options * @return CorsService + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function __invoke(ContainerInterface $container, $requestedName, $options = null) + public function __invoke(ContainerInterface $container, $requestedName, $options = null): CorsService { /* @var $corsOptions CorsOptions */ - $corsOptions = $container->get('LmcCors\Options\CorsOptions'); + $corsOptions = $container->get(CorsOptions::class); return new CorsService($corsOptions); } diff --git a/src/Module.php b/src/Module.php index a524436..5399e38 100644 --- a/src/Module.php +++ b/src/Module.php @@ -21,6 +21,8 @@ use Laminas\EventManager\EventInterface; use Laminas\ModuleManager\Feature\BootstrapListenerInterface; use Laminas\ModuleManager\Feature\ConfigProviderInterface; +use Laminas\Mvc\Application; +use LmcCors\Mvc\CorsRequestListener; /** * @licence MIT @@ -32,14 +34,14 @@ class Module implements BootstrapListenerInterface, ConfigProviderInterface /** * {@inheritDoc} */ - public function onBootstrap(EventInterface $event) + public function onBootstrap(EventInterface $e): void { - /* @var $application \Laminas\Mvc\Application */ - $application = $event->getTarget(); + /* @var $application Application */ + $application = $e->getTarget(); $serviceManager = $application->getServiceManager(); $eventManager = $application->getEventManager(); - /** @var \LmcCors\Mvc\CorsRequestListener $listener */ + /** @var CorsRequestListener $listener */ $listener = $serviceManager->get('LmcCors\Mvc\CorsRequestListener'); $listener->attach($eventManager); } diff --git a/src/Mvc/CorsRequestListener.php b/src/Mvc/CorsRequestListener.php index d4a549d..8d84bc7 100644 --- a/src/Mvc/CorsRequestListener.php +++ b/src/Mvc/CorsRequestListener.php @@ -38,14 +38,14 @@ class CorsRequestListener extends AbstractListenerAggregate /** * @var CorsService */ - protected $corsService; + protected CorsService $corsService; /** * Whether or not a preflight request was detected * * @var bool */ - protected $isPreflight = false; + protected bool $isPreflight = false; /** * @param CorsService $corsService @@ -58,7 +58,7 @@ public function __construct(CorsService $corsService) /** * {@inheritDoc} */ - public function attach(EventManagerInterface $events, $priority = 1) + public function attach(EventManagerInterface $events, $priority = 1): void { // Preflight can be handled during the route event, and should return early $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'onCorsPreflight'], 2); @@ -106,7 +106,7 @@ public function onCorsPreflight(MvcEvent $event) $router = $event->getRouter(); $requestForMatching = clone $request; - // Use the request method for route deteciton, which is being used during the request. + // Use the request method for route detection, which is being used during the request. $requestForMatching->setMethod($request->getHeader('Access-Control-Request-Method')->getFieldValue()); $routeMatch = $router->match($requestForMatching); @@ -119,7 +119,7 @@ public function onCorsPreflight(MvcEvent $event) * * @param MvcEvent $event */ - public function onCorsRequest(MvcEvent $event) + public function onCorsRequest(MvcEvent $event): void { // Do nothing if we previously created a preflight response if ($this->isPreflight) { diff --git a/src/Options/CorsOptions.php b/src/Options/CorsOptions.php index 77ac7b5..f3a508d 100644 --- a/src/Options/CorsOptions.php +++ b/src/Options/CorsOptions.php @@ -35,48 +35,48 @@ class CorsOptions extends AbstractOptions * * @var array */ - protected $allowedOrigins = []; + protected array $allowedOrigins = []; /** * Set the list of HTTP verbs. * * @var array */ - protected $allowedMethods = []; + protected array $allowedMethods = []; /** * Set the list of headers. * * @var array */ - protected $allowedHeaders = []; + protected array $allowedHeaders = []; /** * Set the max age of the authorize request in seconds. * * @var int */ - protected $maxAge = 0; + protected int $maxAge = 0; /** * Set the list of exposed headers. * * @var array */ - protected $exposedHeaders = []; + protected array $exposedHeaders = []; /** * Allow CORS request with credential. * * @var bool */ - protected $allowedCredentials = false; + protected bool $allowedCredentials = false; /** * @param array $allowedOrigins * @return void */ - public function setAllowedOrigins(array $allowedOrigins) + public function setAllowedOrigins(array $allowedOrigins): void { $this->allowedOrigins = $allowedOrigins; } @@ -84,7 +84,7 @@ public function setAllowedOrigins(array $allowedOrigins) /** * @return array */ - public function getAllowedOrigins() + public function getAllowedOrigins(): array { return $this->allowedOrigins; } @@ -93,7 +93,7 @@ public function getAllowedOrigins() * @param array $allowedMethods * @return void */ - public function setAllowedMethods(array $allowedMethods) + public function setAllowedMethods(array $allowedMethods): void { foreach ($allowedMethods as &$allowedMethod) { $allowedMethod = strtoupper($allowedMethod); @@ -105,7 +105,7 @@ public function setAllowedMethods(array $allowedMethods) /** * @return array */ - public function getAllowedMethods() + public function getAllowedMethods(): array { return $this->allowedMethods; } @@ -114,7 +114,7 @@ public function getAllowedMethods() * @param array $allowedHeaders * @return void */ - public function setAllowedHeaders(array $allowedHeaders) + public function setAllowedHeaders(array $allowedHeaders): void { $this->allowedHeaders = $allowedHeaders; } @@ -122,16 +122,16 @@ public function setAllowedHeaders(array $allowedHeaders) /** * @return array */ - public function getAllowedHeaders() + public function getAllowedHeaders(): array { return $this->allowedHeaders; } /** - * @param int $maxAge + * @param int $maxAge * @return void */ - public function setMaxAge($maxAge) + public function setMaxAge(int $maxAge): void { $this->maxAge = (int) $maxAge; } @@ -139,7 +139,7 @@ public function setMaxAge($maxAge) /** * @return int */ - public function getMaxAge() + public function getMaxAge(): int { return $this->maxAge; } @@ -148,7 +148,7 @@ public function getMaxAge() * @param array $exposedHeaders * @return void */ - public function setExposedHeaders(array $exposedHeaders) + public function setExposedHeaders(array $exposedHeaders): void { $this->exposedHeaders = $exposedHeaders; } @@ -156,16 +156,16 @@ public function setExposedHeaders(array $exposedHeaders) /** * @return array */ - public function getExposedHeaders() + public function getExposedHeaders(): array { return $this->exposedHeaders; } /** - * @param bool $allowedCredentials + * @param bool $allowedCredentials * @return void */ - public function setAllowedCredentials($allowedCredentials) + public function setAllowedCredentials(bool $allowedCredentials): void { $this->allowedCredentials = (bool) $allowedCredentials; } @@ -173,7 +173,7 @@ public function setAllowedCredentials($allowedCredentials) /** * @return boolean */ - public function getAllowedCredentials() + public function getAllowedCredentials(): bool { return $this->allowedCredentials; } diff --git a/src/Service/CorsService.php b/src/Service/CorsService.php index a271563..26f0ef3 100644 --- a/src/Service/CorsService.php +++ b/src/Service/CorsService.php @@ -18,7 +18,8 @@ namespace LmcCors\Service; -use Laminas\Mvc\Router\Http\RouteMatch as DeprecatedRouteMatch; +//use Laminas\Mvc\Router\Http\RouteMatch as DeprecatedRouteMatch; +use Laminas\Http\Headers; use Laminas\Router\Http\RouteMatch; use Laminas\Http\Header; use Laminas\Uri\UriFactory; @@ -41,7 +42,7 @@ class CorsService /** * @var CorsOptions */ - protected $options; + protected CorsOptions $options; /** * @param CorsOptions $options @@ -58,7 +59,7 @@ public function __construct(CorsOptions $options) * @param HttpRequest $request * @return bool */ - public function isCorsRequest(HttpRequest $request) + public function isCorsRequest(HttpRequest $request): bool { $headers = $request->getHeaders(); @@ -89,7 +90,7 @@ public function isCorsRequest(HttpRequest $request) * @param HttpRequest $request * @return bool */ - public function isPreflightRequest(HttpRequest $request) + public function isPreflightRequest(HttpRequest $request): bool { return $this->isCorsRequest($request) && strtoupper($request->getMethod()) === 'OPTIONS' @@ -102,7 +103,7 @@ public function isPreflightRequest(HttpRequest $request) * @param HttpRequest $request * @return HttpResponse */ - public function createPreflightCorsResponse(HttpRequest $request) + public function createPreflightCorsResponse(HttpRequest $request): HttpResponse { $response = new HttpResponse(); $response->setStatusCode(200); @@ -123,22 +124,20 @@ public function createPreflightCorsResponse(HttpRequest $request) } /** - * Create a preflight response by adding the correspoding headers which are merged with per-route configuration + * Create a preflight response by adding the corresponding headers which are merged with per-route configuration * * @param HttpRequest $request - * @param RouteMatch|DeprecatedRouteMatch|null $routeMatch + * @param RouteMatch|null $routeMatch * * @return HttpResponse */ - public function createPreflightCorsResponseWithRouteOptions(HttpRequest $request, $routeMatch = null) + public function createPreflightCorsResponseWithRouteOptions(HttpRequest $request, RouteMatch $routeMatch = null): HttpResponse { $options = $this->options; - if ($routeMatch instanceof RouteMatch || $routeMatch instanceof DeprecatedRouteMatch) { + if ($routeMatch instanceof RouteMatch) { $options->setFromArray($routeMatch->getParam(CorsOptions::ROUTE_PARAM) ?: []); } - $response = $this->createPreflightCorsResponse($request); - - return $response; + return $this->createPreflightCorsResponse($request); } /** @@ -146,13 +145,13 @@ public function createPreflightCorsResponseWithRouteOptions(HttpRequest $request * * @param HttpRequest $request * @param HttpResponse $response - * @param null|RouteMatch $routeMatch + * @param RouteMatch|null $routeMatch * @return HttpResponse * @throws DisallowedOriginException If the origin is not allowed */ - public function populateCorsResponse(HttpRequest $request, HttpResponse $response, $routeMatch = null) + public function populateCorsResponse(HttpRequest $request, HttpResponse $response, RouteMatch $routeMatch = null): HttpResponse { - if ($routeMatch instanceof RouteMatch || $routeMatch instanceof DeprecatedRouteMatch) { + if ($routeMatch instanceof RouteMatch) { $this->options->setFromArray($routeMatch->getParam(CorsOptions::ROUTE_PARAM) ?: []); } @@ -197,7 +196,7 @@ public function populateCorsResponse(HttpRequest $request, HttpResponse $respons * @param HttpRequest $request * @return string */ - protected function getAllowedOriginValue(HttpRequest $request) + protected function getAllowedOriginValue(HttpRequest $request): string { $allowedOrigins = $this->options->getAllowedOrigins(); @@ -224,9 +223,9 @@ protected function getAllowedOriginValue(HttpRequest $request) * * @link http://www.w3.org/TR/cors/#resource-implementation * @param HttpResponse $response - * @return \Laminas\Http\Headers + * @return Headers */ - public function ensureVaryHeader(HttpResponse $response) + public function ensureVaryHeader(HttpResponse $response): Headers { $headers = $response->getHeaders(); // If the origin is not "*", we should add the "Origin" value to the "Vary" header diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 9ace177..95d2c5b 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -51,8 +51,3 @@ ServiceManagerFactory::setApplicationConfig($config); unset($files, $file, $loader, $configFiles, $configFile, $config); - - -if (! class_exists(TreeRouteStack::class)) { - class_alias(\Laminas\Mvc\Router\Http\TreeRouteStack::class, TreeRouteStack::class); -} diff --git a/tests/Factory/CorsOptionsFactoryTest.php b/tests/Factory/CorsOptionsFactoryTest.php index 1b89d09..3f0e961 100644 --- a/tests/Factory/CorsOptionsFactoryTest.php +++ b/tests/Factory/CorsOptionsFactoryTest.php @@ -18,8 +18,11 @@ namespace LmcCorsTest\Factory; -use PHPUnit\Framework\TestCase as TestCase; +use LmcCors\Options\CorsOptions; +use PHPUnit\Framework\TestCase; use LmcCorsTest\Util\ServiceManagerFactory; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Integration tests for {@see \LmcCors\Service\CorsService} @@ -31,11 +34,16 @@ */ class CorsOptionsFactoryTest extends TestCase { - public function testCanCreateOptions() + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function testCanCreateOptions(): void { $serviceManager = ServiceManagerFactory::getServiceManager(); - $options = $serviceManager->get('LmcCors\Options\CorsOptions'); + $options = $serviceManager->get(CorsOptions::class); - $this->assertInstanceOf('LmcCors\Options\CorsOptions', $options); + $this->assertInstanceOf(CorsOptions::class, $options); } } diff --git a/tests/Factory/CorsRequestListenerFactoryTest.php b/tests/Factory/CorsRequestListenerFactoryTest.php index f1ed473..9439a6d 100644 --- a/tests/Factory/CorsRequestListenerFactoryTest.php +++ b/tests/Factory/CorsRequestListenerFactoryTest.php @@ -18,7 +18,8 @@ namespace LmcCorsTest\Factory; -use PHPUnit\Framework\TestCase as TestCase; +use LmcCors\Mvc\CorsRequestListener; +use PHPUnit\Framework\TestCase; use LmcCorsTest\Util\ServiceManagerFactory; /** @@ -34,8 +35,8 @@ class CorsRequestListenerFactoryTest extends TestCase public function testCanCreateCorsRequestListener() { $serviceManager = ServiceManagerFactory::getServiceManager(); - $listener = $serviceManager->get('LmcCors\Mvc\CorsRequestListener'); + $listener = $serviceManager->get(CorsRequestListener::class); - $this->assertInstanceOf('LmcCors\Mvc\CorsRequestListener', $listener); + $this->assertInstanceOf(CorsRequestListener::class, $listener); } } diff --git a/tests/Factory/CorsServiceFactoryTest.php b/tests/Factory/CorsServiceFactoryTest.php index ea6629f..c2a9223 100644 --- a/tests/Factory/CorsServiceFactoryTest.php +++ b/tests/Factory/CorsServiceFactoryTest.php @@ -18,6 +18,7 @@ namespace LmcCorsTest\Factory; +use LmcCors\Service\CorsService; use PHPUnit\Framework\TestCase as TestCase; use LmcCorsTest\Util\ServiceManagerFactory; @@ -34,8 +35,8 @@ class CorsServiceFactoryTest extends TestCase public function testCanCreateCorsService() { $serviceManager = ServiceManagerFactory::getServiceManager(); - $service = $serviceManager->get('LmcCors\Service\CorsService'); + $service = $serviceManager->get(CorsService::class); - $this->assertInstanceOf('LmcCors\Service\CorsService', $service); + $this->assertInstanceOf(CorsService::class, $service); } } diff --git a/tests/Mvc/CorsRequestListenerTest.php b/tests/Mvc/CorsRequestListenerTest.php index e4b8fd1..27e375b 100644 --- a/tests/Mvc/CorsRequestListenerTest.php +++ b/tests/Mvc/CorsRequestListenerTest.php @@ -18,7 +18,7 @@ namespace LmcCorsTest\Mvc; -use PHPUnit\Framework\TestCase as TestCase; +use PHPUnit\Framework\TestCase; use Laminas\EventManager\EventManager; use Laminas\Http\Request as HttpRequest; use Laminas\Http\Response as HttpResponse; @@ -43,17 +43,17 @@ class CorsRequestListenerTest extends TestCase /** * @var CorsService */ - protected $corsService; + protected CorsService $corsService; /** * @var CorsOptions */ - protected $corsOptions; + protected CorsOptions $corsOptions; /** * @var CorsRequestListener */ - protected $corsListener; + protected CorsRequestListener $corsListener; public function setUp(): void { @@ -66,13 +66,16 @@ public function testAttach() { $eventManager = $this->getMockBuilder('Laminas\EventManager\EventManagerInterface')->getMock(); + $matcher = $this->exactly(2); $eventManager - ->expects($this->exactly(2)) + ->expects($matcher) ->method('attach') - ->withConsecutive( - [MvcEvent::EVENT_ROUTE, $this->isType('callable'), $this->equalTo(2)], - [MvcEvent::EVENT_FINISH, $this->isType('callable'), $this->greaterThan(1)], - ); + ->willReturnCallback(function (string $event, callable $callback, int $priority) use ($matcher) { + match ($matcher->getInvocationCount()) { + 1 => $this->assertEquals(MvcEvent::EVENT_ROUTE, $event), + 2 => $this->assertEquals(MvcEvent::EVENT_FINISH, $event), + }; + }); $this->corsListener->attach($eventManager); } diff --git a/tests/Options/CorsOptionsTest.php b/tests/Options/CorsOptionsTest.php index 5970935..9b7f642 100644 --- a/tests/Options/CorsOptionsTest.php +++ b/tests/Options/CorsOptionsTest.php @@ -18,7 +18,7 @@ namespace LmcCorsTest\Options; -use PHPUnit\Framework\TestCase as TestCase; +use PHPUnit\Framework\TestCase; use LmcCors\Options\CorsOptions; /** diff --git a/tests/Service/CorsServiceTest.php b/tests/Service/CorsServiceTest.php index 4dc7597..a8deb31 100644 --- a/tests/Service/CorsServiceTest.php +++ b/tests/Service/CorsServiceTest.php @@ -20,11 +20,10 @@ use LmcCors\Exception\DisallowedOriginException; use LmcCors\Exception\InvalidOriginException; -use PHPUnit\Framework\TestCase as TestCase; +use PHPUnit\Framework\TestCase; use Laminas\Http\Response as HttpResponse; use Laminas\Http\Request as HttpRequest; use Laminas\Mvc\MvcEvent; -use Laminas\Mvc\Router\Http\RouteMatch as DeprecatedRouteMatch; use Laminas\Router\Http\RouteMatch; use LmcCors\Options\CorsOptions; use LmcCors\Service\CorsService; @@ -42,27 +41,27 @@ class CorsServiceTest extends TestCase /** * @var CorsService */ - protected $corsService; + protected CorsService $corsService; /** * @var HttpResponse */ - protected $response; + protected HttpResponse $response; /** * @var HttpRequest */ - protected $request; + protected HttpRequest $request; /** * @var MvcEvent */ - protected $event; + protected MvcEvent $event; /** * @var CorsOptions */ - protected $corsOptions; + protected CorsOptions $corsOptions; /** * Set up @@ -92,7 +91,7 @@ public function testCanDetectCorsRequest() $this->assertFalse($this->corsService->isCorsRequest($request)); $request->getHeaders()->addHeaderLine('Origin', 'http://example.com'); - $this->assertEquals(true, $this->corsService->isCorsRequest($request)); + $this->assertTrue($this->corsService->isCorsRequest($request)); } public function testIsNotCorsRequestIfNotACrossRequest() @@ -101,7 +100,7 @@ public function testIsNotCorsRequestIfNotACrossRequest() $request->setUri('http://example.com'); $request->getHeaders()->addHeaderLine('Origin', 'http://example.com'); - $this->assertEquals(false, $this->corsService->isCorsRequest($request)); + $this->assertFalse($this->corsService->isCorsRequest($request)); } public function testCanDetectPreflightRequest() @@ -357,7 +356,7 @@ public function testCanDetectCorsRequestFromSameHostButDifferentScheme() public function testCanHandleUnconfiguredRouteMatch() { - $routeMatch = class_exists(DeprecatedRouteMatch::class) ? new DeprecatedRouteMatch([]) : new RouteMatch([]); + $routeMatch = new RouteMatch([]); $request = new HttpRequest(); $request->getHeaders()->addHeaderLine('Origin', 'http://example.com'); @@ -393,8 +392,7 @@ public function testCanHandleConfiguredRouteMatch() ], ]; - $routeMatch = class_exists(DeprecatedRouteMatch::class) ? new DeprecatedRouteMatch($routeMatchParameters) : - new RouteMatch($routeMatchParameters); + $routeMatch = new RouteMatch($routeMatchParameters); $request = new HttpRequest(); $request->getHeaders()->addHeaderLine('Origin', 'http://example.org'); @@ -443,8 +441,7 @@ public function testCanPopulateNormalCorsRequestWithRouteMatch() ], ]; - $routeMatch = class_exists(DeprecatedRouteMatch::class) ? new DeprecatedRouteMatch($routeMatchParameters) : - new RouteMatch($routeMatchParameters); + $routeMatch = new RouteMatch($routeMatchParameters); $request->getHeaders()->addHeaderLine('Origin', 'http://example.org'); @@ -469,8 +466,7 @@ public function testCanPopulateNormalCorsRequestWithRouteMatchRewriteException() ], ]; - $routeMatch = class_exists(DeprecatedRouteMatch::class) ? new DeprecatedRouteMatch($routeMatchParameters) : - new RouteMatch($routeMatchParameters); + $routeMatch = new RouteMatch($routeMatchParameters); $request->getHeaders()->addHeaderLine('Origin', 'http://example.com'); diff --git a/tests/Util/ServiceManagerFactory.php b/tests/Util/ServiceManagerFactory.php index 96026f3..a5f9146 100644 --- a/tests/Util/ServiceManagerFactory.php +++ b/tests/Util/ServiceManagerFactory.php @@ -19,8 +19,11 @@ namespace LmcCorsTest\Util; +use Laminas\ModuleManager\ModuleManagerInterface; use Laminas\ServiceManager\ServiceManager; use Laminas\Mvc\Service\ServiceManagerConfig; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Base test case to be used when a new service manager instance is required @@ -35,13 +38,14 @@ abstract class ServiceManagerFactory /** * @var array */ - private static $config = []; + private static array $config = []; /** * @static * @param array $config + * @return void */ - public static function setApplicationConfig(array $config) + public static function setApplicationConfig(array $config): void { static::$config = $config; } @@ -50,27 +54,29 @@ public static function setApplicationConfig(array $config) * @static * @return array */ - public static function getApplicationConfig() + public static function getApplicationConfig(): array { return static::$config; } /** - * @param array|null $config + * @param array|null $config * @return ServiceManager + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public static function getServiceManager(array $config = null) + public static function getServiceManager(array $config = null): ServiceManager { $config = $config ?: static::getApplicationConfig(); $serviceManager = new ServiceManager(); $serviceManagerConfig = new ServiceManagerConfig( - isset($config['service_manager']) ? $config['service_manager'] : [] + $config['service_manager'] ?? [] ); $serviceManagerConfig->configureServiceManager($serviceManager); $serviceManager->setService('ApplicationConfig', $config); - /* @var $moduleManager \Laminas\ModuleManager\ModuleManagerInterface */ + /* @var $moduleManager ModuleManagerInterface */ $moduleManager = $serviceManager->get('ModuleManager'); $moduleManager->loadModules();