aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-23 21:52:03 +0100
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit8e47af2b3620c920116ec056173277c039163ec1 (patch)
treed77f31f17bf4485f5a3ec0fea895205e9bde0554 /tests
parentb0428aa9b02b058b72c40b6e8dc2298d55bf692f (diff)
downloadShaarli-8e47af2b3620c920116ec056173277c039163ec1.tar.gz
Shaarli-8e47af2b3620c920116ec056173277c039163ec1.tar.zst
Shaarli-8e47af2b3620c920116ec056173277c039163ec1.zip
Process logout through Slim controller
Diffstat (limited to 'tests')
-rw-r--r--tests/container/ContainerBuilderTest.php10
-rw-r--r--tests/front/controller/LogoutControllerTest.php60
2 files changed, 69 insertions, 1 deletions
diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php
index cc2eb37f..65647249 100644
--- a/tests/container/ContainerBuilderTest.php
+++ b/tests/container/ContainerBuilderTest.php
@@ -10,6 +10,7 @@ use Shaarli\Config\ConfigManager;
10use Shaarli\Formatter\FormatterFactory; 10use Shaarli\Formatter\FormatterFactory;
11use Shaarli\History; 11use Shaarli\History;
12use Shaarli\Render\PageBuilder; 12use Shaarli\Render\PageBuilder;
13use Shaarli\Render\PageCacheManager;
13use Shaarli\Security\LoginManager; 14use Shaarli\Security\LoginManager;
14use Shaarli\Security\SessionManager; 15use Shaarli\Security\SessionManager;
15 16
@@ -35,7 +36,12 @@ class ContainerBuilderTest extends TestCase
35 $this->loginManager = $this->createMock(LoginManager::class); 36 $this->loginManager = $this->createMock(LoginManager::class);
36 $this->loginManager->method('isLoggedIn')->willReturn(true); 37 $this->loginManager->method('isLoggedIn')->willReturn(true);
37 38
38 $this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager); 39 $this->containerBuilder = new ContainerBuilder(
40 $this->conf,
41 $this->sessionManager,
42 $this->loginManager,
43 'UT web path'
44 );
39 } 45 }
40 46
41 public function testBuildContainer(): void 47 public function testBuildContainer(): void
@@ -45,9 +51,11 @@ class ContainerBuilderTest extends TestCase
45 static::assertInstanceOf(ConfigManager::class, $container->conf); 51 static::assertInstanceOf(ConfigManager::class, $container->conf);
46 static::assertInstanceOf(SessionManager::class, $container->sessionManager); 52 static::assertInstanceOf(SessionManager::class, $container->sessionManager);
47 static::assertInstanceOf(LoginManager::class, $container->loginManager); 53 static::assertInstanceOf(LoginManager::class, $container->loginManager);
54 static::assertSame('UT web path', $container->webPath);
48 static::assertInstanceOf(History::class, $container->history); 55 static::assertInstanceOf(History::class, $container->history);
49 static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); 56 static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService);
50 static::assertInstanceOf(PageBuilder::class, $container->pageBuilder); 57 static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
51 static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory); 58 static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory);
59 static::assertInstanceOf(PageCacheManager::class, $container->pageCacheManager);
52 } 60 }
53} 61}
diff --git a/tests/front/controller/LogoutControllerTest.php b/tests/front/controller/LogoutControllerTest.php
new file mode 100644
index 00000000..d9ca1c25
--- /dev/null
+++ b/tests/front/controller/LogoutControllerTest.php
@@ -0,0 +1,60 @@
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}