]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/LogoutControllerTest.php
Process logout through Slim controller
[github/shaarli/Shaarli.git] / tests / front / controller / LogoutControllerTest.php
CommitLineData
8e47af2b
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
8if (!function_exists('Shaarli\Front\Controller\setcookie')) {
9 function setcookie(string $name, string $value): void {
10 $_COOKIE[$name] = $value;
11 }
12}
13
14use PHPUnit\Framework\TestCase;
15use Shaarli\Container\ShaarliContainer;
16use Shaarli\Render\PageCacheManager;
17use Shaarli\Security\LoginManager;
18use Shaarli\Security\SessionManager;
19use Slim\Http\Request;
20use Slim\Http\Response;
21
22class LogoutControllerTest extends TestCase
23{
24 /** @var ShaarliContainer */
25 protected $container;
26
27 /** @var LogoutController */
28 protected $controller;
29
30 public function setUp(): void
31 {
32 $this->container = $this->createMock(ShaarliContainer::class);
33 $this->controller = new LogoutController($this->container);
34
35 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
36 }
37
38 public function testValidControllerInvoke(): void
39 {
40 $request = $this->createMock(Request::class);
41 $response = new Response();
42
43 $pageCacheManager = $this->createMock(PageCacheManager::class);
44 $pageCacheManager->expects(static::once())->method('invalidateCaches');
45 $this->container->pageCacheManager = $pageCacheManager;
46
47 $sessionManager = $this->createMock(SessionManager::class);
48 $sessionManager->expects(static::once())->method('logout');
49 $this->container->sessionManager = $sessionManager;
50
51 static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
52
53 $result = $this->controller->index($request, $response);
54
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]);
59 }
60}