From d15946df2dfb0ae2f0ca9371e5c8071df91ab45a Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 8 Oct 2018 19:30:37 +0200 Subject: Changed `src/` code and templates to use the new user model --- .../Twig/Extensions/AuthenticationTest.php | 31 +++++++++++++++++----- .../Unit/Renderer/Twig/Extensions/GlobalsTest.php | 21 +++++++++++---- 2 files changed, 41 insertions(+), 11 deletions(-) (limited to 'tests/Unit/Renderer') 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); } } -- cgit v1.2.3-54-g00ecf