diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-26 11:15:15 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 485b168a9677d160b0c0426e4f282b9bd0c632c1 (patch) | |
tree | c7c21ea01096bfe28622b3a23c5c76d1cb2435fa /tests | |
parent | bee33239ed444f9724422fe5234cd79997500519 (diff) | |
download | Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.tar.gz Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.tar.zst Shaarli-485b168a9677d160b0c0426e4f282b9bd0c632c1.zip |
Process picwall rendering through Slim controller + UT
Diffstat (limited to 'tests')
-rw-r--r-- | tests/container/ContainerBuilderTest.php | 4 | ||||
-rw-r--r-- | tests/front/controller/PictureWallControllerTest.php | 180 |
2 files changed, 184 insertions, 0 deletions
diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php index 9b97ed6d..cc2eb37f 100644 --- a/tests/container/ContainerBuilderTest.php +++ b/tests/container/ContainerBuilderTest.php | |||
@@ -7,6 +7,7 @@ namespace Shaarli\Container; | |||
7 | use PHPUnit\Framework\TestCase; | 7 | use PHPUnit\Framework\TestCase; |
8 | use Shaarli\Bookmark\BookmarkServiceInterface; | 8 | use Shaarli\Bookmark\BookmarkServiceInterface; |
9 | use Shaarli\Config\ConfigManager; | 9 | use Shaarli\Config\ConfigManager; |
10 | use Shaarli\Formatter\FormatterFactory; | ||
10 | use Shaarli\History; | 11 | use Shaarli\History; |
11 | use Shaarli\Render\PageBuilder; | 12 | use Shaarli\Render\PageBuilder; |
12 | use Shaarli\Security\LoginManager; | 13 | use Shaarli\Security\LoginManager; |
@@ -30,7 +31,9 @@ class ContainerBuilderTest extends TestCase | |||
30 | { | 31 | { |
31 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | 32 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
32 | $this->sessionManager = $this->createMock(SessionManager::class); | 33 | $this->sessionManager = $this->createMock(SessionManager::class); |
34 | |||
33 | $this->loginManager = $this->createMock(LoginManager::class); | 35 | $this->loginManager = $this->createMock(LoginManager::class); |
36 | $this->loginManager->method('isLoggedIn')->willReturn(true); | ||
34 | 37 | ||
35 | $this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager); | 38 | $this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager); |
36 | } | 39 | } |
@@ -45,5 +48,6 @@ class ContainerBuilderTest extends TestCase | |||
45 | static::assertInstanceOf(History::class, $container->history); | 48 | static::assertInstanceOf(History::class, $container->history); |
46 | static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); | 49 | static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); |
47 | static::assertInstanceOf(PageBuilder::class, $container->pageBuilder); | 50 | static::assertInstanceOf(PageBuilder::class, $container->pageBuilder); |
51 | static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory); | ||
48 | } | 52 | } |
49 | } | 53 | } |
diff --git a/tests/front/controller/PictureWallControllerTest.php b/tests/front/controller/PictureWallControllerTest.php new file mode 100644 index 00000000..63802abd --- /dev/null +++ b/tests/front/controller/PictureWallControllerTest.php | |||
@@ -0,0 +1,180 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Bookmark\Bookmark; | ||
9 | use Shaarli\Bookmark\BookmarkServiceInterface; | ||
10 | use Shaarli\Config\ConfigManager; | ||
11 | use Shaarli\Container\ShaarliContainer; | ||
12 | use Shaarli\Formatter\BookmarkFormatter; | ||
13 | use Shaarli\Formatter\BookmarkRawFormatter; | ||
14 | use Shaarli\Formatter\FormatterFactory; | ||
15 | use Shaarli\Front\Exception\ThumbnailsDisabledException; | ||
16 | use Shaarli\Plugin\PluginManager; | ||
17 | use Shaarli\Render\PageBuilder; | ||
18 | use Shaarli\Security\LoginManager; | ||
19 | use Shaarli\Thumbnailer; | ||
20 | use Slim\Http\Request; | ||
21 | use Slim\Http\Response; | ||
22 | |||
23 | class PictureWallControllerTest extends TestCase | ||
24 | { | ||
25 | /** @var ShaarliContainer */ | ||
26 | protected $container; | ||
27 | |||
28 | /** @var PictureWallController */ | ||
29 | protected $controller; | ||
30 | |||
31 | public function setUp(): void | ||
32 | { | ||
33 | $this->container = $this->createMock(ShaarliContainer::class); | ||
34 | $this->controller = new PictureWallController($this->container); | ||
35 | } | ||
36 | |||
37 | public function testValidControllerInvokeDefault(): void | ||
38 | { | ||
39 | $this->createValidContainerMockSet(); | ||
40 | |||
41 | $request = $this->createMock(Request::class); | ||
42 | $request->expects(static::once())->method('getQueryParams')->willReturn([]); | ||
43 | $response = new Response(); | ||
44 | |||
45 | // ConfigManager: thumbnails are enabled | ||
46 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
47 | if ($parameter === 'thumbnails.mode') { | ||
48 | return Thumbnailer::MODE_COMMON; | ||
49 | } | ||
50 | |||
51 | return $default; | ||
52 | }); | ||
53 | |||
54 | // Save RainTPL assigned variables | ||
55 | $assignedVariables = []; | ||
56 | $this->container->pageBuilder | ||
57 | ->expects(static::atLeastOnce()) | ||
58 | ->method('assign') | ||
59 | ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { | ||
60 | $assignedVariables[$key] = $value; | ||
61 | |||
62 | return $this; | ||
63 | }) | ||
64 | ; | ||
65 | |||
66 | // Links dataset: 2 links with thumbnails | ||
67 | $this->container->bookmarkService | ||
68 | ->expects(static::once()) | ||
69 | ->method('search') | ||
70 | ->willReturnCallback(function (array $parameters, ?string $visibility): array { | ||
71 | // Visibility is set through the container, not the call | ||
72 | static::assertNull($visibility); | ||
73 | |||
74 | // No query parameters | ||
75 | if (count($parameters) === 0) { | ||
76 | return [ | ||
77 | (new Bookmark())->setId(1)->setUrl('http://url.tld')->setThumbnail('thumb1'), | ||
78 | (new Bookmark())->setId(2)->setUrl('http://url2.tld'), | ||
79 | (new Bookmark())->setId(3)->setUrl('http://url3.tld')->setThumbnail('thumb2'), | ||
80 | ]; | ||
81 | } | ||
82 | }) | ||
83 | ; | ||
84 | |||
85 | // Make sure that PluginManager hook is triggered | ||
86 | $this->container->pluginManager | ||
87 | ->expects(static::at(0)) | ||
88 | ->method('executeHooks') | ||
89 | ->willReturnCallback(function (string $hook, array $data, array $param): array { | ||
90 | static::assertSame('render_picwall', $hook); | ||
91 | static::assertArrayHasKey('linksToDisplay', $data); | ||
92 | static::assertCount(2, $data['linksToDisplay']); | ||
93 | static::assertSame(1, $data['linksToDisplay'][0]['id']); | ||
94 | static::assertSame(3, $data['linksToDisplay'][1]['id']); | ||
95 | static::assertArrayHasKey('loggedin', $param); | ||
96 | |||
97 | return $data; | ||
98 | }); | ||
99 | |||
100 | $result = $this->controller->index($request, $response); | ||
101 | |||
102 | static::assertSame(200, $result->getStatusCode()); | ||
103 | static::assertSame('picwall', (string) $result->getBody()); | ||
104 | static::assertSame('Picture wall - Shaarli', $assignedVariables['pagetitle']); | ||
105 | static::assertCount(2, $assignedVariables['linksToDisplay']); | ||
106 | |||
107 | $link = $assignedVariables['linksToDisplay'][0]; | ||
108 | |||
109 | static::assertSame(1, $link['id']); | ||
110 | static::assertSame('http://url.tld', $link['url']); | ||
111 | static::assertSame('thumb1', $link['thumbnail']); | ||
112 | |||
113 | $link = $assignedVariables['linksToDisplay'][1]; | ||
114 | |||
115 | static::assertSame(3, $link['id']); | ||
116 | static::assertSame('http://url3.tld', $link['url']); | ||
117 | static::assertSame('thumb2', $link['thumbnail']); | ||
118 | } | ||
119 | |||
120 | public function testControllerWithThumbnailsDisabled(): void | ||
121 | { | ||
122 | $this->expectException(ThumbnailsDisabledException::class); | ||
123 | |||
124 | $this->createValidContainerMockSet(); | ||
125 | |||
126 | $request = $this->createMock(Request::class); | ||
127 | $response = new Response(); | ||
128 | |||
129 | // ConfigManager: thumbnails are disabled | ||
130 | $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
131 | if ($parameter === 'thumbnails.mode') { | ||
132 | return Thumbnailer::MODE_NONE; | ||
133 | } | ||
134 | |||
135 | return $default; | ||
136 | }); | ||
137 | |||
138 | $this->controller->index($request, $response); | ||
139 | } | ||
140 | |||
141 | protected function createValidContainerMockSet(): void | ||
142 | { | ||
143 | $loginManager = $this->createMock(LoginManager::class); | ||
144 | $this->container->loginManager = $loginManager; | ||
145 | |||
146 | // Config | ||
147 | $conf = $this->createMock(ConfigManager::class); | ||
148 | $this->container->conf = $conf; | ||
149 | |||
150 | // PageBuilder | ||
151 | $pageBuilder = $this->createMock(PageBuilder::class); | ||
152 | $pageBuilder | ||
153 | ->method('render') | ||
154 | ->willReturnCallback(function (string $template): string { | ||
155 | return $template; | ||
156 | }) | ||
157 | ; | ||
158 | $this->container->pageBuilder = $pageBuilder; | ||
159 | |||
160 | // Plugin Manager | ||
161 | $pluginManager = $this->createMock(PluginManager::class); | ||
162 | $this->container->pluginManager = $pluginManager; | ||
163 | |||
164 | // BookmarkService | ||
165 | $bookmarkService = $this->createMock(BookmarkServiceInterface::class); | ||
166 | $this->container->bookmarkService = $bookmarkService; | ||
167 | |||
168 | // Formatter | ||
169 | $formatterFactory = $this->createMock(FormatterFactory::class); | ||
170 | $formatterFactory | ||
171 | ->method('getFormatter') | ||
172 | ->willReturnCallback(function (string $type): BookmarkFormatter { | ||
173 | if ($type === 'raw') { | ||
174 | return new BookmarkRawFormatter($this->container->conf, true); | ||
175 | } | ||
176 | }) | ||
177 | ; | ||
178 | $this->container->formatterFactory = $formatterFactory; | ||
179 | } | ||
180 | } | ||