Skip to content

Commit

Permalink
Merge pull request #130 from coopTilleuls/chore/php-8
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon authored Nov 24, 2023
2 parents e2035c7 + 8433b11 commit abb88cc
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 458 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- name: Run php-cs-fixer tests
if: matrix.quality
env:
# PHP CS Fixer does not support PHP 8.2 yet
# PHP CS Fixer does not support PHP 8.3 yet
PHP_CS_FIXER_IGNORE_ENV: 1
run: php-cs-fixer fix --diff --dry-run
- name: Run PHPUnit tests
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"jms/serializer-bundle": "^1.4 || ^2.3 || ^3.0 || ^4.0",
"laminas/laminas-code": "^3.4 || ^4.0",
"ocramius/proxy-manager": "^2.0.4",
"phpspec/prophecy": "^1.10",
"sebastian/comparator": "^3.0",
"symfony/asset": "^5.1 || ^6.0",
"symfony/browser-kit": "^5.1 || ^6.0",
Expand Down
49 changes: 23 additions & 26 deletions tests/Bridge/ApiPlatform/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CoopTilleuls\ForgotPasswordBundle\Bridge\ApiPlatform\Serializer\DocumentationNormalizer;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderChainInterface;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderInterface;
use CoopTilleuls\ForgotPasswordBundle\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Routing\Route;
Expand All @@ -29,8 +28,6 @@
*/
final class DocumentationNormalizerTest extends TestCase
{
use ProphecyTrait;

/**
* @var NormalizerInterface|ObjectProphecy
*/
Expand Down Expand Up @@ -68,41 +65,41 @@ final class DocumentationNormalizerTest extends TestCase

protected function setUp(): void
{
$this->normalizerMock = $this->prophesize(NormalizerInterface::class);
$this->routerMock = $this->prophesize(RouterInterface::class);
$this->routeCollectionMock = $this->prophesize(RouteCollection::class);
$this->routeMock = $this->prophesize(Route::class);
$this->providerChainMock = $this->prophesize(ProviderChainInterface::class);
$this->providerMock = $this->prophesize(ProviderInterface::class);
$this->normalizerMock = $this->createMock(NormalizerInterface::class);
$this->routerMock = $this->createMock(RouterInterface::class);
$this->routeCollectionMock = $this->createMock(RouteCollection::class);
$this->routeMock = $this->createMock(Route::class);
$this->providerChainMock = $this->createMock(ProviderChainInterface::class);
$this->providerMock = $this->createMock(ProviderInterface::class);
$this->normalizer = new DocumentationNormalizer(
$this->normalizerMock->reveal(),
$this->routerMock->reveal(),
$this->providerChainMock->reveal()
$this->normalizerMock,
$this->routerMock,
$this->providerChainMock
);
}

public function testItSupportsDecoratedSupport(): void
{
$this->normalizerMock->supportsNormalization('foo', 'bar')->willReturn(true)->shouldBeCalledOnce();
$this->normalizerMock->expects($this->once())->method('supportsNormalization')->with('foo', 'bar')->willReturn(true);
$this->assertTrue($this->normalizer->supportsNormalization('foo', 'bar'));
}

public function testItDecoratesNormalizedData(): void
{
$this->routerMock->getRouteCollection()->willReturn($this->routeCollectionMock)->shouldBeCalledOnce();
$this->routeCollectionMock->get('coop_tilleuls_forgot_password.reset')->willReturn($this->routeMock)->shouldBeCalledOnce();
$this->routeCollectionMock->get('coop_tilleuls_forgot_password.get_token')->willReturn($this->routeMock)->shouldBeCalledOnce();
$this->routeCollectionMock->get('coop_tilleuls_forgot_password.update')->willReturn($this->routeMock)->shouldBeCalledOnce();
$this->routeMock->getPath()->willReturn('/api/forgot-password/', '/api/forgot-password/{tokenValue}', '/api/forgot-password/{tokenValue}')->shouldBeCalledTimes(3);
$this->routerMock->expects($this->once())->method('getRouteCollection')->willReturn($this->routeCollectionMock);
$this->routeCollectionMock->expects($this->exactly(3))->method('get')
->withConsecutive(['coop_tilleuls_forgot_password.reset'], ['coop_tilleuls_forgot_password.get_token'], ['coop_tilleuls_forgot_password.update'])
->willReturn($this->routeMock);
$this->routeMock->expects($this->exactly(3))->method('getPath')->willReturnOnConsecutiveCalls('/api/forgot-password/', '/api/forgot-password/{tokenValue}', '/api/forgot-password/{tokenValue}');

$this->providerChainMock->all()->willReturn([
'user' => $this->providerMock->reveal(),
'admin' => $this->providerMock->reveal(),
])->shouldBeCalledOnce();
$this->providerMock->getUserPasswordField()->willReturn('password', 'password')->shouldBeCalledTimes(2);
$this->providerMock->getUserAuthorizedFields()->willReturn(['email'], ['username', 'email'])->shouldBeCalledTimes(2);
$this->providerChainMock->expects($this->once())->method('all')->willReturn([
'user' => $this->providerMock,
'admin' => $this->providerMock,
]);
$this->providerMock->expects($this->exactly(2))->method('getUserPasswordField')->willReturn('password');
$this->providerMock->expects($this->exactly(2))->method('getUserAuthorizedFields')->willReturnOnConsecutiveCalls(['email'], ['username', 'email']);

$this->normalizerMock->normalize(new \stdClass(), 'bar', [])->willReturn([
$this->normalizerMock->expects($this->once())->method('normalize')->with(new \stdClass(), 'bar', [])->willReturn([
'tags' => [['name' => 'Login']],
'paths' => [
'/login' => [
Expand Down Expand Up @@ -148,7 +145,7 @@ public function testItDecoratesNormalizedData(): void
],
],
],
])->shouldBeCalledOnce();
]);
$this->assertEquals([
'tags' => [['name' => 'Login'], ['name' => 'Forgot password']],
'paths' => [
Expand Down
21 changes: 7 additions & 14 deletions tests/Controller/GetTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Normalizer\NormalizerInterface;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderInterface;
use CoopTilleuls\ForgotPasswordBundle\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -27,8 +26,6 @@
*/
final class GetTokenTest extends TestCase
{
use ProphecyTrait;

/**
* @var ProviderInterface|ObjectProphecy
*/
Expand All @@ -46,21 +43,17 @@ final class GetTokenTest extends TestCase

protected function setUp(): void
{
$this->providerMock = $this->prophesize(ProviderInterface::class);
$this->normalizerMock = $this->prophesize(NormalizerInterface::class);
$this->tokenMock = $this->prophesize(AbstractPasswordToken::class);
$this->providerMock = $this->createMock(ProviderInterface::class);
$this->normalizerMock = $this->createMock(NormalizerInterface::class);
$this->tokenMock = $this->createMock(AbstractPasswordToken::class);
}

public function testGetTokenAction(): void
{
$this->providerMock->getPasswordTokenSerializationGroups()
->willReturn(['foo'])
->shouldBeCalledOnce();
$this->normalizerMock->normalize($this->tokenMock->reveal(), 'json', ['groups' => ['foo']])
->willReturn(['foo' => 'bar'])
->shouldBeCalledOnce();
$controller = new GetToken($this->normalizerMock->reveal());
$response = $controller($this->tokenMock->reveal(), $this->providerMock->reveal());
$this->providerMock->expects($this->once())->method('getPasswordTokenSerializationGroups')->willReturn(['foo']);
$this->normalizerMock->expects($this->once())->method('normalize')->with($this->tokenMock, 'json', ['groups' => ['foo']])->willReturn(['foo' => 'bar']);
$controller = new GetToken($this->normalizerMock);
$response = $controller($this->tokenMock, $this->providerMock);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(json_encode(['foo' => 'bar']), $response->getContent());
}
Expand Down
13 changes: 5 additions & 8 deletions tests/Controller/ResetPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use CoopTilleuls\ForgotPasswordBundle\Controller\ResetPassword;
use CoopTilleuls\ForgotPasswordBundle\Manager\ForgotPasswordManager;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderInterface;
use CoopTilleuls\ForgotPasswordBundle\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -26,8 +25,6 @@
*/
final class ResetPasswordTest extends TestCase
{
use ProphecyTrait;

/**
* @var ProviderInterface|ObjectProphecy
*/
Expand All @@ -39,15 +36,15 @@ final class ResetPasswordTest extends TestCase

protected function setUp(): void
{
$this->providerMock = $this->prophesize(ProviderInterface::class);
$this->managerMock = $this->prophesize(ForgotPasswordManager::class);
$this->providerMock = $this->createMock(ProviderInterface::class);
$this->managerMock = $this->createMock(ForgotPasswordManager::class);
}

public function testResetPasswordAction(): void
{
$this->managerMock->resetPassword('email', '[email protected]', $this->providerMock)->shouldBeCalledOnce();
$controller = new ResetPassword($this->managerMock->reveal());
$response = $controller('email', '[email protected]', $this->providerMock->reveal());
$this->managerMock->expects($this->once())->method('resetPassword')->with('email', '[email protected]', $this->providerMock);
$controller = new ResetPassword($this->managerMock);
$response = $controller('email', '[email protected]', $this->providerMock);
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('', $response->getContent());
$this->assertEquals(204, $response->getStatusCode());
Expand Down
15 changes: 6 additions & 9 deletions tests/Controller/UpdatePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use CoopTilleuls\ForgotPasswordBundle\Entity\AbstractPasswordToken;
use CoopTilleuls\ForgotPasswordBundle\Manager\ForgotPasswordManager;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderInterface;
use CoopTilleuls\ForgotPasswordBundle\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -27,8 +26,6 @@
*/
final class UpdatePasswordTest extends TestCase
{
use ProphecyTrait;

/**
* @var ProviderInterface|ObjectProphecy
*/
Expand All @@ -45,19 +42,19 @@ final class UpdatePasswordTest extends TestCase

protected function setUp(): void
{
$this->providerMock = $this->prophesize(ProviderInterface::class);
$this->managerMock = $this->prophesize(ForgotPasswordManager::class);
$this->tokenMock = $this->prophesize(AbstractPasswordToken::class);
$this->providerMock = $this->createMock(ProviderInterface::class);
$this->managerMock = $this->createMock(ForgotPasswordManager::class);
$this->tokenMock = $this->createMock(AbstractPasswordToken::class);
}

public function testUpdatePasswordAction(): void
{
$expiredAt = new \DateTime('+1 day');
$expiredAt->setTime((int) $expiredAt->format('H'), (int) $expiredAt->format('m'), (int) $expiredAt->format('s'), 0);

$this->managerMock->updatePassword($this->tokenMock->reveal(), 'bar', $this->providerMock)->shouldBeCalledOnce();
$controller = new UpdatePassword($this->managerMock->reveal());
$response = $controller($this->tokenMock->reveal(), 'bar', $this->providerMock->reveal());
$this->managerMock->expects($this->once())->method('updatePassword')->with($this->tokenMock, 'bar', $this->providerMock);
$controller = new UpdatePassword($this->managerMock);
$response = $controller($this->tokenMock, 'bar', $this->providerMock);
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('', $response->getContent());
$this->assertEquals(204, $response->getStatusCode());
Expand Down
70 changes: 31 additions & 39 deletions tests/EventListener/ExceptionEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

use CoopTilleuls\ForgotPasswordBundle\EventListener\ExceptionEventListener;
use CoopTilleuls\ForgotPasswordBundle\Exception\MissingFieldHttpException;
use CoopTilleuls\ForgotPasswordBundle\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Expand All @@ -27,48 +25,46 @@
*/
final class ExceptionEventListenerTest extends TestCase
{
use ProphecyTrait;

public function testOnKernelExceptionInvalid(): void
{
if (class_exists(ExceptionEvent::class)) {
$eventMock = $this->prophesize(ExceptionEvent::class);
$eventMock->getThrowable()->willReturn($this->prophesize(\Exception::class)->reveal())->shouldBeCalledOnce();
$eventMock = $this->createMock(ExceptionEvent::class);
$eventMock->expects($this->once())->method('getThrowable')->willReturn($this->createMock(\Exception::class));
} else {
$eventMock = $this->prophesize(GetResponseForExceptionEvent::class);
$eventMock->getException()->willReturn($this->prophesize(\Exception::class)->reveal())->shouldBeCalledOnce();
$eventMock = $this->createMock(GetResponseForExceptionEvent::class);
$eventMock->expects($this->once())->method('getException')->willReturn($this->createMock(\Exception::class));
}

if (method_exists(ExceptionEvent::class, 'isMainRequest')) {
$eventMock->isMainRequest()->willReturn(true)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('isMainRequest')->willReturn(true);
} else {
$eventMock->isMasterRequest()->willReturn(true)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('isMasterRequest')->willReturn(true);
}
$eventMock->setResponse(Argument::any())->shouldNotBeCalled();
$eventMock->expects($this->never())->method('setResponse');

$listener = new ExceptionEventListener();
$listener->onKernelException($eventMock->reveal());
$listener->onKernelException($eventMock);
}

public function testOnKernelExceptionSubRequest(): void
{
if (class_exists(ExceptionEvent::class)) {
$eventMock = $this->prophesize(ExceptionEvent::class);
$eventMock->getThrowable()->willReturn($this->prophesize(\Exception::class)->reveal())->shouldBeCalledOnce();
$eventMock = $this->createMock(ExceptionEvent::class);
$eventMock->expects($this->once())->method('getThrowable')->willReturn($this->createMock(\Exception::class));
} else {
$eventMock = $this->prophesize(GetResponseForExceptionEvent::class);
$eventMock->getException()->willReturn($this->prophesize(\Exception::class)->reveal())->shouldBeCalledOnce();
$eventMock = $this->createMock(GetResponseForExceptionEvent::class);
$eventMock->expects($this->once())->method('getException')->willReturn($this->createMock(\Exception::class));
}

if (method_exists(ExceptionEvent::class, 'isMainRequest')) {
$eventMock->isMainRequest()->willReturn(false)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('isMainRequest')->willReturn(false);
} else {
$eventMock->isMasterRequest()->willReturn(false)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('isMasterRequest')->willReturn(false);
}
$eventMock->setResponse(Argument::any())->shouldNotBeCalled();
$eventMock->expects($this->never())->method('setResponse');

$listener = new ExceptionEventListener();
$listener->onKernelException($eventMock->reveal());
$listener->onKernelException($eventMock);
}

public function testOnKernelException(): void
Expand All @@ -78,31 +74,27 @@ public function testOnKernelException(): void
$exception = new MissingFieldHttpException('foo');

if (class_exists(ExceptionEvent::class)) {
$eventMock = $this->prophesize(ExceptionEvent::class);
$eventMock->getThrowable()->willReturn($exception)->shouldBeCalledOnce();
$eventMock = $this->createMock(ExceptionEvent::class);
$eventMock->expects($this->once())->method('getThrowable')->willReturn($exception);
} else {
$eventMock = $this->prophesize(GetResponseForExceptionEvent::class);
$eventMock->getException()->willReturn($exception)->shouldBeCalledOnce();
$eventMock = $this->createMock(GetResponseForExceptionEvent::class);
$eventMock->expects($this->once())->method('getException')->willReturn($exception);
}
if (method_exists(ExceptionEvent::class, 'isMainRequest')) {
$eventMock->isMainRequest()->willReturn(true)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('isMainRequest')->willReturn(true);
} else {
$eventMock->isMasterRequest()->willReturn(true)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('isMasterRequest')->willReturn(true);
}
$eventMock->setResponse(
Argument::that(
function ($response) {
return $response instanceof JsonResponse
&& json_encode(
['message' => 'Parameter "foo" is missing.'],
15
) === $response->getContent()
&& 400 === $response->getStatusCode();
}
)
)->shouldBeCalledOnce();
$eventMock->expects($this->once())->method('setResponse')->with($this->callback(function ($response) {
return $response instanceof JsonResponse
&& json_encode(
['message' => 'Parameter "foo" is missing.'],
15
) === $response->getContent()
&& 400 === $response->getStatusCode();
}));

$listener = new ExceptionEventListener();
$listener->onKernelException($eventMock->reveal());
$listener->onKernelException($eventMock);
}
}
Loading

0 comments on commit abb88cc

Please sign in to comment.