MyShaarli/tests/front/controller/LogoutControllerTest.php

58 lines
1.7 KiB
PHP
Raw Normal View History

2020-01-23 21:52:03 +01:00
<?php
declare(strict_types=1);
namespace Shaarli\Front\Controller;
/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
if (!function_exists('Shaarli\Front\Controller\setcookie')) {
function setcookie(string $name, string $value): void {
$_COOKIE[$name] = $value;
}
}
use PHPUnit\Framework\TestCase;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;
use Slim\Http\Request;
use Slim\Http\Response;
class LogoutControllerTest extends TestCase
{
use FrontControllerMockHelper;
2020-01-23 21:52:03 +01:00
/** @var LogoutController */
protected $controller;
public function setUp(): void
{
$this->createContainer();
2020-01-23 21:52:03 +01:00
$this->controller = new LogoutController($this->container);
setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
}
public function testValidControllerInvoke(): void
{
$this->createValidContainerMockSet();
2020-01-23 21:52:03 +01:00
$request = $this->createMock(Request::class);
$response = new Response();
$this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');
2020-01-23 21:52:03 +01:00
$this->container->sessionManager = $this->createMock(SessionManager::class);
$this->container->sessionManager->expects(static::once())->method('logout');
2020-01-23 21:52:03 +01:00
static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
$result = $this->controller->index($request, $response);
static::assertInstanceOf(Response::class, $result);
static::assertSame(302, $result->getStatusCode());
static::assertContains('./', $result->getHeader('Location'));
static::assertSame('false', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
}
}