Skip to content

Commit 265e077

Browse files
committed
Improve authorization constructor
1 parent f2e454f commit 265e077

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

src/Authentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Authentication implements AuthenticationInterface
7070
*/
7171
private $on_user_deauthenticated = [];
7272

73-
public function __construct(array $adapters)
73+
public function __construct(AdapterInterface ...$adapters)
7474
{
7575
foreach ($adapters as $adapter) {
7676
if (!($adapter instanceof AdapterInterface)) {

test/src/AuthenticationMiddlewareTest.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@
2727
use ActiveCollab\Authentication\Token\TokenInterface;
2828
use ActiveCollab\Cookies\Cookies;
2929
use ActiveCollab\Cookies\CookiesInterface;
30-
use LogicException;
3130
use Psr\Http\Message\ResponseInterface;
3231
use Psr\Http\Message\ServerRequestInterface;
3332
use Psr\Http\Server\RequestHandlerInterface;
34-
use stdClass;
3533
use Zend\Diactoros\ResponseFactory;
3634

3735
class AuthenticationMiddlewareTest extends RequestResponseTestCase
@@ -127,21 +125,11 @@ public function setUp()
127125
);
128126
}
129127

130-
public function testExceptionIfInvalidAdaptersAreSet()
131-
{
132-
$this->expectException(LogicException::class);
133-
$this->expectExceptionMessage('Invalid authentication adapter provided');
134-
135-
new Authentication([new stdClass()]);
136-
}
137-
138128
public function testMiddlewareAcceptsMultipleAdapters()
139129
{
140130
$middleware = new Authentication(
141-
[
142-
$this->browser_session_adapter,
143-
$this->token_bearer_adapter
144-
]
131+
$this->browser_session_adapter,
132+
$this->token_bearer_adapter
145133
);
146134

147135
$this->assertIsArray($middleware->getAdapters());
@@ -161,9 +149,7 @@ public function testBrowserSessionAuthentication()
161149
] = $this->cookies->set($this->request, $this->response, $this->browser_session_cookie_name, 'my-session-id');
162150

163151
$middleware = new Authentication(
164-
[
165-
$this->browser_session_adapter
166-
]
152+
$this->browser_session_adapter
167153
);
168154

169155
/** @var ServerRequestInterface $modifiedRequest */
@@ -217,9 +203,7 @@ public function testBrowserSessionAuthenticationPsr15()
217203
$this->assertInstanceOf(ServerRequestInterface::class, $request);
218204

219205
$middleware = new Authentication(
220-
[
221-
$this->browser_session_adapter
222-
]
206+
$this->browser_session_adapter
223207
);
224208

225209
$requestHandler = new class implements RequestHandlerInterface
@@ -268,7 +252,7 @@ public function testTokenBearerAuthentication()
268252
/** @var ServerRequestInterface $request */
269253
$request = $this->request->withHeader('Authorization', 'Bearer awesome-token');
270254

271-
$middleware = new Authentication([$this->token_bearer_adapter]);
255+
$middleware = new Authentication($this->token_bearer_adapter);
272256

273257
/** @var ServerRequestInterface $modifiedRequest */
274258
$modifiedRequest = null;
@@ -313,7 +297,7 @@ public function testTokenBearerAuthenticationPsr15()
313297
/** @var ServerRequestInterface $request */
314298
$request = $this->request->withHeader('Authorization', 'Bearer awesome-token');
315299

316-
$middleware = new Authentication([$this->token_bearer_adapter]);
300+
$middleware = new Authentication($this->token_bearer_adapter);
317301

318302
$requestHandler = new class implements RequestHandlerInterface
319303
{
@@ -360,11 +344,23 @@ public function testExceptionOnMultipleIds()
360344

361345
/** @var ServerRequestInterface $request */
362346
/** @var ResponseInterface $response */
363-
[$request, $response] = $this->cookies->set($this->request, $this->response, $this->browser_session_cookie_name, 'my-session-id');
347+
[
348+
$request,
349+
$response,
350+
] = $this->cookies->set(
351+
$this->request,
352+
$this->response,
353+
$this->browser_session_cookie_name,
354+
'my-session-id'
355+
);
364356

365357
/** @var ServerRequestInterface $request */
366358
$request = $request->withHeader('Authorization', 'Bearer awesome-token');
367359

368-
call_user_func(new Authentication([$this->browser_session_adapter, $this->token_bearer_adapter]), $request, $response);
360+
call_user_func(
361+
new Authentication($this->browser_session_adapter, $this->token_bearer_adapter),
362+
$request,
363+
$response
364+
);
369365
}
370366
}

test/src/EventsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ private function prepareForAuthentication($session_id = 'my-session-id')
249249
]);
250250
$session_repository = new SessionRepository([new Session($session_id, '[email protected]')]);
251251

252-
return new Authentication([
253-
new BrowserSessionAdapter($user_repository, $session_repository, $this->cookies),
254-
]);
252+
return new Authentication(
253+
new BrowserSessionAdapter($user_repository, $session_repository, $this->cookies)
254+
);
255255
}
256256

257257
private function prepareForAuthorization($session_id = 'my-session-id')
@@ -263,7 +263,7 @@ private function prepareForAuthorization($session_id = 'my-session-id')
263263
$session_repository = new SessionRepository([new Session($session_id, '[email protected]')]);
264264

265265
$browser_session_adapter = new BrowserSessionAdapter($user_repository, $session_repository, $this->cookies);
266-
$authentication = new Authentication([$browser_session_adapter]);
266+
$authentication = new Authentication($browser_session_adapter);
267267

268268
return [$authentication, $browser_session_adapter, new LocalAuthorizer($user_repository)];
269269
}

0 commit comments

Comments
 (0)