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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RequestMatcherInterface to RouterDecorator #21

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "pgs-soft/hashid-bundle",
"name": "eductradesa/hashid-bundle",
"type": "symfony-bundle",
"description": "Symfony Hash Id",
"keywords": [],
Expand All @@ -11,12 +11,12 @@
}
],
"require": {
"php": "^7.2.5",
"symfony/dependency-injection": "^4.1.12|^5.0",
"symfony/config": "~4.4|~5.0",
"doctrine/annotations": "^1.6",
"hashids/hashids": ">=2.0",
"symfony/routing": "^5.0"
"php": "^8.0.0",
"symfony/dependency-injection": "^5.4|^6.0",
"symfony/config": "~5.0|~6.0",
"doctrine/annotations": "^1.11",
"hashids/hashids": "^4.1.0",
"symfony/routing": "^5.0|^6.0"
},
"autoload": {
"psr-4": { "Pgs\\HashIdBundle\\": "src/" }
Expand All @@ -34,12 +34,12 @@
},
"require-dev": {
"phpstan/phpstan": "^0.12",
"sebastian/phpcpd": "^4.0",
"sebastian/phpcpd": "^6.0",
"squizlabs/php_codesniffer": "^3.2",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/http-kernel": "^4.4.13|^5.1.5",
"sensio/framework-extra-bundle": "^5.1",
"friendsofphp/php-cs-fixer": "^2.10",
"phpunit/phpunit": "^7.0",
"phpunit/phpunit": "^9.5.16",
"symfony/browser-kit": "^4.0",
"symfony/yaml": "^4.0"
}
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 6
paths:
- src
- tests
11 changes: 10 additions & 1 deletion src/Decorator/RouterDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;

class RouterDecorator implements RouterInterface, WarmableInterface
class RouterDecorator implements RouterInterface, WarmableInterface, RequestMatcherInterface
{
use DecoratorTrait;

Expand Down Expand Up @@ -102,4 +103,12 @@ public function warmUp($cacheDir)
$this->getRouter()->warmUp($cacheDir);
}
}

/**
* @codeCoverageIgnore
*/
public function matchRequest($pathinfo)
{
return $this->getRouter()->matchRequest($pathinfo);
}
}
6 changes: 3 additions & 3 deletions tests/AnnotationProvider/AnnotationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Pgs\HashIdBundle\Exception\InvalidControllerException;
use Pgs\HashIdBundle\Reflection\ReflectionProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class AnnotationProviderTest extends TestCase
{
Expand All @@ -18,7 +18,7 @@ class AnnotationProviderTest extends TestCase
*/
protected $controllerAnnotationProvider;

protected function setUp()
protected function setUp(): void
{
$this->controllerAnnotationProvider = new AnnotationProvider(
$this->getReaderMock(),
Expand Down Expand Up @@ -90,7 +90,7 @@ protected function getReflectionProviderMock()

protected function getControllerMock()
{
$mock = $this->getMockBuilder(Controller::class)->setMethods(['demo'])->getMockForAbstractClass();
$mock = $this->getMockBuilder(AbstractController::class)->setMethods(['demo'])->getMockForAbstractClass();

return $mock;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/App/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
framework:
secret: secret
test: ~
router: { resource: "%kernel.root_dir%/../../src/Resources/routes.yaml" }
router: { resource: "%kernel.project_dir%/src/Resources/routes.yaml" }
28 changes: 28 additions & 0 deletions tests/Controller/ParamsAnnotatedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Pgs\HashIdBundle\Tests\Controller;

use Pgs\HashIdBundle\Annotation\Hash;
use Symfony\Component\Routing\Annotation\Route;

class ParamsAnnotatedController
{
/**
* @Route("/{enroll}", name="test_route")
* @Hash('toDecode')
*/
public function getArticlesAction($toDecode = null)
{
}

#[Route('/{toDecode}', name: 'test_route')]
#[Hash('toDecode')]
public function getArticlesAttributesAction($toDecode = null)
{
}

public function __invoke()
{

}
}
8 changes: 4 additions & 4 deletions tests/Decorator/RouterDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RouterDecoratorTest extends WebTestCase
*/
protected static $container;

protected function setUp()
protected function setUp(): void
{
$this::$container = static::createClient()->getContainer();
$this->router = self::$container->get('router');
Expand All @@ -35,13 +35,13 @@ public function testGenerateUrl(): void
$generatedPath = $this->router->generate(...$routeArgs);
$this->assertNotSame(sprintf('/hash-id/demo/decode/%d/%d', $id, $other), $generatedPath);
$pattern = sprintf('/\/hash-id\/demo\/decode\/[%s]{%d}\/\d+/', $alphabet, $hashLength);
$this->assertRegExp($pattern, $generatedPath);
$this->assertMatchesRegularExpression($pattern, $generatedPath);

$routeArgs = ['pgs_hash_id_demo_encode_localized', ['id' => $id, '_locale' => 'pl']];
$generatedPath = $this->router->generate(...$routeArgs);
$this->assertNotSame(sprintf('/hash-id/demo/encode-pl/%d', $id), $generatedPath);
$pattern = sprintf('/\/hash-id\/demo\/encode-pl\/[%s]{%d}/', $alphabet, $hashLength);
$this->assertRegExp($pattern, $generatedPath);
$this->assertMatchesRegularExpression($pattern, $generatedPath);

$routeArgs = ['pgs_hash_id_demo_decode_more', ['id' => $id, 'other' => $other]];
$generatedPath = $this->router->generate(...$routeArgs);
Expand All @@ -52,6 +52,6 @@ public function testGenerateUrl(): void
$alphabet,
$hashLength
);
$this->assertRegExp($pattern, $generatedPath);
$this->assertMatchesRegularExpression($pattern, $generatedPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EventSubscriberCompilerPassTest extends TestCase
*/
private $decodeControllerParametersDefinition;

protected function setUp()
protected function setUp(): void
{
$this->pass = new EventSubscriberCompilerPass();
$this->container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HashidsConverterCompilerPassTest extends TestCase
*/
private $container;

protected function setUp()
protected function setUp(): void
{
$this->compilerPass = new HashidsConverterCompilerPass();

Expand Down
16 changes: 7 additions & 9 deletions tests/EventSubscriber/AbstractEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

abstract class AbstractEventSubscriberTest extends TestCase
Expand All @@ -17,18 +18,15 @@ protected function subscribedEventsList(string $eventSubscriberClass): void
}

/**
* @return ControllerEvent|MockObject
* @return ControllerEvent
*/
protected function getEventMock(): ControllerEvent
protected function getEvent(): ControllerEvent
{
$mock = $this->getMockBuilder(ControllerEvent::class)
->disableOriginalConstructor()
->setMethods(['getRequest'])
->getMock();

$mock->method('getRequest')->willReturn($this->getRequestMock());
$kernel = $this->createMock(HttpKernelInterface::class);
$request = $this->getRequestMock();
$event = new ControllerEvent($kernel, fn() => (null), $request, HttpKernelInterface::MAIN_REQUEST);

return $mock;
return $event;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testSubscribedEvents(): void
public function testOnKernelController(): void
{
$subscriber = new DecodeControllerParametersSubscriber($this->getDecodeControllerParametersMock());
$event = $this->getEventMock();
$event = $this->getEvent();
$encodedParameters = $event->getRequest()->attributes->all();
$subscriber->onKernelController($event);
$this->assertNotSame($encodedParameters, $event->getRequest()->attributes->all());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class ParametersProcessorFactoryTest extends TestCase

protected $parametersProcessorMockProvider;

protected function setUp()
protected function setUp(): void
{
$this->controllerMockProvider = new ControllerMockProvider();
$this->controllerAnnotationMockProvider = new ControllerAnnotationProviderMockProvider();
Expand Down
2 changes: 1 addition & 1 deletion tests/ParametersProcessor/NoOpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NoOpTest extends TestCase
*/
protected $parametersProcessor;

protected function setUp()
protected function setUp(): void
{
$this->parametersProcessor = new NoOp($this->getHashidMock(), []);
}
Expand Down
27 changes: 12 additions & 15 deletions tests/Service/DecodeControllerParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
use Pgs\HashIdBundle\ParametersProcessor\Factory\DecodeParametersProcessorFactory;
use Pgs\HashIdBundle\Service\DecodeControllerParameters;
use Pgs\HashIdBundle\Tests\Controller\ControllerMockProvider;
use Pgs\HashIdBundle\Tests\Controller\ParamsAnnotatedController;
use Pgs\HashIdBundle\Tests\ParametersProcessor\ParametersProcessorMockProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class DecodeControllerParametersTest extends TestCase
{
protected $controllerMockProvider;

protected $parametersProcessorMockProvider;

protected function setUp()
protected function setUp(): void
{
$this->parametersProcessorMockProvider = new ParametersProcessorMockProvider();
}
Expand All @@ -44,7 +46,7 @@ public function testDecodeControllerParameters($controller): void
{
$decodeParametersProcessorFactory = $this->getDecodeParametersProcessorFactoryMock();
$decodeControllerParameters = new DecodeControllerParameters($decodeParametersProcessorFactory);
$event = $this->getEventMock(
$event = $this->getEvent(
[
[
'id' => 'encoded',
Expand All @@ -63,8 +65,9 @@ public function testDecodeControllerParameters($controller): void

public function decodeControllerParametersDataProvider()
{
$controller = new ParamsAnnotatedController();
return [
['controller as array' => $this->getControllerMockProvider()->getTestControllerMock(), 'demo'],
['controller' => $controller, 'demo'],
];
}

Expand All @@ -78,7 +81,7 @@ public function testDecodeControllerParametersWithParamConverter($controller): v
$decodeParametersProcessorFactory = $this->getDecodeParametersProcessorFactoryMock();
$decodeControllerParameters = new DecodeControllerParameters($decodeParametersProcessorFactory);
$decodeControllerParameters->setParamConverterListener($this->getDoctrineParamConverterListenerMock());
$event = $this->getEventMock(
$event = $this->getEvent(
[
[
'id' => 'encoded',
Expand Down Expand Up @@ -122,20 +125,14 @@ protected function getDecodeParametersProcessorMock(): Decode
/**
* @return ControllerEvent|MockObject
*/
protected function getEventMock(array $requestConsecutiveCalls, $controller): ControllerEvent
protected function getEvent(array $requestConsecutiveCalls, $controller): ControllerEvent
{
$mock = $this->getMockBuilder(ControllerEvent::class)
->disableOriginalConstructor()
->setMethods(['getController', 'getRequest'])
->getMock();
$kernel = $this->createMock(HttpKernelInterface::class);
$request = $this->getRequestMock($requestConsecutiveCalls);

$mock->method('getController')
->willReturn($controller);
$event = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST);

$mock->method('getRequest')
->willReturn($this->getRequestMock($requestConsecutiveCalls));

return $mock;
return $event;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Traits/DecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DecoratorTraitTest extends TestCase
{
protected $decorateClass;

protected function setUp()
protected function setUp(): void
{
$baseClass = new BaseTestClass();
$this->decorateClass = new DecorateTestClass($baseClass);
Expand Down