diff --git a/VERSION b/VERSION index 1d0ba9e..8f0916f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.0 +0.5.0 diff --git a/composer.json b/composer.json index 2b7984d..f09093d 100644 --- a/composer.json +++ b/composer.json @@ -15,24 +15,24 @@ "ext-intl": "*", "doctrine/annotations": "^2.0", "meritoo/common-library": "^1.3", - "symfony/form": "^5.4", - "symfony/framework-bundle": "^5.4", - "symfony/twig-bundle": "^5.4", - "symfony/validator": "^5.4" + "symfony/form": "^7.2", + "symfony/framework-bundle": "^7.2", + "symfony/twig-bundle": "^7.2", + "symfony/validator": "^7.2" }, "require-dev": { "friends-of-phpspec/phpspec-code-coverage": "^6.1", "friendsofphp/php-cs-fixer": "^3.8", - "infection/infection": "^0.26.6", + "infection/infection": "^0.27.11", "php-coveralls/php-coveralls": "^2.5", "phpspec/phpspec": "^7.2", "phpstan/phpstan": "^1.6", "phpunit/phpunit": "^9.5", "sebastian/phpcpd": "^6.0", "squizlabs/php_codesniffer": "^3.6", - "symfony/phpunit-bridge": "^6.0", - "symfony/translation": "^6.0", - "vimeo/psalm": "^4.22" + "symfony/phpunit-bridge": "^7.2", + "symfony/translation": "^7.2", + "vimeo/psalm": "^5.26" }, "autoload": { "psr-4": { diff --git a/src/Service/RequestService.php b/src/Service/RequestService.php index b4c5910..2a4593d 100644 --- a/src/Service/RequestService.php +++ b/src/Service/RequestService.php @@ -15,7 +15,6 @@ use Meritoo\CommonBundle\Service\Base\BaseService; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Serves request @@ -28,13 +27,6 @@ class RequestService extends BaseService implements RequestServiceInterface private const ROUTE_PARAMETER = '_route'; private const PARAMETERS_PARAMETER = '_route_params'; - /** - * The session - * - * @var SessionInterface - */ - private SessionInterface $session; - /** * Request stack * @@ -52,12 +44,10 @@ class RequestService extends BaseService implements RequestServiceInterface /** * Class constructor * - * @param SessionInterface $session The session - * @param RequestStack $requestStack + * @param RequestStack $requestStack */ - public function __construct(SessionInterface $session, RequestStack $requestStack) + public function __construct(RequestStack $requestStack) { - $this->session = $session; $this->requestStack = $requestStack; } @@ -68,17 +58,21 @@ public function __construct(SessionInterface $session, RequestStack $requestStac */ public function fetchRefererUrl(): string { - $url = $this->session->get($this->refererUrlKey, ''); - $this->session->remove($this->refererUrlKey); + $url = $this + ->requestStack + ->getSession() + ->get($this->refererUrlKey, '') + ; + + $this + ->requestStack + ->getSession() + ->remove($this->refererUrlKey) + ; return $url; } - public function getCurrentRoute(): string - { - return $this->getParameter(self::ROUTE_PARAMETER) ?? ''; - } - public function getCurrentRouteParameters(): array { return $this->getParameter(self::PARAMETERS_PARAMETER) ?? []; @@ -89,39 +83,21 @@ public function getParameter(string $parameter) return $this->getCurrentRequest()->get($parameter); } - /** - * Returns url of referer - * - * @param Request $request The request (that probably contains referer) - * @return string - */ - public function getRefererUrl(Request $request): string - { - return $request->headers->get('referer', ''); - } - public function isCurrentRoute(string $route): bool { return $this->getCurrentRoute() === $route; } - /** - * Stores url of referer in session - * - * @param string $url Url of referer to store - * @return RequestServiceInterface - */ - public function storeRefererUrl(string $url): RequestServiceInterface + public function getCurrentRoute(): string { - $this->session->set($this->refererUrlKey, $url); - - return $this; + return $this->getParameter(self::ROUTE_PARAMETER) ?? ''; } /** * Stores the referer url in session grabbed from given request * * @param Request $request The request (that probably contains referer) + * * @return RequestServiceInterface */ public function storeRefererUrlFromRequest(Request $request): RequestServiceInterface @@ -139,6 +115,36 @@ public function storeRefererUrlFromRequest(Request $request): RequestServiceInte return $this->storeRefererUrl($url); } + /** + * Returns url of referer + * + * @param Request $request The request (that probably contains referer) + * + * @return string + */ + public function getRefererUrl(Request $request): string + { + return $request->headers->get('referer', ''); + } + + /** + * Stores url of referer in session + * + * @param string $url Url of referer to store + * + * @return RequestServiceInterface + */ + public function storeRefererUrl(string $url): RequestServiceInterface + { + $this + ->requestStack + ->getSession() + ->set($this->refererUrlKey, $url) + ; + + return $this; + } + private function getCurrentRequest(): Request { $request = $this->requestStack->getCurrentRequest(); diff --git a/tests/Controller/Base/BaseControllerTest.php b/tests/Controller/Base/BaseControllerTest.php index b1a66fb..7eae126 100644 --- a/tests/Controller/Base/BaseControllerTest.php +++ b/tests/Controller/Base/BaseControllerTest.php @@ -14,6 +14,9 @@ use Meritoo\CommonBundle\Exception\Controller\BaseController\CannotRedirectToEmptyRefererUrlException; use Meritoo\Test\CommonBundle\Controller\Base\BaseController\RealController; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; /** * Test case for the base controller with common and useful methods @@ -28,6 +31,7 @@ class BaseControllerTest extends KernelTestCase { private RequestServiceInterface $requestService; + /** @runInSeparateProcess */ public function testRedirectToReferer(): void { $refererUrl = '/'; @@ -40,6 +44,7 @@ public function testRedirectToReferer(): void static::assertSame($refererUrl, $response->getTargetUrl()); } + /** @runInSeparateProcess */ public function testRedirectToRefererOrRoute(): void { $controller = $this->getRealController(); @@ -49,6 +54,7 @@ public function testRedirectToRefererOrRoute(): void static::assertSame('/test/index', $response->getTargetUrl()); } + /** @runInSeparateProcess */ public function testRedirectToRefererUsingEmptyRefererUrl(): void { $this->expectException(CannotRedirectToEmptyRefererUrlException::class); @@ -74,12 +80,28 @@ protected function setUp(): void parent::setUp(); static::bootKernel(); + /** @var RequestStack $requestStack */ + $requestStack = static::getContainer()->get(RequestStack::class); + /** @var RequestServiceInterface $requestService */ $requestService = static::getContainer()->get(RequestServiceInterface::class); + $request = $this->createRequestWithSession(); + $requestStack->push($request); + $this->requestService = $requestService; } + private function createRequestWithSession(): Request + { + $session = new Session(); + + $request = new Request(); + $request->setSession($session); + + return $request; + } + /** * Returns instance of RealController used for testing * diff --git a/tests/Resources/config/packages/test/framework.yaml b/tests/Resources/config/packages/test/framework.yaml index 76eaa63..09774fa 100644 --- a/tests/Resources/config/packages/test/framework.yaml +++ b/tests/Resources/config/packages/test/framework.yaml @@ -1,7 +1,7 @@ framework: test: true session: - storage_id: session.storage.mock_file + storage_factory_id: session.storage.factory.mock_file translator: default_path: '%kernel.project_dir%/../src/Resources/translations' fallbacks: diff --git a/tests/Service/RequestServiceTest.php b/tests/Service/RequestServiceTest.php index c5d3fbf..6061ad1 100644 --- a/tests/Service/RequestServiceTest.php +++ b/tests/Service/RequestServiceTest.php @@ -19,7 +19,7 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\HttpFoundation\Session\Session; /** * Test case for the service that serves request @@ -35,7 +35,7 @@ class RequestServiceTest extends KernelTestCase { use BaseTestCaseTrait; - private SessionInterface $session; + private RequestStack $requestStack; private RequestServiceInterface $requestService; public function provideCurrentRoute(): ?Generator @@ -100,26 +100,26 @@ public function provideRequestAndRefererUrl(): Generator */ public function provideRequestAndRefererUrlToStore(): Generator { - yield [ + yield 'no referer data' => [ new Request(), null, ]; - yield [ + yield 'an empty string as referer' => [ new Request([], [], [], [], [], [ 'HTTP_REFERER' => '', ]), null, ]; - yield [ + yield '"/" as referer' => [ new Request([], [], [], [], [], [ 'HTTP_REFERER' => '/', ]), '/', ]; - yield [ + yield '"/products/123" as referer' => [ new Request([], [], [], [], [], [ 'HTTP_REFERER' => '/products/123', ]), @@ -144,8 +144,8 @@ public function testConstructor(): void static::assertConstructorVisibilityAndArguments( RequestService::class, OopVisibility::Public, - 2, - 2, + 1, + 1, ); } @@ -153,10 +153,15 @@ public function testConstructor(): void * @param string $expected Expected url of referer * * @dataProvider provideUrl + * @runInSeparateProcess */ public function testFetchRefererUrl(string $expected): void { - $this->session->set('meritoo_common.referer_url', $expected); + $this + ->requestStack + ->getSession() + ->set('meritoo_common.referer_url', $expected) + ; static::assertSame($expected, $this->requestService->fetchRefererUrl()); static::assertSame('', $this->requestService->fetchRefererUrl()); @@ -166,7 +171,6 @@ public function testGetCurrentRoute(): void { $expected = 'test-route'; - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); $request = $this->createMock(Request::class); @@ -183,7 +187,7 @@ public function testGetCurrentRoute(): void ->willReturn($expected) ; - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $result = $service->getCurrentRoute(); static::assertSame($expected, $result); @@ -194,16 +198,14 @@ public function testGetCurrentRouteIfCurrentRequestIsUnknown(): void $this->expectException(UnknownRequestException::class); $this->expectExceptionMessage('Cannot get current request, because it is unknown'); - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $service->getCurrentRoute(); } public function testGetCurrentRouteIfCurrentRouteIsUnknown(): void { - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); $request = $this->createMock(Request::class); @@ -220,7 +222,7 @@ public function testGetCurrentRouteIfCurrentRouteIsUnknown(): void ->willReturn(null) ; - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $result = $service->getCurrentRoute(); static::assertSame('', $result); @@ -233,7 +235,6 @@ public function testGetCurrentRouteParameters(): void 'parameter2' => 'test2', ]; - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); $request = $this->createMock(Request::class); @@ -250,7 +251,7 @@ public function testGetCurrentRouteParameters(): void ->willReturn($expected) ; - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $result = $service->getCurrentRouteParameters(); static::assertSame($expected, $result); @@ -261,16 +262,14 @@ public function testGetCurrentRouteParametersIfCurrentRequestIsUnknown(): void $this->expectException(UnknownRequestException::class); $this->expectExceptionMessage('Cannot get current request, because it is unknown'); - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $service->getCurrentRouteParameters(); } public function testGetCurrentRouteParametersIfRouteParametersAreUnknown(): void { - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); $request = $this->createMock(Request::class); @@ -287,7 +286,7 @@ public function testGetCurrentRouteParametersIfRouteParametersAreUnknown(): void ->willReturn(null) ; - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $result = $service->getCurrentRouteParameters(); static::assertSame([], $result); @@ -298,7 +297,6 @@ public function testGetParameter(): void $expected = 'test-value'; $parameter = 'test-parameter'; - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); $request = $this->createMock(Request::class); @@ -315,7 +313,7 @@ public function testGetParameter(): void ->willReturn($expected) ; - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $result = $service->getParameter('test-parameter'); static::assertSame($expected, $result); @@ -326,10 +324,9 @@ public function testGetParameterIfCurrentRequestIsUnknown(): void $this->expectException(UnknownRequestException::class); $this->expectExceptionMessage('Cannot get current request, because it is unknown'); - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $service->getParameter('test'); } @@ -349,7 +346,6 @@ public function testGetRefererUrl(Request $request, ?string $expected): void */ public function testIsCurrentRoute(string $currentRoute, string $route, bool $expected): void { - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); $request = $this->createMock(Request::class); @@ -366,7 +362,7 @@ public function testIsCurrentRoute(string $currentRoute, string $route, bool $ex ->willReturn($currentRoute) ; - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); self::assertSame($expected, $service->isCurrentRoute($route)); } @@ -375,10 +371,9 @@ public function testIsCurrentRouteIfCurrentRequestIsUnknown(): void $this->expectException(UnknownRequestException::class); $this->expectExceptionMessage('Cannot get current request, because it is unknown'); - $session = $this->createMock(SessionInterface::class); $requestStack = $this->createMock(RequestStack::class); - $service = new RequestService($session, $requestStack); + $service = new RequestService($requestStack); $service->isCurrentRoute(''); } @@ -386,11 +381,19 @@ public function testIsCurrentRouteIfCurrentRequestIsUnknown(): void * @param string $url The referer url to store * * @dataProvider provideUrl + * @runInSeparateProcess */ public function testStoreRefererUrl(string $url): void { $this->requestService->storeRefererUrl($url); - static::assertSame($url, $this->session->get('meritoo_common.referer_url')); + + $refererUrl = $this + ->requestStack + ->getSession() + ->get('meritoo_common.referer_url') + ; + + static::assertSame($url, $refererUrl); } /** @@ -398,6 +401,7 @@ public function testStoreRefererUrl(string $url): void * @param null|string $expected Expected url of referer * * @dataProvider provideRequestAndRefererUrlToStore + * @runInSeparateProcess */ public function testStoreRefererUrlFromRequest(Request $request, ?string $expected): void { @@ -406,7 +410,13 @@ public function testStoreRefererUrlFromRequest(Request $request, ?string $expect ->storeRefererUrlFromRequest($request) ; - static::assertSame($expected, $this->session->get('meritoo_common.referer_url')); + $refererUrl = $this + ->requestStack + ->getSession() + ->get('meritoo_common.referer_url') + ; + + static::assertSame($expected, $refererUrl); } /** @@ -417,13 +427,26 @@ protected function setUp(): void parent::setUp(); static::bootKernel(); - /** @var SessionInterface $session */ - $session = static::getContainer()->get(SessionInterface::class); + /** @var RequestStack $requestStack */ + $requestStack = static::getContainer()->get(RequestStack::class); /** @var RequestServiceInterface $requestService */ $requestService = static::getContainer()->get(RequestServiceInterface::class); - $this->session = $session; + $request = $this->createRequestWithSession(); + $requestStack->push($request); + + $this->requestStack = $requestStack; $this->requestService = $requestService; } + + private function createRequestWithSession(): Request + { + $session = new Session(); + + $request = new Request(); + $request->setSession($session); + + return $request; + } }