Skip to content

Commit e4c6ba6

Browse files
author
klapaudius
committed
Refactor client initialization and add test for null request
Refactored client initialization to allow nullable ClientInterface with a default value. Replaced redundant `isset` checks with null coalescing operator for cleaner code. Added a unit test to verify behavior when no current request is available, ensuring robust error handling.
1 parent 49ec8ae commit e4c6ba6

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

Controller/AuthorizeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
class AuthorizeController
4444
{
45-
private ClientInterface $client;
45+
private ?ClientInterface $client = null;
4646

4747
/**
4848
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
@@ -146,7 +146,7 @@ protected function getClient(): ClientInterface
146146

147147
if (null === $clientId = $request->get('client_id')) {
148148
$formData = $request->get($this->authorizeForm->getName(), []);
149-
$clientId = isset($formData['client_id']) ? $formData['client_id'] : null;
149+
$clientId = $formData['client_id'] ?? null;
150150
}
151151

152152
$this->client = $this->clientManager->findClientByPublicId($clientId);

Tests/Controller/AuthorizeControllerTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPUnit\Framework\Attributes\Small;
2424
use PHPUnit\Framework\MockObject\MockObject;
2525
use PHPUnit\Framework\TestCase;
26+
use ReflectionMethod;
2627
use ReflectionProperty;
2728
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2829
use Symfony\Component\Form\Form;
@@ -168,9 +169,14 @@ public function testAuthorizeActionWillRenderTemplate(): void
168169
->with('_fos_oauth_server.ensure_logout')
169170
->willReturn(false);
170171

171-
$propertyReflection = new ReflectionProperty(AuthorizeController::class, 'client');
172-
$propertyReflection->setAccessible(true);
173-
$propertyReflection->setValue($this->instance, $this->client);
172+
$this->requestStack
173+
->expects($this->once())
174+
->method('getCurrentRequest')
175+
->willReturn($this->request);
176+
177+
$this->clientManager->expects($this->once())
178+
->method('findClientByPublicId')
179+
->willReturn($this->client);
174180

175181
$this->eventDispatcher
176182
->expects($this->exactly(1))
@@ -477,4 +483,20 @@ public function prepareAuthorizationData(string $randomScope): array
477483

478484
return $formFields;
479485
}
486+
487+
public function testGetCurrentRequestWithNoRequestWillThrowException(): void
488+
{
489+
$reflectionMethod = new ReflectionMethod(AuthorizeController::class, 'getCurrentRequest');
490+
$reflectionMethod->setAccessible(true);
491+
492+
$this->requestStack
493+
->expects($this->once())
494+
->method('getCurrentRequest')
495+
->willReturn(null);
496+
497+
$this->expectException(\RuntimeException::class);
498+
$this->expectExceptionMessage('No current request.');
499+
500+
$reflectionMethod->invoke($this->instance);
501+
}
480502
}

0 commit comments

Comments
 (0)