Skip to content

Commit 9989c82

Browse files
authored
Remove globals from unit tests (#282)
1 parent 02526ff commit 9989c82

File tree

8 files changed

+14
-100
lines changed

8 files changed

+14
-100
lines changed

src/Controllers/Traits/AuthenticatedGetClientFromRequestTrait.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/Helpers/Client.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
namespace SimpleSAML\Module\oidc\Helpers;
66

77
use Psr\Http\Message\ServerRequestInterface;
8-
use SimpleSAML\Error\BadRequest;
9-
use SimpleSAML\Error\NotFound;
108
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
9+
use SimpleSAML\Module\oidc\Exceptions\OidcException;
1110
use SimpleSAML\Module\oidc\Repositories\ClientRepository;
1211

1312
class Client
@@ -18,9 +17,7 @@ public function __construct(protected Http $http)
1817

1918
/**
2019
* @throws \JsonException
21-
* @throws \SimpleSAML\Error\BadRequest
22-
* @throws \SimpleSAML\Error\NotFound
23-
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
20+
* @throws \SimpleSAML\Module\oidc\Exceptions\OidcException
2421
*/
2522
public function getFromRequest(
2623
ServerRequestInterface $request,
@@ -30,13 +27,13 @@ public function getFromRequest(
3027
$clientId = empty($params['client_id']) ? null : (string)$params['client_id'];
3128

3229
if (!is_string($clientId)) {
33-
throw new BadRequest('Client ID is missing.');
30+
throw new OidcException('Client ID is missing.');
3431
}
3532

3633
$client = $clientRepository->findById($clientId);
3734

3835
if (!$client) {
39-
throw new NotFound('Client not found.');
36+
throw new OidcException('Client not found.');
4037
}
4138

4239
return $client;

src/Services/AuthenticationService.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use SimpleSAML\Module\oidc\Controllers\EndSessionController;
2929
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
3030
use SimpleSAML\Module\oidc\Entities\UserEntity;
31+
use SimpleSAML\Module\oidc\Exceptions\OidcException;
3132
use SimpleSAML\Module\oidc\Factories\AuthSimpleFactory;
3233
use SimpleSAML\Module\oidc\Factories\Entities\UserEntityFactory;
3334
use SimpleSAML\Module\oidc\Factories\ProcessingChainFactory;
@@ -78,12 +79,11 @@ public function __construct(
7879
*
7980
* @return array
8081
* @throws Error\AuthSource
81-
* @throws Error\BadRequest
82-
* @throws Error\NotFound
8382
* @throws Exception
8483
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
8584
* @throws Error\UnserializableException
8685
* @throws \JsonException
86+
* @throws \SimpleSAML\Module\oidc\Exceptions\OidcException
8787
*/
8888
public function processRequest(
8989
ServerRequestInterface $request,
@@ -117,13 +117,14 @@ public function processRequest(
117117

118118

119119
/**
120-
* @param array|null $state
120+
* @param array|null $state
121121
*
122122
* @return UserEntity
123123
* @throws Error\NotFound
124124
* @throws Exception
125125
* @throws \JsonException
126126
* @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
127+
* @throws \SimpleSAML\Module\oidc\Exceptions\OidcException
127128
*/
128129
public function getAuthenticateUser(
129130
?array $state,
@@ -164,7 +165,7 @@ public function getAuthenticateUser(
164165

165166
$client = $this->clientRepository->findById((string)$state['Oidc']['RelyingPartyMetadata']['id']);
166167
if (!$client) {
167-
throw new Error\NotFound('Client not found.');
168+
throw new OidcException('Client not found.');
168169
}
169170

170171
$this->addRelyingPartyAssociation($client, $user);

tests/unit/src/Entities/AccessTokenEntityTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
/**
2020
* @covers \SimpleSAML\Module\oidc\Entities\AccessTokenEntity
21-
*
22-
* @backupGlobals enabled
2321
*/
2422
class AccessTokenEntityTest extends TestCase
2523
{

tests/unit/src/Forms/ClientFormTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ public function setUp(): void
4040
$this->serverRequestMock = $this->createMock(ServerRequest::class);
4141
}
4242

43-
public static function setUpBeforeClass(): void
44-
{
45-
// To make lib/SimpleSAML/Utils/HTTP::getSelfURL() work...
46-
global $_SERVER;
47-
$_SERVER['REQUEST_URI'] = '/';
48-
}
49-
5043
public static function validateOriginProvider(): array
5144
{
5245
return [

tests/unit/src/Helpers/ClientTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use PHPUnit\Framework\MockObject\MockObject;
99
use PHPUnit\Framework\TestCase;
1010
use Psr\Http\Message\ServerRequestInterface;
11-
use SimpleSAML\Error\BadRequest;
12-
use SimpleSAML\Error\NotFound;
1311
use SimpleSAML\Module\oidc\Entities\ClientEntity;
12+
use SimpleSAML\Module\oidc\Exceptions\OidcException;
1413
use SimpleSAML\Module\oidc\Helpers\Client;
1514
use SimpleSAML\Module\oidc\Helpers\Http;
1615
use SimpleSAML\Module\oidc\Repositories\ClientRepository;
@@ -56,15 +55,15 @@ public function testCanGetFromRequest(): void
5655

5756
public function testGetFromRequestThrowsIfNoClientId(): void
5857
{
59-
$this->expectException(BadRequest::class);
58+
$this->expectException(OidcException::class);
6059
$this->expectExceptionMessage('Client ID');
6160

6261
$this->sut()->getFromRequest($this->requestMock, $this->clientRepositoryMock);
6362
}
6463

6564
public function testGetFromRequestThrowsIfClientNotFound(): void
6665
{
67-
$this->expectException(NotFound::class);
66+
$this->expectException(OidcException::class);
6867
$this->expectExceptionMessage('Client not found');
6968

7069
$this->httpMock->expects($this->once())->method('getAllRequestParams')

tests/unit/src/Server/Validators/BearerTokenValidatorTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
/**
2626
* @covers \SimpleSAML\Module\oidc\Server\Validators\BearerTokenValidator
27-
*
28-
* @backupGlobals enabled
2927
*/
3028
class BearerTokenValidatorTest extends TestCase
3129
{
@@ -60,10 +58,6 @@ public function setUp(): void
6058
*/
6159
public static function setUpBeforeClass(): void
6260
{
63-
// To make lib/SimpleSAML/Utils/HTTP::getSelfURL() work...
64-
global $_SERVER;
65-
$_SERVER['REQUEST_URI'] = '';
66-
6761
$tempDir = sys_get_temp_dir();
6862

6963
// Plant certdir config for JsonWebTokenBuilderService (since we don't inject it)

tests/unit/src/Services/AuthenticationServiceTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use SimpleSAML\Auth\State;
1818
use SimpleSAML\Error\Exception;
1919
use SimpleSAML\Error\NoState;
20-
use SimpleSAML\Error\NotFound;
2120
use SimpleSAML\Module\oidc\Entities\ClientEntity;
2221
use SimpleSAML\Module\oidc\Entities\UserEntity;
22+
use SimpleSAML\Module\oidc\Exceptions\OidcException;
2323
use SimpleSAML\Module\oidc\Factories\AuthSimpleFactory;
2424
use SimpleSAML\Module\oidc\Factories\Entities\UserEntityFactory;
2525
use SimpleSAML\Module\oidc\Factories\ProcessingChainFactory;
@@ -84,16 +84,6 @@ class AuthenticationServiceTest extends TestCase
8484
protected MockObject $requestParamsResolverMock;
8585
protected MockObject $userEntityFactoryMock;
8686

87-
/**
88-
* @return void
89-
*/
90-
public static function setUpBeforeClass(): void
91-
{
92-
// To make lib/SimpleSAML/Utils/HTTP::getSelfURL() work...
93-
global $_SERVER;
94-
$_SERVER['REQUEST_URI'] = '';
95-
}
96-
9787
/**
9888
* @throws \PHPUnit\Framework\MockObject\Exception
9989
*/
@@ -280,7 +270,7 @@ public static function getUserState(): array
280270
'AuthorizationRequestParameters' => self::AUTHZ_REQUEST_PARAMS,
281271
],
282272
],
283-
NotFound::class,
273+
OidcException::class,
284274
'/Client not found./',
285275
],
286276
];

0 commit comments

Comments
 (0)