Skip to content

Commit

Permalink
Merge pull request #30 from andriisobolatpaysera/symfony-5-support
Browse files Browse the repository at this point in the history
Symfony 5.X support
  • Loading branch information
mSprunskas authored Feb 22, 2024
2 parents 0767b55 + 8fa9aa8 commit 345d752
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 93 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Tests

on:
push:
branches:
- master
pull_request:
branches:
- master

permissions:
contents: read

jobs:
tests:
runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
operating-system:
- 'ubuntu-latest'
php:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
dependency:
- 'highest'

name: PHP ${{ matrix.php }} with ${{ matrix.dependency }} dependencies tests on ${{ matrix.operating-system }}

steps:
- name: Setup PHP version ${{ matrix.php }} on ${{ matrix.operating-system }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Checkout repository
uses: actions/checkout@v4

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency }}

- name: Run all tests
run: composer test
25 changes: 0 additions & 25 deletions .github/workflows/code_quality.yml

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## 5.1.1
### Added
- Support for PHP >=7.4
- Support for Symfony 5.X
### Removed
- Support for Symfony 3.X

## 5.1.0
### Added
- Dockerfiles
Expand Down
39 changes: 25 additions & 14 deletions Listener/RestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
use Paysera\Component\Serializer\Exception\EncodingException;
use Paysera\Bundle\RestBundle\ApiManager;
use Paysera\Bundle\RestBundle\Service\RequestLogger;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Psr\Log\LoggerInterface;

class RestListener
Expand Down Expand Up @@ -64,7 +64,7 @@ public function __construct(
$this->loggersCache = array();
}

public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();

Expand Down Expand Up @@ -92,13 +92,13 @@ public function onKernelRequest(GetResponseEvent $event)
/**
* Ran on kernel.controller event
*
* @param FilterControllerEvent $event
* @param KernelEvent $event
*
* @throws ApiException
* @throws InvalidDataException
* @throws Exception
*/
public function onKernelController(FilterControllerEvent $event)
public function onKernelController(KernelEvent $event)
{
/** @var $request Request */
$request = $event->getRequest();
Expand Down Expand Up @@ -131,9 +131,9 @@ public function onKernelController(FilterControllerEvent $event)
/**
* Ran on kernel.view event
*
* @param GetResponseForControllerResultEvent $event
* @param ViewEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
public function onKernelView(ViewEvent $event)
{
/** @var $request Request */
$request = $event->getRequest();
Expand Down Expand Up @@ -228,6 +228,10 @@ public function onKernelView(GetResponseForControllerResultEvent $event)

$response->setContent($responseContent);

if ($responseContent === null) {
$responseContent = '';
}

$response->setEtag($etag === null ? hash('sha256', $responseContent) : $etag);
$response->headers->set('X-Frame-Options', 'DENY');

Expand All @@ -237,22 +241,29 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
/**
* Ran on kernel.exception event
*
* @param GetResponseForExceptionEvent $event
* @param ExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
public function onKernelException(ExceptionEvent $event)
{
if (!$event->getThrowable() instanceof Exception) {
return;
}

/** @var $request Request */
$request = $event->getRequest();
$logger = $this->getLogger($request);

$logger->debug('Handling kernel.exception', array($event));
$logger->debug($event->getException());

$response = $this->apiManager->getResponseForException($request, $event->getException());
/** @var Exception $exception */
$exception = $event->getThrowable();

$logger->debug($exception);

$response = $this->apiManager->getResponseForException($request, $exception);
if ($response !== null) {
$event->setResponse($response);
$logger->debug('Setting error response', array($response->getContent()));
$exception = $event->getException();

$this->exceptionLogger->log($logger, $response, $exception);
}
Expand Down
4 changes: 4 additions & 0 deletions PayseraRestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* @deprecated Use https://github.com/paysera/lib-api-bundle instead.
*/
class PayseraRestBundle extends Bundle
{
/**
Expand All @@ -22,6 +25,7 @@ class PayseraRestBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new ApiCompilerPass());
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PayseraRestBundle ![](https://travis-ci.org/paysera/lib-rest-bundle.svg?branch=master)
=================

This bundle provides means for rapid API development.
> **Deprecated**, use [https://github.com/paysera/lib-api-bundle](https://github.com/paysera/lib-api-bundle) instead.
This bundle provides means for rapid API development.

Installation
------------
Expand Down
3 changes: 3 additions & 0 deletions RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ public function getLogger()
*/
protected function normalizeControllerKey($controllerKey)
{
if ($controllerKey === null) {
return null;
}
$i = strpos($controllerKey, '\\Controller\\');
if ($i !== false) {
$controllerKey = substr($controllerKey, $i + 12);
Expand Down
7 changes: 4 additions & 3 deletions Security/RoleAndIpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public function isAllowed(Request $request)
return false;
}

$availableRoles = array_map(function (Role $role) {
return $role->getRole();
}, $this->roleHierarchy->getReachableRoles($token->getRoles()));
$availableRoles = array_map(
fn(string $role) => $role,
$this->roleHierarchy->getReachableRoleNames($token->getRoleNames())
);

$availableRoles = array_unique($availableRoles);

Expand Down
12 changes: 6 additions & 6 deletions Tests/Listener/RestListenerLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
use Paysera\Bundle\RestBundle\Service\RequestLogger;
use Paysera\Bundle\RestBundle\Listener\RestListener;
use Paysera\Bundle\RestBundle\Service\ExceptionLogger;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Paysera\Bundle\RestBundle\Service\ParameterToEntityMapBuilder;
use Paysera\Component\Serializer\Factory\ContextAwareNormalizerFactory;

class RestListenerLocaleTest extends TestCase
{
/**
* @var MockInterface|GetResponseEvent
* @var MockInterface|RequestEvent
*/
private $responseEvent;
private $requestEvent;

public function setUp(): void
{
$this->responseEvent = Mockery::mock(GetResponseEvent::class);
$this->requestEvent = Mockery::mock(RequestEvent::class);
}

/**
Expand All @@ -46,9 +46,9 @@ public function testCorrectLocaleIsBeingSet(array $acceptedLocales, $expected, $
$request->headers->set('Accept-Language', $acceptLanguage);
}

$this->responseEvent->shouldReceive('getRequest')->andReturn($request);
$this->requestEvent->shouldReceive('getRequest')->andReturn($request);

$restListener->onKernelRequest($this->responseEvent);
$restListener->onKernelRequest($this->requestEvent);

$this->assertEquals($expected, $request->getLocale());
}
Expand Down
14 changes: 7 additions & 7 deletions Tests/Listener/RestListenerPathConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class RestListenerPathConverterTest extends TestCase
{
/**
* @var MockInterface|FilterControllerEvent
* @var MockInterface|KernelEvent
*/
private $filterControllerEvent;
private $kernelEvent;

public function setUp(): void
{
$this->filterControllerEvent = Mockery::mock(FilterControllerEvent::class);
$this->kernelEvent = Mockery::mock(KernelEvent::class);
}

public function testOnKernelControllerWithRequestQueryMapperValidationThrowsExceptionWithCamelCasePathConverter()
Expand All @@ -51,7 +51,7 @@ public function testOnKernelControllerWithRequestQueryMapperValidationThrowsExce
try {
$this
->createRestListener(new CamelCaseToSnakeCaseConverter())
->onKernelController($this->filterControllerEvent)
->onKernelController($this->kernelEvent)
;
} catch (ApiException $apiException) {
$exceptionThrown = true;
Expand Down Expand Up @@ -79,7 +79,7 @@ public function testOnKernelControllerWithRequestQueryMapperValidationThrowsExce
{
$exceptionThrown = false;
try {
$this->createRestListener(new NoOpConverter())->onKernelController($this->filterControllerEvent);
$this->createRestListener(new NoOpConverter())->onKernelController($this->kernelEvent);
$this->expectException(ApiException::class);
} catch (ApiException $apiException) {
$exceptionThrown = true;
Expand Down Expand Up @@ -128,7 +128,7 @@ private function createRestListener(PropertyPathConverterInterface $pathConverte
$request->attributes = $parameterBag;
$request->query = $queryParameterBag;

$this->filterControllerEvent->shouldReceive('getRequest')->andReturn($request);
$this->kernelEvent->shouldReceive('getRequest')->andReturn($request);

$validator = Mockery::mock(ValidatorInterface::class);

Expand Down
Loading

0 comments on commit 345d752

Please sign in to comment.