]>
Commit | Line | Data |
---|---|---|
8e47af2b A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller; | |
6 | ||
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; | |
11 | } | |
12 | } | |
13 | ||
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; | |
21 | ||
22 | class 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 | } |