2018-09-02 02:09:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
|
|
|
|
2018-10-08 19:30:37 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Models\User\User;
|
2018-09-02 02:09:56 +02:00
|
|
|
use Engelsystem\Renderer\Twig\Extensions\Authentication;
|
2018-10-08 19:30:37 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-09-02 02:09:56 +02:00
|
|
|
|
|
|
|
class AuthenticationTest extends ExtensionTest
|
|
|
|
{
|
|
|
|
/**
|
2018-10-08 19:30:37 +02:00
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::__construct
|
2018-09-02 02:09:56 +02:00
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::getFunctions
|
|
|
|
*/
|
|
|
|
public function testGetFunctions(): void
|
|
|
|
{
|
2018-10-08 19:30:37 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
|
|
|
|
|
|
|
$extension = new Authentication($auth);
|
2018-09-02 02:09:56 +02:00
|
|
|
$functions = $extension->getFunctions();
|
|
|
|
|
|
|
|
$this->assertExtensionExists('is_user', [$extension, 'isAuthenticated'], $functions);
|
|
|
|
$this->assertExtensionExists('is_guest', [$extension, 'isGuest'], $functions);
|
2018-11-12 14:41:23 +01:00
|
|
|
$this->assertExtensionExists('has_permission_to', [$auth, 'can'], $functions);
|
2018-09-02 02:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::isAuthenticated
|
|
|
|
* @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::isGuest
|
|
|
|
*/
|
|
|
|
public function testIsAuthenticated(): void
|
|
|
|
{
|
2018-10-08 19:30:37 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
2021-06-29 00:27:57 +02:00
|
|
|
$user = User::factory()->make();
|
2018-10-08 19:30:37 +02:00
|
|
|
|
|
|
|
$auth->expects($this->exactly(4))
|
|
|
|
->method('user')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$user,
|
|
|
|
$user
|
|
|
|
);
|
2018-09-02 02:09:56 +02:00
|
|
|
|
2018-10-08 19:30:37 +02:00
|
|
|
$extension = new Authentication($auth);
|
2018-09-02 02:09:56 +02:00
|
|
|
|
|
|
|
$this->assertFalse($extension->isAuthenticated());
|
|
|
|
$this->assertTrue($extension->isGuest());
|
|
|
|
|
|
|
|
$this->assertTrue($extension->isAuthenticated());
|
|
|
|
$this->assertFalse($extension->isGuest());
|
|
|
|
}
|
|
|
|
}
|