3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 /** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
8 if (!function_exists('Shaarli\Front\Controller\Admin\setcookie')) {
9 function setcookie(string $name, string $value): void {
10 $_COOKIE[$name] = $value;
14 use PHPUnit\Framework\TestCase
;
15 use Shaarli\Security\LoginManager
;
16 use Shaarli\Security\SessionManager
;
17 use Slim\Http\Request
;
18 use Slim\Http\Response
;
20 class LogoutControllerTest
extends TestCase
22 use FrontAdminControllerMockHelper
;
24 /** @var LogoutController */
25 protected $controller;
27 public function setUp(): void
29 $this->createContainer();
31 $this->controller
= new LogoutController($this->container
);
33 setcookie(LoginManager
::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
36 public function testValidControllerInvoke(): void
38 $request = $this->createMock(Request
::class);
39 $response = new Response();
41 $this->container
->pageCacheManager
->expects(static::once())->method('invalidateCaches');
43 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
44 $this->container
->sessionManager
->expects(static::once())->method('logout');
46 static::assertSame('hi there', $_COOKIE[LoginManager
::$STAY_SIGNED_IN_COOKIE]);
48 $result = $this->controller
->index($request, $response);
50 static::assertInstanceOf(Response
::class, $result);
51 static::assertSame(302, $result->getStatusCode());
52 static::assertSame(['/subfolder/'], $result->getHeader('location'));
53 static::assertSame('false', $_COOKIE[LoginManager
::$STAY_SIGNED_IN_COOKIE]);