Skip to content

Commit

Permalink
Merge pull request #88 from jongotlin/sf6
Browse files Browse the repository at this point in the history
Symfony 6
  • Loading branch information
vincentchalamon authored Jan 28, 2022
2 parents a342740 + 036547e commit 8320b46
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 55 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ jobs:
- '8.0'
symfony:
- '4.4.*'
- '5.3.*'
- '5.4.*'
- '6.0.*'
include:
- php: '8.0'
symfony: '5.3.*'
symfony: '5.4.*'
bootable: true
quality: true
exclude:
- php: '8.0'
symfony: '4.4.*'
- php: '7.3'
symfony: '6.0.*'
- php: '7.4'
symfony: '6.0.*'
fail-fast: false
steps:
- name: Checkout
Expand Down Expand Up @@ -65,6 +71,7 @@ jobs:
composer req --ignore-platform-reqs tilleuls/forgot-password-bundle:dev-${GITHUB_REF#refs/heads/}
- name: Run php-cs-fixer tests
run: php-cs-fixer fix --diff --dry-run
if: matrix.quality
- name: Run PHPUnit tests
run: vendor/bin/simple-phpunit --colors=always --testdox
- name: Run Behat tests
Expand Down
4 changes: 2 additions & 2 deletions Bridge/ApiPlatform/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(NormalizerInterface $decorated)
$this->decorated = $decorated;
}

public function normalize($object, $format = null, array $context = [])
public function normalize($object, $format = null, array $context = []): array
{
$docs = $this->decorated->normalize($object, $format, $context);

Expand Down Expand Up @@ -154,7 +154,7 @@ public function normalize($object, $format = null, array $context = [])
return $docs;
}

public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, $format = null): bool
{
return $this->decorated->supportsNormalization($data, $format);
}
Expand Down
4 changes: 3 additions & 1 deletion EventListener/ExceptionEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
*/
final class ExceptionEventListener
{
use MainRequestTrait;

public function onKernelException(KernelEvent $event): void
{
$exception = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
if (!$event->isMasterRequest() || !$exception instanceof JsonHttpExceptionInterface) {
if (!$this->isMainRequest($event) || !$exception instanceof JsonHttpExceptionInterface) {
return;
}

Expand Down
27 changes: 27 additions & 0 deletions EventListener/MainRequestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the CoopTilleulsForgotPasswordBundle package.
*
* (c) Vincent Chalamon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace CoopTilleuls\ForgotPasswordBundle\EventListener;

use Symfony\Component\HttpKernel\Event\KernelEvent;

/**
* @author Jon Gotlin <[email protected]>
*/
trait MainRequestTrait
{
private function isMainRequest(KernelEvent $event): bool
{
return method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest();
}
}
6 changes: 4 additions & 2 deletions EventListener/RequestEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
final class RequestEventListener
{
use MainRequestTrait;

private $authorizedFields;
private $userPasswordField;
private $passwordTokenManager;
Expand All @@ -47,7 +49,7 @@ public function decodeRequest(KernelEvent $event): void
{
$request = $event->getRequest();
$routeName = $request->attributes->get('_route');
if (!$event->isMasterRequest() || !\in_array(
if (!$this->isMainRequest($event) || !\in_array(
$routeName,
['coop_tilleuls_forgot_password.reset', 'coop_tilleuls_forgot_password.update'], true
)
Expand Down Expand Up @@ -88,7 +90,7 @@ public function getTokenFromRequest(KernelEvent $event): void
{
$request = $event->getRequest();
$routeName = $request->attributes->get('_route');
if (!$event->isMasterRequest() || !\in_array(
if (!$this->isMainRequest($event) || !\in_array(
$routeName,
['coop_tilleuls_forgot_password.get_token', 'coop_tilleuls_forgot_password.update'], true
)
Expand Down
41 changes: 22 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"require": {
"php": ">=7.2",
"ircmaxell/random-lib": "^1.2",
"symfony/config": "^3.4 || ^4.4 || ^5.1",
"symfony/dependency-injection": "^3.4.26 || ^4.4 || ^5.1",
"symfony/event-dispatcher": "^3.4 || ^4.4 || ^5.1",
"symfony/http-foundation": "^3.4.35 || ^4.4.7 || ^5.1",
"symfony/http-kernel": "^3.4.35 || ^4.4.13 || ^5.1.5",
"symfony/serializer": "^3.4 || ^4.4 || ^5.1"
"symfony/config": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/dependency-injection": "^3.4.26 || ^4.4 || ^5.1 || ^6.0",
"symfony/event-dispatcher": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/http-foundation": "^3.4.35 || ^4.4.7 || ^5.1 || ^6.0",
"symfony/http-kernel": "^3.4.35 || ^4.4.13 || ^5.1.5 || ^6.0",
"symfony/serializer": "^3.4 || ^4.4 || ^5.1 || ^6.0"
},
"require-dev": {
"api-platform/core": "^2.3",
Expand All @@ -34,22 +34,22 @@
"doctrine/orm": "^2.6.3",
"egulias/email-validator": "^2.1.10",
"friends-of-behat/symfony-extension": "^2.0.11 || ^2.1.0",
"jms/serializer-bundle": "^1.4 || ^2.3 || ^3.0",
"jms/serializer-bundle": "^1.4 || ^2.3 || ^3.0 || ^4.0",
"laminas/laminas-code": "^3.4",
"ocramius/proxy-manager": "^2.0.4",
"phpspec/prophecy": "^1.10",
"sebastian/comparator": "^3.0",
"symfony/asset": "^3.4 || ^4.4 || ^5.1",
"symfony/browser-kit": "^3.4 || ^4.4 || ^5.1",
"symfony/framework-bundle": "^3.4.26 || ^4.4 || ^5.1",
"symfony/mailer": "^4.4 || ^5.1",
"symfony/phpunit-bridge": "^5.1",
"symfony/property-access": "^3.4 || ^4.4 || ^5.1",
"symfony/security-bundle": "^3.4.11 || ^4.4 || ^5.1",
"symfony/stopwatch": "^3.4 || ^4.4 || ^5.1",
"symfony/templating": "^3.4 || ^4.4 || ^5.1",
"symfony/twig-bundle": "^3.4 || ^4.4 || ^5.1",
"symfony/var-dumper": "^3.4 || ^4.4 || ^5.1"
"symfony/asset": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/browser-kit": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/framework-bundle": "^3.4.26 || ^4.4 || ^5.1 || ^6.0",
"symfony/mailer": "^4.4 || ^5.1 || ^6.0",
"symfony/phpunit-bridge": "^5.1 || ^6.0",
"symfony/property-access": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/security-bundle": "^3.4.11 || ^4.4 || ^5.1 || ^6.0",
"symfony/stopwatch": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/templating": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/twig-bundle": "^3.4 || ^4.4 || ^5.1 || ^6.0",
"symfony/var-dumper": "^3.4 || ^4.4 || ^5.1 || ^6.0"
},
"suggest": {
"doctrine/doctrine-bundle": "To connect with Doctrine in Symfony project",
Expand Down Expand Up @@ -82,6 +82,9 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
}
}
58 changes: 44 additions & 14 deletions features/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\Security\Core\User\UserInterface;

Expand All @@ -30,22 +32,22 @@ final class AppKernel extends Kernel
{
use MicroKernelTrait;

public function getCacheDir()
public function getCacheDir(): string
{
return __DIR__.'/cache/'.$this->getEnvironment();
}

public function getLogDir()
public function getLogDir(): string
{
return __DIR__.'/logs/'.$this->getEnvironment();
}

public function getProjectDir()
public function getProjectDir(): string
{
return __DIR__;
}

public function registerBundles()
public function registerBundles(): array
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
Expand All @@ -63,14 +65,28 @@ public function registerBundles()
return $bundles;
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
/**
* @param RoutingConfigurator|RouteCollectionBuilder $routes
*/
protected function configureRoutes($routes): void
{
if ($routes instanceof RoutingConfigurator) {
$routes->import('@CoopTilleulsForgotPasswordBundle/Resources/config/routing.xml')->prefix('/forgot_password');

return;
}

$routes->import('@CoopTilleulsForgotPasswordBundle/Resources/config/routing.xml', '/forgot_password');
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
/**
* @param ContainerConfigurator|ContainerBuilder $container
*/
protected function configureContainer($container, LoaderInterface $loader): void
{
$c->loadFromExtension('coop_tilleuls_forgot_password', [
$method = $container instanceof ContainerConfigurator ? 'extension' : 'loadFromExtension';

$container->{$method}('coop_tilleuls_forgot_password', [
'password_token_class' => PasswordToken::class,
'user_class' => User::class,
'user' => [
Expand All @@ -79,7 +95,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'use_jms_serializer' => 'jmsserializer' === $this->getEnvironment(),
]);

$c->loadFromExtension('doctrine', [
$container->{$method}('doctrine', [
'dbal' => [
'driver' => 'pdo_sqlite',
'path' => '%kernel.cache_dir%/db.sqlite',
Expand All @@ -92,7 +108,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
],
]);

$c->loadFromExtension('framework', array_merge([
$container->{$method}('framework', array_merge([
'secret' => 'CoopTilleulsForgotPasswordBundle',
'mailer' => [
'dsn' => 'null://null',
Expand All @@ -106,8 +122,23 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
],
]));

$c->loadFromExtension('security', [
'encoders' => [UserInterface::class => 'plaintext'],
$firewallExtra = [];
$passwordHashers = [
'password_hashers' => [
UserInterface::class => [
'algorithm' => 'plaintext',
],
],
];
$anonymousRole = 'PUBLIC_ACCESS';

if (6 > Kernel::MAJOR_VERSION) {
$firewallExtra = ['anonymous' => true];
$passwordHashers = ['encoders' => [UserInterface::class => 'plaintext']];
$anonymousRole = 'IS_AUTHENTICATED_ANONYMOUSLY';
}

$container->{$method}('security', $passwordHashers + [
'providers' => [
'in_memory' => [
'memory' => [
Expand All @@ -121,12 +152,11 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'main' => [
'pattern' => '^/',
'stateless' => true,
'anonymous' => true,
'http_basic' => null,
],
] + $firewallExtra,
],
'access_control' => [
['path' => '^/forgot_password', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'],
['path' => '^/forgot_password', 'roles' => $anonymousRole],
['path' => '^/', 'roles' => 'IS_AUTHENTICATED_FULLY'],
],
]);
Expand Down
8 changes: 7 additions & 1 deletion features/app/TestBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public function setPassword($password): void
/**
* {@inheritdoc}
*/
public function getRoles(): void
public function getRoles(): array
{
return [];
}

/**
Expand All @@ -129,4 +130,9 @@ public function getSalt(): void
public function eraseCredentials(): void
{
}

public function getUserIdentifier(): string
{
return (string) $this->email;
}
}
19 changes: 15 additions & 4 deletions tests/EventListener/ExceptionEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public function testOnKernelExceptionInvalid(): void
$eventMock->getException()->willReturn($this->prophesize(\Exception::class)->reveal())->shouldBeCalledTimes(1);
}

$eventMock->isMasterRequest()->willReturn(true)->shouldBeCalledTimes(1);
if (method_exists(ExceptionEvent::class, 'isMainRequest')) {
$eventMock->isMainRequest()->willReturn(true)->shouldBeCalledTimes(1);
} else {
$eventMock->isMasterRequest()->willReturn(true)->shouldBeCalledTimes(1);
}
$eventMock->setResponse(Argument::any())->shouldNotBeCalled();

$listener = new ExceptionEventListener();
Expand All @@ -56,7 +60,11 @@ public function testOnKernelExceptionSubRequest(): void
$eventMock->getException()->willReturn($this->prophesize(\Exception::class)->reveal())->shouldBeCalledTimes(1);
}

$eventMock->isMasterRequest()->willReturn(false)->shouldBeCalledTimes(1);
if (method_exists(ExceptionEvent::class, 'isMainRequest')) {
$eventMock->isMainRequest()->willReturn(false)->shouldBeCalledTimes(1);
} else {
$eventMock->isMasterRequest()->willReturn(false)->shouldBeCalledTimes(1);
}
$eventMock->setResponse(Argument::any())->shouldNotBeCalled();

$listener = new ExceptionEventListener();
Expand All @@ -76,8 +84,11 @@ public function testOnKernelException(): void
$eventMock = $this->prophesize(GetResponseForExceptionEvent::class);
$eventMock->getException()->willReturn($exception)->shouldBeCalledTimes(1);
}

$eventMock->isMasterRequest()->willReturn(true)->shouldBeCalledTimes(1);
if (method_exists(ExceptionEvent::class, 'isMainRequest')) {
$eventMock->isMainRequest()->willReturn(true)->shouldBeCalledTimes(1);
} else {
$eventMock->isMasterRequest()->willReturn(true)->shouldBeCalledTimes(1);
}
$eventMock->setResponse(
Argument::that(
function ($response) {
Expand Down
Loading

0 comments on commit 8320b46

Please sign in to comment.