diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/container/ContainerBuilderTest.php | 10 | ||||
-rw-r--r-- | tests/front/controller/LogoutControllerTest.php | 60 |
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; | |||
10 | use Shaarli\Formatter\FormatterFactory; | 10 | use Shaarli\Formatter\FormatterFactory; |
11 | use Shaarli\History; | 11 | use Shaarli\History; |
12 | use Shaarli\Render\PageBuilder; | 12 | use Shaarli\Render\PageBuilder; |
13 | use Shaarli\Render\PageCacheManager; | ||
13 | use Shaarli\Security\LoginManager; | 14 | use Shaarli\Security\LoginManager; |
14 | use Shaarli\Security\SessionManager; | 15 | use 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 | |||
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 | } | ||