From d5e3f13cc353509f92d6d879e6ad97924c51120d Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Wed, 21 Jul 2021 11:37:44 +0200
Subject: [PATCH] Remove more references to core bundle internals

---
 tests/DummyUserProvider.php                   | 35 +++++++++++++
 tests/DummyUserSession.php                    | 50 +++++++++++++++++++
 .../KeycloakBearerAuthenticatorTest.php       |  6 +--
 .../KeycloakBearerUserProviderTest.php        |  4 +-
 tests/Keycloak/KeycloakBearerUserTest.php     |  2 +-
 .../KeycloakLocalTokenValidatorTest.php       |  6 +--
 .../KeycloakRemoteTokenValidatorTest.php      |  6 +--
 tests/Keycloak/KeycloakTest.php               |  2 +-
 8 files changed, 98 insertions(+), 13 deletions(-)
 create mode 100644 tests/DummyUserProvider.php
 create mode 100644 tests/DummyUserSession.php

diff --git a/tests/DummyUserProvider.php b/tests/DummyUserProvider.php
new file mode 100644
index 0000000..507cd14
--- /dev/null
+++ b/tests/DummyUserProvider.php
@@ -0,0 +1,35 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DBP\API\KeycloakBundle\Tests;
+
+use DBP\API\KeycloakBundle\Keycloak\KeycloakBearerUserProviderInterface;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Core\User\UserInterface;
+
+class DummyUserProvider implements KeycloakBearerUserProviderInterface
+{
+    private $user;
+    private $token;
+
+    public function __construct(UserInterface $user, string $token)
+    {
+        $this->user = $user;
+        $this->token = $token;
+    }
+
+    public function loadUserByToken(string $accessToken): UserInterface
+    {
+        if ($this->token !== $accessToken) {
+            throw new AuthenticationException('invalid token');
+        }
+
+        return $this->user;
+    }
+
+    public function loadUserByValidatedToken(array $jwt): UserInterface
+    {
+        return $this->user;
+    }
+}
diff --git a/tests/DummyUserSession.php b/tests/DummyUserSession.php
new file mode 100644
index 0000000..5ac16b1
--- /dev/null
+++ b/tests/DummyUserSession.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DBP\API\KeycloakBundle\Tests;
+
+use DBP\API\CoreBundle\API\UserSessionInterface;
+
+class DummyUserSession implements UserSessionInterface
+{
+    private $jwt;
+    private $id;
+    private $roles;
+
+    public function __construct(?string $id = 'id', array $roles = [])
+    {
+        $this->id = $id;
+        $this->roles = $roles;
+    }
+
+    public function setSessionToken(?array $jwt): void
+    {
+        $this->jwt = $jwt;
+    }
+
+    public function getUserIdentifier(): ?string
+    {
+        return $this->id;
+    }
+
+    public function getUserRoles(): array
+    {
+        return $this->roles;
+    }
+
+    public function getSessionLoggingId(): ?string
+    {
+        return 'logging-id';
+    }
+
+    public function getSessionCacheKey(): ?string
+    {
+        return 'cache';
+    }
+
+    public function getSessionTTL(): int
+    {
+        return 42;
+    }
+}
diff --git a/tests/Keycloak/KeycloakBearerAuthenticatorTest.php b/tests/Keycloak/KeycloakBearerAuthenticatorTest.php
index 77f193e..9fb9e6f 100644
--- a/tests/Keycloak/KeycloakBearerAuthenticatorTest.php
+++ b/tests/Keycloak/KeycloakBearerAuthenticatorTest.php
@@ -5,9 +5,9 @@ declare(strict_types=1);
 namespace DBP\API\KeycloakBundle\Tests\Keycloak;
 
 use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
-use DBP\API\CoreBundle\Keycloak\KeycloakBearerAuthenticator;
-use DBP\API\CoreBundle\Keycloak\KeycloakBearerUser;
-use DBP\API\CoreBundle\TestUtils\DummyUserProvider;
+use DBP\API\KeycloakBundle\Keycloak\KeycloakBearerAuthenticator;
+use DBP\API\KeycloakBundle\Keycloak\KeycloakBearerUser;
+use DBP\API\KeycloakBundle\Tests\DummyUserProvider;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Security\Core\Exception\BadCredentialsException;
 
diff --git a/tests/Keycloak/KeycloakBearerUserProviderTest.php b/tests/Keycloak/KeycloakBearerUserProviderTest.php
index 85d312e..3970fb0 100644
--- a/tests/Keycloak/KeycloakBearerUserProviderTest.php
+++ b/tests/Keycloak/KeycloakBearerUserProviderTest.php
@@ -5,8 +5,8 @@ declare(strict_types=1);
 namespace DBP\API\KeycloakBundle\Tests\Keycloak;
 
 use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
-use DBP\API\CoreBundle\Keycloak\KeycloakBearerUserProvider;
-use DBP\API\CoreBundle\TestUtils\DummyUserSession;
+use DBP\API\KeycloakBundle\Keycloak\KeycloakBearerUserProvider;
+use DBP\API\KeycloakBundle\Tests\DummyUserSession;
 
 class KeycloakBearerUserProviderTest extends ApiTestCase
 {
diff --git a/tests/Keycloak/KeycloakBearerUserTest.php b/tests/Keycloak/KeycloakBearerUserTest.php
index 534a622..d86bdec 100644
--- a/tests/Keycloak/KeycloakBearerUserTest.php
+++ b/tests/Keycloak/KeycloakBearerUserTest.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
 
 namespace DBP\API\KeycloakBundle\Tests\Keycloak;
 
-use DBP\API\CoreBundle\Keycloak\KeycloakBearerUser;
+use DBP\API\KeycloakBundle\Keycloak\KeycloakBearerUser;
 use PHPUnit\Framework\TestCase;
 
 class KeycloakBearerUserTest extends TestCase
diff --git a/tests/Keycloak/KeycloakLocalTokenValidatorTest.php b/tests/Keycloak/KeycloakLocalTokenValidatorTest.php
index df407d5..1a4e41e 100644
--- a/tests/Keycloak/KeycloakLocalTokenValidatorTest.php
+++ b/tests/Keycloak/KeycloakLocalTokenValidatorTest.php
@@ -4,9 +4,9 @@ declare(strict_types=1);
 
 namespace DBP\API\KeycloakBundle\Tests\Keycloak;
 
-use DBP\API\CoreBundle\Keycloak\Keycloak;
-use DBP\API\CoreBundle\Keycloak\KeycloakLocalTokenValidator;
-use DBP\API\CoreBundle\Keycloak\TokenValidationException;
+use DBP\API\KeycloakBundle\Keycloak\Keycloak;
+use DBP\API\KeycloakBundle\Keycloak\KeycloakLocalTokenValidator;
+use DBP\API\KeycloakBundle\Keycloak\TokenValidationException;
 use GuzzleHttp\Handler\MockHandler;
 use GuzzleHttp\HandlerStack;
 use GuzzleHttp\Psr7\Response;
diff --git a/tests/Keycloak/KeycloakRemoteTokenValidatorTest.php b/tests/Keycloak/KeycloakRemoteTokenValidatorTest.php
index 3cabaa7..810cac8 100644
--- a/tests/Keycloak/KeycloakRemoteTokenValidatorTest.php
+++ b/tests/Keycloak/KeycloakRemoteTokenValidatorTest.php
@@ -4,9 +4,9 @@ declare(strict_types=1);
 
 namespace DBP\API\KeycloakBundle\Tests\Keycloak;
 
-use DBP\API\CoreBundle\Keycloak\Keycloak;
-use DBP\API\CoreBundle\Keycloak\KeycloakRemoteTokenValidator;
-use DBP\API\CoreBundle\Keycloak\TokenValidationException;
+use DBP\API\KeycloakBundle\Keycloak\Keycloak;
+use DBP\API\KeycloakBundle\Keycloak\KeycloakRemoteTokenValidator;
+use DBP\API\KeycloakBundle\Keycloak\TokenValidationException;
 use GuzzleHttp\Handler\MockHandler;
 use GuzzleHttp\HandlerStack;
 use GuzzleHttp\Psr7\Response;
diff --git a/tests/Keycloak/KeycloakTest.php b/tests/Keycloak/KeycloakTest.php
index 127a696..8274361 100644
--- a/tests/Keycloak/KeycloakTest.php
+++ b/tests/Keycloak/KeycloakTest.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
 
 namespace DBP\API\KeycloakBundle\Tests\Keycloak;
 
-use DBP\API\CoreBundle\Keycloak\Keycloak;
+use DBP\API\KeycloakBundle\Keycloak\Keycloak;
 use PHPUnit\Framework\TestCase;
 
 class KeycloakTest extends TestCase
-- 
GitLab