3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Feed\FeedBuilder
;
10 use Slim\Http\Response
;
12 class FeedControllerTest
extends TestCase
14 use FrontControllerMockHelper
;
16 /** @var FeedController */
17 protected $controller;
19 public function setUp(): void
21 $this->createContainer();
23 $this->container
->feedBuilder
= $this->createMock(FeedBuilder
::class);
25 $this->controller
= new FeedController($this->container
);
29 * Feed Controller - RSS default behaviour
31 public function testDefaultRssController(): void
33 $request = $this->createMock(Request
::class);
34 $response = new Response();
36 $this->container
->feedBuilder
->expects(static::once())->method('setLocale');
37 $this->container
->feedBuilder
->expects(static::once())->method('setHideDates')->with(false);
38 $this->container
->feedBuilder
->expects(static::once())->method('setUsePermalinks')->with(true);
40 // Save RainTPL assigned variables
41 $assignedVariables = [];
42 $this->assignTemplateVars($assignedVariables);
44 $this->container
->feedBuilder
->method('buildData')->willReturn(['content' => 'data']);
46 // Make sure that PluginManager hook is triggered
47 $this->container
->pluginManager
48 ->expects(static::atLeastOnce())
49 ->method('executeHooks')
50 ->withConsecutive(['render_feed'])
51 ->willReturnCallback(function (string $hook, array $data, array $param): void {
52 if ('render_feed' === $hook) {
53 static::assertSame('data', $data['content']);
55 static::assertArrayHasKey('loggedin', $param);
56 static::assertSame('feed.rss', $param['target']);
61 $result = $this->controller
->rss($request, $response);
63 static::assertSame(200, $result->getStatusCode());
64 static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]);
65 static::assertSame('feed.rss', (string) $result->getBody());
66 static::assertSame('data', $assignedVariables['content']);
70 * Feed Controller - ATOM default behaviour
72 public function testDefaultAtomController(): void
74 $request = $this->createMock(Request
::class);
75 $response = new Response();
77 $this->container
->feedBuilder
->expects(static::once())->method('setLocale');
78 $this->container
->feedBuilder
->expects(static::once())->method('setHideDates')->with(false);
79 $this->container
->feedBuilder
->expects(static::once())->method('setUsePermalinks')->with(true);
81 // Save RainTPL assigned variables
82 $assignedVariables = [];
83 $this->assignTemplateVars($assignedVariables);
85 $this->container
->feedBuilder
->method('buildData')->willReturn(['content' => 'data']);
87 // Make sure that PluginManager hook is triggered
88 $this->container
->pluginManager
89 ->expects(static::atLeastOnce())
90 ->method('executeHooks')
91 ->withConsecutive(['render_feed'])
92 ->willReturnCallback(function (string $hook, array $data, array $param): void {
93 if ('render_feed' === $hook) {
94 static::assertSame('data', $data['content']);
96 static::assertArrayHasKey('loggedin', $param);
97 static::assertSame('feed.atom', $param['target']);
102 $result = $this->controller
->atom($request, $response);
104 static::assertSame(200, $result->getStatusCode());
105 static::assertStringContainsString('application/atom', $result->getHeader('Content-Type')[0]);
106 static::assertSame('feed.atom', (string) $result->getBody());
107 static::assertSame('data', $assignedVariables['content']);
111 * Feed Controller - ATOM with parameters
113 public function testAtomControllerWithParameters(): void
115 $request = $this->createMock(Request
::class);
116 $request->method('getParams')->willReturn(['parameter' => 'value']);
117 $response = new Response();
119 // Save RainTPL assigned variables
120 $assignedVariables = [];
121 $this->assignTemplateVars($assignedVariables);
123 $this->container
->feedBuilder
124 ->method('buildData')
125 ->with('atom', ['parameter' => 'value'])
126 ->willReturn(['content' => 'data'])
129 // Make sure that PluginManager hook is triggered
130 $this->container
->pluginManager
131 ->expects(static::atLeastOnce())
132 ->method('executeHooks')
133 ->withConsecutive(['render_feed'])
134 ->willReturnCallback(function (string $hook, array $data, array $param): void {
135 if ('render_feed' === $hook) {
136 static::assertSame('data', $data['content']);
138 static::assertArrayHasKey('loggedin', $param);
139 static::assertSame('feed.atom', $param['target']);
144 $result = $this->controller
->atom($request, $response);
146 static::assertSame(200, $result->getStatusCode());
147 static::assertStringContainsString('application/atom', $result->getHeader('Content-Type')[0]);
148 static::assertSame('feed.atom', (string) $result->getBody());
149 static::assertSame('data', $assignedVariables['content']);