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