aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller')
-rw-r--r--tests/front/controller/FeedControllerTest.php219
1 files changed, 219 insertions, 0 deletions
diff --git a/tests/front/controller/FeedControllerTest.php b/tests/front/controller/FeedControllerTest.php
new file mode 100644
index 00000000..d4cc5536
--- /dev/null
+++ b/tests/front/controller/FeedControllerTest.php
@@ -0,0 +1,219 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Container\ShaarliContainer;
11use Shaarli\Feed\FeedBuilder;
12use Shaarli\Formatter\FormatterFactory;
13use Shaarli\Plugin\PluginManager;
14use Shaarli\Render\PageBuilder;
15use Shaarli\Render\PageCacheManager;
16use Shaarli\Security\LoginManager;
17use Slim\Http\Request;
18use Slim\Http\Response;
19
20class FeedControllerTest extends TestCase
21{
22 /** @var ShaarliContainer */
23 protected $container;
24
25 /** @var FeedController */
26 protected $controller;
27
28 public function setUp(): void
29 {
30 $this->container = $this->createMock(ShaarliContainer::class);
31 $this->controller = new FeedController($this->container);
32 }
33
34 /**
35 * Feed Controller - RSS default behaviour
36 */
37 public function testDefaultRssController(): void
38 {
39 $this->createValidContainerMockSet();
40
41 $request = $this->createMock(Request::class);
42 $response = new Response();
43
44 $this->container->feedBuilder->expects(static::once())->method('setLocale');
45 $this->container->feedBuilder->expects(static::once())->method('setHideDates')->with(false);
46 $this->container->feedBuilder->expects(static::once())->method('setUsePermalinks')->with(true);
47
48 // Save RainTPL assigned variables
49 $assignedVariables = [];
50 $this->assignTemplateVars($assignedVariables);
51
52 $this->container->feedBuilder->method('buildData')->willReturn(['content' => 'data']);
53
54 // Make sure that PluginManager hook is triggered
55 $this->container->pluginManager
56 ->expects(static::at(0))
57 ->method('executeHooks')
58 ->willReturnCallback(function (string $hook, array $data, array $param): void {
59 static::assertSame('render_feed', $hook);
60 static::assertSame('data', $data['content']);
61
62 static::assertArrayHasKey('loggedin', $param);
63 static::assertSame('rss', $param['target']);
64 })
65 ;
66
67 $result = $this->controller->rss($request, $response);
68
69 static::assertSame(200, $result->getStatusCode());
70 static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]);
71 static::assertSame('feed.rss', (string) $result->getBody());
72 static::assertSame('data', $assignedVariables['content']);
73 }
74
75 /**
76 * Feed Controller - ATOM default behaviour
77 */
78 public function testDefaultAtomController(): void
79 {
80 $this->createValidContainerMockSet();
81
82 $request = $this->createMock(Request::class);
83 $response = new Response();
84
85 $this->container->feedBuilder->expects(static::once())->method('setLocale');
86 $this->container->feedBuilder->expects(static::once())->method('setHideDates')->with(false);
87 $this->container->feedBuilder->expects(static::once())->method('setUsePermalinks')->with(true);
88
89 // Save RainTPL assigned variables
90 $assignedVariables = [];
91 $this->assignTemplateVars($assignedVariables);
92
93 $this->container->feedBuilder->method('buildData')->willReturn(['content' => 'data']);
94
95 // Make sure that PluginManager hook is triggered
96 $this->container->pluginManager
97 ->expects(static::at(0))
98 ->method('executeHooks')
99 ->willReturnCallback(function (string $hook, array $data, array $param): void {
100 static::assertSame('render_feed', $hook);
101 static::assertSame('data', $data['content']);
102
103 static::assertArrayHasKey('loggedin', $param);
104 static::assertSame('atom', $param['target']);
105 })
106 ;
107
108 $result = $this->controller->atom($request, $response);
109
110 static::assertSame(200, $result->getStatusCode());
111 static::assertStringContainsString('application/atom', $result->getHeader('Content-Type')[0]);
112 static::assertSame('feed.atom', (string) $result->getBody());
113 static::assertSame('data', $assignedVariables['content']);
114 }
115
116 /**
117 * Feed Controller - ATOM with parameters
118 */
119 public function testAtomControllerWithParameters(): void
120 {
121 $this->createValidContainerMockSet();
122
123 $request = $this->createMock(Request::class);
124 $request->method('getParams')->willReturn(['parameter' => 'value']);
125 $response = new Response();
126
127 // Save RainTPL assigned variables
128 $assignedVariables = [];
129 $this->assignTemplateVars($assignedVariables);
130
131 $this->container->feedBuilder
132 ->method('buildData')
133 ->with('atom', ['parameter' => 'value'])
134 ->willReturn(['content' => 'data'])
135 ;
136
137 // Make sure that PluginManager hook is triggered
138 $this->container->pluginManager
139 ->expects(static::at(0))
140 ->method('executeHooks')
141 ->willReturnCallback(function (string $hook, array $data, array $param): void {
142 static::assertSame('render_feed', $hook);
143 static::assertSame('data', $data['content']);
144
145 static::assertArrayHasKey('loggedin', $param);
146 static::assertSame('atom', $param['target']);
147 })
148 ;
149
150 $result = $this->controller->atom($request, $response);
151
152 static::assertSame(200, $result->getStatusCode());
153 static::assertStringContainsString('application/atom', $result->getHeader('Content-Type')[0]);
154 static::assertSame('feed.atom', (string) $result->getBody());
155 static::assertSame('data', $assignedVariables['content']);
156 }
157
158 protected function createValidContainerMockSet(): void
159 {
160 $loginManager = $this->createMock(LoginManager::class);
161 $this->container->loginManager = $loginManager;
162
163 // Config
164 $conf = $this->createMock(ConfigManager::class);
165 $this->container->conf = $conf;
166 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
167 return $default;
168 });
169
170 // PageBuilder
171 $pageBuilder = $this->createMock(PageBuilder::class);
172 $pageBuilder
173 ->method('render')
174 ->willReturnCallback(function (string $template): string {
175 return $template;
176 })
177 ;
178 $this->container->pageBuilder = $pageBuilder;
179
180 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
181 $this->container->bookmarkService = $bookmarkService;
182
183 // Plugin Manager
184 $pluginManager = $this->createMock(PluginManager::class);
185 $this->container->pluginManager = $pluginManager;
186
187 // Formatter
188 $formatterFactory = $this->createMock(FormatterFactory::class);
189 $this->container->formatterFactory = $formatterFactory;
190
191 // CacheManager
192 $pageCacheManager = $this->createMock(PageCacheManager::class);
193 $this->container->pageCacheManager = $pageCacheManager;
194
195 // FeedBuilder
196 $feedBuilder = $this->createMock(FeedBuilder::class);
197 $this->container->feedBuilder = $feedBuilder;
198
199 // $_SERVER
200 $this->container->environment = [
201 'SERVER_NAME' => 'shaarli',
202 'SERVER_PORT' => '80',
203 'REQUEST_URI' => '/daily-rss',
204 ];
205 }
206
207 protected function assignTemplateVars(array &$variables): void
208 {
209 $this->container->pageBuilder
210 ->expects(static::atLeastOnce())
211 ->method('assign')
212 ->willReturnCallback(function ($key, $value) use (&$variables) {
213 $variables[$key] = $value;
214
215 return $this;
216 })
217 ;
218 }
219}