From b72898d65960f1c8bffd7fd8ff8396e897056c17 Mon Sep 17 00:00:00 2001 From: Seth Battis Date: Mon, 28 Nov 2022 00:26:09 +0000 Subject: [PATCH] test Manager --- composer.json | 27 ++++++------- phpunit.xml | 8 +++- src/Actions/HandleLogin.php | 2 + src/Manager.php | 15 ++++--- src/Middleware/RequireAuthentication.php | 3 +- templates/login.php | 4 +- tests/Actions/HandleLoginTest.php | 28 +++++++++++++ tests/Actions/ShowLoginViewTest.php | 35 +++++++++++++++++ tests/Fixtures/ManagerTest/User.php | 29 -------------- tests/Fixtures/Reusable/PageRenderer.php | 39 +++++++++++++++++++ .../{ManagerTest => Reusable}/Session.php | 2 +- tests/Fixtures/Reusable/User.php | 31 +++++++++++++++ tests/Fixtures/Reusable/UserRepository.php | 21 ++++++++++ tests/Fixtures/app/dependencies.inc.php | 8 ++++ tests/Fixtures/app/middleware.inc.php | 12 ++++++ tests/Fixtures/app/routes.inc.php | 20 ++++++++++ tests/Fixtures/app/settings.inc.php | 7 ++++ tests/ManagerTest.php | 26 ++++--------- tests/TestCase.php | 25 +++++++++--- tests/TestZZZ.php | 13 +++++++ tests/bootstrap.php | 5 +++ 21 files changed, 284 insertions(+), 76 deletions(-) create mode 100644 tests/Actions/HandleLoginTest.php create mode 100644 tests/Actions/ShowLoginViewTest.php delete mode 100644 tests/Fixtures/ManagerTest/User.php create mode 100644 tests/Fixtures/Reusable/PageRenderer.php rename tests/Fixtures/{ManagerTest => Reusable}/Session.php (88%) create mode 100644 tests/Fixtures/Reusable/User.php create mode 100644 tests/Fixtures/Reusable/UserRepository.php create mode 100644 tests/Fixtures/app/dependencies.inc.php create mode 100644 tests/Fixtures/app/middleware.inc.php create mode 100644 tests/Fixtures/app/routes.inc.php create mode 100644 tests/Fixtures/app/settings.inc.php create mode 100644 tests/TestZZZ.php create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json index 2295cb0..bfa966b 100644 --- a/composer.json +++ b/composer.json @@ -1,36 +1,37 @@ { "name": "battis/user-session", "description": "User session management for Slim Framework", - "license": "MIT", - "autoload": { - "psr-4": { - "Battis\\UserSession\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "Battis\\UserSession\\Tests\\": "tests" - } - }, "authors": [ { "name": "Seth Battis", "email": "seth@battis.net" } ], + "license": "MIT", "require": { "bryanjhv/slim-session": "^4.0", "slim/http": "^1.2", "slim/php-view": "^3.0", "slim/psr7": "^1.0" }, - "config": { - "sort-packages": true + "autoload": { + "psr-4": { + "Battis\\UserSession\\": "src" + } }, "require-dev": { "battis/data-utilities": "^1.1", + "ext-simplexml": "*", "php-di/php-di": "^6.4", "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.0" + }, + "autoload-dev": { + "psr-4": { + "Battis\\UserSession\\Tests\\": "tests" + } + }, + "config": { + "sort-packages": true } } diff --git a/phpunit.xml b/phpunit.xml index 93357a1..c1d7df2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + src @@ -10,4 +10,10 @@ tests + + + + + + diff --git a/src/Actions/HandleLogin.php b/src/Actions/HandleLogin.php index 2d67972..96f709c 100644 --- a/src/Actions/HandleLogin.php +++ b/src/Actions/HandleLogin.php @@ -47,6 +47,8 @@ public function __invoke(ServerRequest $request, Response $response) return $this->renderer->render($response, "login.php", [ "message" => "bad credentials", "message_type" => "error", + "usernameFieldName" => $this->usernameFieldName, + "passwordFieldName" => $this->passwordFieldName ]); } } diff --git a/src/Manager.php b/src/Manager.php index ee7785f..6439a71 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -10,6 +10,11 @@ class Manager { + public const DEFAULT_LOGIN_PATH = '/auth/login'; + public const DEFAULT_REDIRECT = '/'; + + private const HEADER_LOCATION = 'Location'; + private const USER = "battis.userSession.manager.user"; private const REDIRECT = "battis.userSession.manager.redirect"; @@ -19,8 +24,8 @@ class Manager public function __construct( Session $session, - string $loginPath = "/auth/login", - string $defaultRedirect = "/" + string $loginPath = self::DEFAULT_LOGIN_PATH, + string $defaultRedirect = self::DEFAULT_REDIRECT ) { $this->session = $session; $this->loginPath = $loginPath; @@ -31,7 +36,7 @@ public function startUserLogin(RequestInterface $request): ResponseInterface { $this->session->set(self::REDIRECT, $request->getUri()->getPath()); $response = new Response(); - return $response->withHeader("Location", $this->loginPath)->withStatus(302); // using status 302 forces the redirect to use the GET method + return $response->withHeader(self::HEADER_LOCATION, $this->loginPath)->withStatus(302); // using status 302 forces the redirect to use the GET method } public function startUserSession( @@ -43,7 +48,7 @@ public function startUserSession( $redirect = $this->session->get(self::REDIRECT, $this->defaultRedirect); $this->session->delete(self::REDIRECT); $response = new Response(); - return $response->withHeader("Location", $redirect)->withStatus(302); + return $response->withHeader(self::HEADER_LOCATION, $redirect)->withStatus(302); } public function sessionIsActive(): bool @@ -58,6 +63,6 @@ public function getCurrentUser(): ?UserEntityInterface public function endUserSession(): void { - $this->session->destroy(); + $this->session->clear(); } } diff --git a/src/Middleware/RequireAuthentication.php b/src/Middleware/RequireAuthentication.php index fdf3f6c..4c76597 100644 --- a/src/Middleware/RequireAuthentication.php +++ b/src/Middleware/RequireAuthentication.php @@ -11,9 +11,10 @@ class RequireAuthentication extends Session { private $manager; - public function __construct($settings = [], Manager $manager) + public function __construct($settings = [], Manager $manager = null) { parent::__construct($settings); + assert($manager !== null); $this->manager = $manager; } diff --git a/templates/login.php b/templates/login.php index e40dddb..b6e2ecc 100644 --- a/templates/login.php +++ b/templates/login.php @@ -14,14 +14,14 @@
diff --git a/tests/Actions/HandleLoginTest.php b/tests/Actions/HandleLoginTest.php new file mode 100644 index 0000000..92c7c39 --- /dev/null +++ b/tests/Actions/HandleLoginTest.php @@ -0,0 +1,28 @@ +getAppInstance(); + + $user = new User(); + + /** @var UserRepository $userRepo */ + $userRepo = $app->getContainer()->get(UserRepositoryInterface::class); + $userRepo->addUser($user); + + /** #var Manager $manager */ + $manager = $app->getContainer()->get(Manager::class); + + $response = $app->handle($this->createRequest('POST', Manager::DEFAULT_LOGIN_PATH)); + } +} diff --git a/tests/Actions/ShowLoginViewTest.php b/tests/Actions/ShowLoginViewTest.php new file mode 100644 index 0000000..535c0d4 --- /dev/null +++ b/tests/Actions/ShowLoginViewTest.php @@ -0,0 +1,35 @@ +getAppInstance(); + $response = $app->handle($this->createRequest('GET', Manager::DEFAULT_LOGIN_PATH)); + $payload = (string) $response->getBody(); + $xml = new SimpleXMLElement($payload); + $this->assertNotEmpty($payload); + $this->assertCount(0, $xml->{'non-document-error'}); + $this->assertCount(0, $xml->error); + $this->assertStringContainsString('