3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 /** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
8 if (!function_exists('Shaarli\Front\Controller\setcookie')) {
9 function setcookie(string $name, string $value): void {
10 $_COOKIE[$name] = $value;
14 use PHPUnit\Framework\TestCase
;
15 use Shaarli\Container\ShaarliContainer
;
16 use Shaarli\Render\PageCacheManager
;
17 use Shaarli\Security\LoginManager
;
18 use Shaarli\Security\SessionManager
;
19 use Slim\Http\Request
;
20 use Slim\Http\Response
;
22 class LogoutControllerTest
extends TestCase
24 /** @var ShaarliContainer */
27 /** @var LogoutController */
28 protected $controller;
30 public function setUp(): void
32 $this->container
= $this->createMock(ShaarliContainer
::class);
33 $this->controller
= new LogoutController($this->container
);
35 setcookie(LoginManager
::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
38 public function testValidControllerInvoke(): void
40 $request = $this->createMock(Request
::class);
41 $response = new Response();
43 $pageCacheManager = $this->createMock(PageCacheManager
::class);
44 $pageCacheManager->expects(static::once())->method('invalidateCaches');
45 $this->container
->pageCacheManager
= $pageCacheManager;
47 $sessionManager = $this->createMock(SessionManager
::class);
48 $sessionManager->expects(static::once())->method('logout');
49 $this->container
->sessionManager
= $sessionManager;
51 static::assertSame('hi there', $_COOKIE[LoginManager
::$STAY_SIGNED_IN_COOKIE]);
53 $result = $this->controller
->index($request, $response);
55 static::assertInstanceOf(Response
::class, $result);
56 static::assertSame(302, $result->getStatusCode());
57 static::assertContains('./', $result->getHeader('Location'));
58 static::assertSame('false', $_COOKIE[LoginManager
::$STAY_SIGNED_IN_COOKIE]);