summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-10-08 19:30:37 +0200
committermsquare <msquare@notrademark.de>2018-10-31 13:43:23 +0100
commitd15946df2dfb0ae2f0ca9371e5c8071df91ab45a (patch)
tree49601aafd74da5a781920543ed92bbd180fe5e89 /tests/Unit/Helpers
parent8e62c4c52c27f9432820915deeb699c3d1f58ce7 (diff)
Changed `src/` code and templates to use the new user model
Diffstat (limited to 'tests/Unit/Helpers')
-rw-r--r--tests/Unit/Helpers/AuthenticatorServiceProviderTest.php25
-rw-r--r--tests/Unit/Helpers/AuthenticatorTest.php55
-rw-r--r--tests/Unit/Helpers/Stub/UserModelImplementation.php29
3 files changed, 109 insertions, 0 deletions
diff --git a/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php b/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php
new file mode 100644
index 00000000..f7819da2
--- /dev/null
+++ b/tests/Unit/Helpers/AuthenticatorServiceProviderTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers;
+
+use Engelsystem\Application;
+use Engelsystem\Helpers\Authenticator;
+use Engelsystem\Helpers\AuthenticatorServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+
+class AuthenticatorServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Helpers\AuthenticatorServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ $app = new Application();
+
+ $serviceProvider = new AuthenticatorServiceProvider($app);
+ $serviceProvider->register();
+
+ $this->assertInstanceOf(Authenticator::class, $app->get(Authenticator::class));
+ $this->assertInstanceOf(Authenticator::class, $app->get('authenticator'));
+ }
+}
diff --git a/tests/Unit/Helpers/AuthenticatorTest.php b/tests/Unit/Helpers/AuthenticatorTest.php
new file mode 100644
index 00000000..085887c4
--- /dev/null
+++ b/tests/Unit/Helpers/AuthenticatorTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers;
+
+use Engelsystem\Helpers\Authenticator;
+use Engelsystem\Models\User\User;
+use Engelsystem\Test\Unit\Helpers\Stub\UserModelImplementation;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class AuthenticatorTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Helpers\Authenticator::__construct(
+ * @covers \Engelsystem\Helpers\Authenticator::user
+ */
+ public function testUser()
+ {
+ /** @var Session|MockObject $session */
+ $session = $this->createMock(Session::class);
+ /** @var UserModelImplementation|MockObject $userRepository */
+ $userRepository = new UserModelImplementation();
+ /** @var User|MockObject $user */
+ $user = $this->createMock(User::class);
+
+ $session->expects($this->exactly(3))
+ ->method('get')
+ ->with('uid')
+ ->willReturnOnConsecutiveCalls(
+ null,
+ 42,
+ 1337
+ );
+
+ $auth = new Authenticator($session, $userRepository);
+
+ // Not in session
+ $this->assertEquals(null, $auth->user());
+
+ // Unknown user
+ UserModelImplementation::$id = 42;
+ $this->assertEquals(null, $auth->user());
+
+ // User found
+ UserModelImplementation::$id = 1337;
+ UserModelImplementation::$user = $user;
+ $this->assertEquals($user, $auth->user());
+
+ // User cached
+ UserModelImplementation::$id = null;
+ UserModelImplementation::$user = null;
+ $this->assertEquals($user, $auth->user());
+ }
+}
diff --git a/tests/Unit/Helpers/Stub/UserModelImplementation.php b/tests/Unit/Helpers/Stub/UserModelImplementation.php
new file mode 100644
index 00000000..934aaeb2
--- /dev/null
+++ b/tests/Unit/Helpers/Stub/UserModelImplementation.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers\Stub;
+
+use Engelsystem\Models\User\User;
+use InvalidArgumentException;
+
+class UserModelImplementation extends User
+{
+ /** @var User */
+ public static $user = null;
+
+ /** @var int */
+ public static $id = null;
+
+ /**
+ * @param mixed $id
+ * @param array $columns
+ * @return User|null
+ */
+ public static function find($id, $columns = ['*'])
+ {
+ if ($id != static::$id) {
+ throw new InvalidArgumentException('Wrong user ID searched');
+ }
+
+ return self::$user;
+ }
+}