diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-23 21:52:03 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 8e47af2b3620c920116ec056173277c039163ec1 (patch) | |
tree | d77f31f17bf4485f5a3ec0fea895205e9bde0554 /tests/front/controller | |
parent | b0428aa9b02b058b72c40b6e8dc2298d55bf692f (diff) | |
download | Shaarli-8e47af2b3620c920116ec056173277c039163ec1.tar.gz Shaarli-8e47af2b3620c920116ec056173277c039163ec1.tar.zst Shaarli-8e47af2b3620c920116ec056173277c039163ec1.zip |
Process logout through Slim controller
Diffstat (limited to 'tests/front/controller')
-rw-r--r-- | tests/front/controller/LogoutControllerTest.php | 60 |
1 files changed, 60 insertions, 0 deletions
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 | |||
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 | } | ||