]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller; | |
6 | ||
7 | use PHPUnit\Framework\TestCase; | |
8 | use Shaarli\Feed\FeedBuilder; | |
9 | use Slim\Http\Request; | |
10 | use Slim\Http\Response; | |
11 | ||
12 | class FeedControllerTest extends TestCase | |
13 | { | |
14 | use FrontControllerMockHelper; | |
15 | ||
16 | /** @var FeedController */ | |
17 | protected $controller; | |
18 | ||
19 | public function setUp(): void | |
20 | { | |
21 | $this->createContainer(); | |
22 | ||
23 | $this->container->feedBuilder = $this->createMock(FeedBuilder::class); | |
24 | ||
25 | $this->controller = new FeedController($this->container); | |
26 | } | |
27 | ||
28 | /** | |
29 | * Feed Controller - RSS default behaviour | |
30 | */ | |
31 | public function testDefaultRssController(): void | |
32 | { | |
33 | $this->createValidContainerMockSet(); | |
34 | ||
35 | $request = $this->createMock(Request::class); | |
36 | $response = new Response(); | |
37 | ||
38 | $this->container->feedBuilder->expects(static::once())->method('setLocale'); | |
39 | $this->container->feedBuilder->expects(static::once())->method('setHideDates')->with(false); | |
40 | $this->container->feedBuilder->expects(static::once())->method('setUsePermalinks')->with(true); | |
41 | ||
42 | // Save RainTPL assigned variables | |
43 | $assignedVariables = []; | |
44 | $this->assignTemplateVars($assignedVariables); | |
45 | ||
46 | $this->container->feedBuilder->method('buildData')->willReturn(['content' => 'data']); | |
47 | ||
48 | // Make sure that PluginManager hook is triggered | |
49 | $this->container->pluginManager | |
50 | ->expects(static::at(0)) | |
51 | ->method('executeHooks') | |
52 | ->willReturnCallback(function (string $hook, array $data, array $param): void { | |
53 | static::assertSame('render_feed', $hook); | |
54 | static::assertSame('data', $data['content']); | |
55 | ||
56 | static::assertArrayHasKey('loggedin', $param); | |
57 | static::assertSame('rss', $param['target']); | |
58 | }) | |
59 | ; | |
60 | ||
61 | $result = $this->controller->rss($request, $response); | |
62 | ||
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']); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Feed Controller - ATOM default behaviour | |
71 | */ | |
72 | public function testDefaultAtomController(): void | |
73 | { | |
74 | $this->createValidContainerMockSet(); | |
75 | ||
76 | $request = $this->createMock(Request::class); | |
77 | $response = new Response(); | |
78 | ||
79 | $this->container->feedBuilder->expects(static::once())->method('setLocale'); | |
80 | $this->container->feedBuilder->expects(static::once())->method('setHideDates')->with(false); | |
81 | $this->container->feedBuilder->expects(static::once())->method('setUsePermalinks')->with(true); | |
82 | ||
83 | // Save RainTPL assigned variables | |
84 | $assignedVariables = []; | |
85 | $this->assignTemplateVars($assignedVariables); | |
86 | ||
87 | $this->container->feedBuilder->method('buildData')->willReturn(['content' => 'data']); | |
88 | ||
89 | // Make sure that PluginManager hook is triggered | |
90 | $this->container->pluginManager | |
91 | ->expects(static::at(0)) | |
92 | ->method('executeHooks') | |
93 | ->willReturnCallback(function (string $hook, array $data, array $param): void { | |
94 | static::assertSame('render_feed', $hook); | |
95 | static::assertSame('data', $data['content']); | |
96 | ||
97 | static::assertArrayHasKey('loggedin', $param); | |
98 | static::assertSame('atom', $param['target']); | |
99 | }) | |
100 | ; | |
101 | ||
102 | $result = $this->controller->atom($request, $response); | |
103 | ||
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']); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Feed Controller - ATOM with parameters | |
112 | */ | |
113 | public function testAtomControllerWithParameters(): void | |
114 | { | |
115 | $this->createValidContainerMockSet(); | |
116 | ||
117 | $request = $this->createMock(Request::class); | |
118 | $request->method('getParams')->willReturn(['parameter' => 'value']); | |
119 | $response = new Response(); | |
120 | ||
121 | // Save RainTPL assigned variables | |
122 | $assignedVariables = []; | |
123 | $this->assignTemplateVars($assignedVariables); | |
124 | ||
125 | $this->container->feedBuilder | |
126 | ->method('buildData') | |
127 | ->with('atom', ['parameter' => 'value']) | |
128 | ->willReturn(['content' => 'data']) | |
129 | ; | |
130 | ||
131 | // Make sure that PluginManager hook is triggered | |
132 | $this->container->pluginManager | |
133 | ->expects(static::at(0)) | |
134 | ->method('executeHooks') | |
135 | ->willReturnCallback(function (string $hook, array $data, array $param): void { | |
136 | static::assertSame('render_feed', $hook); | |
137 | static::assertSame('data', $data['content']); | |
138 | ||
139 | static::assertArrayHasKey('loggedin', $param); | |
140 | static::assertSame('atom', $param['target']); | |
141 | }) | |
142 | ; | |
143 | ||
144 | $result = $this->controller->atom($request, $response); | |
145 | ||
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']); | |
150 | } | |
151 | } |