summaryrefslogtreecommitdiff
path: root/tests/Unit
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
parent8e62c4c52c27f9432820915deeb699c3d1f58ce7 (diff)
Changed `src/` code and templates to use the new user model
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Exceptions/Handlers/WhoopsTest.php32
-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
-rw-r--r--tests/Unit/Middleware/LegacyMiddlewareTest.php9
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php31
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php21
7 files changed, 183 insertions, 19 deletions
diff --git a/tests/Unit/Exceptions/Handlers/WhoopsTest.php b/tests/Unit/Exceptions/Handlers/WhoopsTest.php
index 4062979b..4949c45a 100644
--- a/tests/Unit/Exceptions/Handlers/WhoopsTest.php
+++ b/tests/Unit/Exceptions/Handlers/WhoopsTest.php
@@ -5,6 +5,7 @@ namespace Engelsystem\Test\Unit\Exceptions\handlers;
use Engelsystem\Application;
use Engelsystem\Exceptions\Handlers\Whoops;
+use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Request;
use Exception;
use PHPUnit\Framework\TestCase;
@@ -23,15 +24,23 @@ class WhoopsTest extends TestCase
{
/** @var Application|Mock $app */
$app = $this->createMock(Application::class);
+ /** @var Authenticator|Mock $auth */
+ $auth = $this->createMock(Authenticator::class);
/** @var Request|Mock $request */
$request = $this->createMock(Request::class);
- $request->expects($this->once())
- ->method('isXmlHttpRequest')
- ->willReturn(true);
/** @var WhoopsRunnerInterface|Mock $whoopsRunner */
$whoopsRunner = $this->getMockForAbstractClass(WhoopsRunnerInterface::class);
/** @var PrettyPageHandler|Mock $prettyPageHandler */
$prettyPageHandler = $this->createMock(PrettyPageHandler::class);
+ /** @var JsonResponseHandler|Mock $jsonResponseHandler */
+ $jsonResponseHandler = $this->createMock(JsonResponseHandler::class);
+ /** @var Exception|Mock $exception */
+ $exception = $this->createMock(Exception::class);
+
+ $request->expects($this->once())
+ ->method('isXmlHttpRequest')
+ ->willReturn(true);
+
$prettyPageHandler
->expects($this->atLeastOnce())
->method('setApplicationPaths');
@@ -41,16 +50,13 @@ class WhoopsTest extends TestCase
$prettyPageHandler
->expects($this->once())
->method('addDataTable');
- /** @var JsonResponseHandler|Mock $jsonResponseHandler */
- $jsonResponseHandler = $this->createMock(JsonResponseHandler::class);
+
$jsonResponseHandler->expects($this->once())
->method('setJsonApi')
->with(true);
$jsonResponseHandler->expects($this->once())
->method('addTraceToOutput')
->with(true);
- /** @var Exception|Mock $exception */
- $exception = $this->createMock(Exception::class);
$app->expects($this->exactly(3))
->method('make')
@@ -64,6 +70,18 @@ class WhoopsTest extends TestCase
$prettyPageHandler,
$jsonResponseHandler
);
+ $app->expects($this->once())
+ ->method('has')
+ ->with('authenticator')
+ ->willReturn(true);
+ $app->expects($this->once())
+ ->method('get')
+ ->with('authenticator')
+ ->willReturn($auth);
+
+ $auth->expects($this->once())
+ ->method('user')
+ ->willReturn(null);
$whoopsRunner
->expects($this->exactly(2))
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;
+ }
+}
diff --git a/tests/Unit/Middleware/LegacyMiddlewareTest.php b/tests/Unit/Middleware/LegacyMiddlewareTest.php
index 09674a59..caea483d 100644
--- a/tests/Unit/Middleware/LegacyMiddlewareTest.php
+++ b/tests/Unit/Middleware/LegacyMiddlewareTest.php
@@ -2,6 +2,7 @@
namespace Engelsystem\Test\Unit\Middleware;
+use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Middleware\LegacyMiddleware;
@@ -23,9 +24,11 @@ class LegacyMiddlewareTest extends TestCase
{
/** @var ContainerInterface|MockObject $container */
$container = $this->getMockForAbstractClass(ContainerInterface::class);
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->createMock(Authenticator::class);
/** @var LegacyMiddleware|MockObject $middleware */
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
- ->setConstructorArgs([$container])
+ ->setConstructorArgs([$container, $auth])
->setMethods(['loadPage', 'renderPage'])
->getMock();
/** @var Request|MockObject $defaultRequest */
@@ -70,6 +73,10 @@ class LegacyMiddlewareTest extends TestCase
$defaultRequest
);
+ $auth->expects($this->atLeastOnce())
+ ->method('user')
+ ->willReturn(false);
+
$translator->expects($this->exactly(2))
->method('translate')
->willReturnOnConsecutiveCalls(
diff --git a/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php
index 0a72c0e7..b67d4eed 100644
--- a/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php
+++ b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php
@@ -2,16 +2,23 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+use Engelsystem\Helpers\Authenticator;
+use Engelsystem\Models\User\User;
use Engelsystem\Renderer\Twig\Extensions\Authentication;
+use PHPUnit\Framework\MockObject\MockObject;
class AuthenticationTest extends ExtensionTest
{
/**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::__construct
* @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::getFunctions
*/
public function testGetFunctions()
{
- $extension = new Authentication();
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->createMock(Authenticator::class);
+
+ $extension = new Authentication($auth);
$functions = $extension->getFunctions();
$this->assertExtensionExists('is_user', [$extension, 'isAuthenticated'], $functions);
@@ -25,15 +32,24 @@ class AuthenticationTest extends ExtensionTest
*/
public function testIsAuthenticated()
{
- global $user;
- $user = [];
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->createMock(Authenticator::class);
+ $user = new User();
+
+ $auth->expects($this->exactly(4))
+ ->method('user')
+ ->willReturnOnConsecutiveCalls(
+ null,
+ null,
+ $user,
+ $user
+ );
- $extension = new Authentication();
+ $extension = new Authentication($auth);
$this->assertFalse($extension->isAuthenticated());
$this->assertTrue($extension->isGuest());
- $user = ['lorem' => 'ipsum'];
$this->assertTrue($extension->isAuthenticated());
$this->assertFalse($extension->isGuest());
}
@@ -46,7 +62,10 @@ class AuthenticationTest extends ExtensionTest
global $privileges;
$privileges = [];
- $extension = new Authentication();
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->createMock(Authenticator::class);
+
+ $extension = new Authentication($auth);
$this->assertFalse($extension->checkAuth('foo.bar'));
diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
index 6441904b..a317ad97 100644
--- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
+++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
@@ -2,25 +2,36 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+use Engelsystem\Helpers\Authenticator;
+use Engelsystem\Models\User\User;
use Engelsystem\Renderer\Twig\Extensions\Globals;
+use PHPUnit\Framework\MockObject\MockObject;
class GlobalsTest extends ExtensionTest
{
/**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::__construct
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
*/
public function testGetGlobals()
{
- global $user;
- $user = [];
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->createMock(Authenticator::class);
+ $user = new User();
- $extension = new Globals();
+ $auth->expects($this->exactly(2))
+ ->method('user')
+ ->willReturnOnConsecutiveCalls(
+ null,
+ $user
+ );
+
+ $extension = new Globals($auth);
$globals = $extension->getGlobals();
$this->assertGlobalsExists('user', [], $globals);
- $user['foo'] = 'bar';
$globals = $extension->getGlobals();
- $this->assertGlobalsExists('user', ['foo' => 'bar'], $globals);
+ $this->assertGlobalsExists('user', $user, $globals);
}
}