Skip to content

Commit 9000199

Browse files
author
Greg Bowler
committed
Begin login tests (assertion of login uri)
1 parent 15734a7 commit 9000199

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/Authenticator.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ class Authenticator {
1111
private string $redirectPath;
1212
private SessionContainer $session;
1313
private SessionData $sessionData;
14+
private RedirectHandler $redirectHandler;
1415

1516
public function __construct(
1617
string $clientKey,
1718
string $clientSecret,
1819
string $redirectPath,
19-
SessionContainer $session = null
20+
SessionContainer $session = null,
21+
RedirectHandler $redirectHandler = null
2022
) {
2123
if(is_null($session)) {
2224
$session = new GlobalSessionContainer();
@@ -31,6 +33,7 @@ public function __construct(
3133
$this->redirectPath = $redirectPath;
3234
$this->session = $session;
3335
$this->sessionData = $session->get(self::SESSION_KEY);
36+
$this->redirectHandler = $redirectHandler ?? new RedirectHandler();
3437

3538
if($this->authInProgress()) {
3639
$this->completeAuth();
@@ -50,18 +53,23 @@ public function isLoggedIn():bool {
5053
return isset($userData);
5154
}
5255

56+
public function login():void {
57+
if($this->isLoggedIn()) {
58+
return;
59+
}
60+
61+
62+
}
63+
5364
public function logout():void {
65+
// TODO: Should the logout redirect the user agent to the redirectPath?
5466
$this->session->remove(self::SESSION_KEY);
5567
}
5668

5769
private function authInProgress():bool {
5870
return false;
5971
}
6072

61-
private function beginAuth():void {
62-
63-
}
64-
6573
private function completeAuth():void {
6674

6775
}

src/RedirectHandler.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace Authwave;
3+
4+
use Psr\Http\Message\UriInterface;
5+
6+
class RedirectHandler {
7+
public function redirect(UriInterface $uri, int $code = 303):void {
8+
header("Location: $uri", true, $code);
9+
exit;
10+
}
11+
}

test/phpunit/AuthenticatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
use Authwave\Authenticator;
55
use Authwave\GlobalSessionContainer;
6+
use Authwave\RedirectHandler;
67
use Authwave\SessionData;
78
use Authwave\SessionNotStartedException;
9+
use Authwave\Test\Helper\TestRedirectHandler;
810
use Authwave\UserData;
911
use PHPUnit\Framework\TestCase;
1012

@@ -74,4 +76,24 @@ public function testLogoutClearsSession() {
7476
$sut->logout();
7577
self::assertEmpty($_SESSION);
7678
}
79+
80+
public function testLoginRedirects() {
81+
$redirectHandler = self::createMock(RedirectHandler::class);
82+
$redirectHandler->expects(self::once())
83+
->method("redirect")
84+
->with(
85+
self::callback(function($uri) {
86+
echo $uri;
87+
})
88+
);
89+
90+
$sut = new Authenticator(
91+
"test-key",
92+
"test-secret",
93+
"/",
94+
null,
95+
$redirectHandler
96+
);
97+
$sut->login();
98+
}
7799
}

0 commit comments

Comments
 (0)