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 --- tests/Unit/Exceptions/Handlers/WhoopsTest.php | 32 ++++++++++--- .../Helpers/AuthenticatorServiceProviderTest.php | 25 ++++++++++ tests/Unit/Helpers/AuthenticatorTest.php | 55 ++++++++++++++++++++++ .../Unit/Helpers/Stub/UserModelImplementation.php | 29 ++++++++++++ tests/Unit/Middleware/LegacyMiddlewareTest.php | 9 +++- .../Twig/Extensions/AuthenticationTest.php | 31 +++++++++--- .../Unit/Renderer/Twig/Extensions/GlobalsTest.php | 21 +++++++-- 7 files changed, 183 insertions(+), 19 deletions(-) create mode 100644 tests/Unit/Helpers/AuthenticatorServiceProviderTest.php create mode 100644 tests/Unit/Helpers/AuthenticatorTest.php create mode 100644 tests/Unit/Helpers/Stub/UserModelImplementation.php (limited to 'tests/Unit') 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 @@ +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 @@ +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 @@ +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); } } -- cgit v1.2.3-70-g09d2