]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/helper/DailyPageHelperTest.php
Daily RSS - Remove relative description (today, yesterday)
[github/shaarli/Shaarli.git] / tests / helper / DailyPageHelperTest.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Helper;
6
7 use Shaarli\Bookmark\Bookmark;
8 use Shaarli\TestCase;
9 use Slim\Http\Request;
10
11 class DailyPageHelperTest extends TestCase
12 {
13 /**
14 * @dataProvider getRequestedTypes
15 */
16 public function testExtractRequestedType(array $queryParams, string $expectedType): void
17 {
18 $request = $this->createMock(Request::class);
19 $request->method('getQueryParam')->willReturnCallback(function ($key) use ($queryParams): ?string {
20 return $queryParams[$key] ?? null;
21 });
22
23 $type = DailyPageHelper::extractRequestedType($request);
24
25 static::assertSame($type, $expectedType);
26 }
27
28 /**
29 * @dataProvider getRequestedDateTimes
30 */
31 public function testExtractRequestedDateTime(
32 string $type,
33 string $input,
34 ?Bookmark $bookmark,
35 \DateTimeInterface $expectedDateTime,
36 string $compareFormat = 'Ymd'
37 ): void {
38 $dateTime = DailyPageHelper::extractRequestedDateTime($type, $input, $bookmark);
39
40 static::assertSame($dateTime->format($compareFormat), $expectedDateTime->format($compareFormat));
41 }
42
43 public function testExtractRequestedDateTimeExceptionUnknownType(): void
44 {
45 $this->expectException(\Exception::class);
46 $this->expectExceptionMessage('Unsupported daily format type');
47
48 DailyPageHelper::extractRequestedDateTime('nope', null, null);
49 }
50
51 /**
52 * @dataProvider getFormatsByType
53 */
54 public function testGetFormatByType(string $type, string $expectedFormat): void
55 {
56 $format = DailyPageHelper::getFormatByType($type);
57
58 static::assertSame($expectedFormat, $format);
59 }
60
61 public function testGetFormatByTypeExceptionUnknownType(): void
62 {
63 $this->expectException(\Exception::class);
64 $this->expectExceptionMessage('Unsupported daily format type');
65
66 DailyPageHelper::getFormatByType('nope');
67 }
68
69 /**
70 * @dataProvider getStartDatesByType
71 */
72 public function testGetStartDatesByType(
73 string $type,
74 \DateTimeImmutable $dateTime,
75 \DateTimeInterface $expectedDateTime
76 ): void {
77 $startDateTime = DailyPageHelper::getStartDateTimeByType($type, $dateTime);
78
79 static::assertEquals($expectedDateTime, $startDateTime);
80 }
81
82 public function testGetStartDatesByTypeExceptionUnknownType(): void
83 {
84 $this->expectException(\Exception::class);
85 $this->expectExceptionMessage('Unsupported daily format type');
86
87 DailyPageHelper::getStartDateTimeByType('nope', new \DateTimeImmutable());
88 }
89
90 /**
91 * @dataProvider getEndDatesByType
92 */
93 public function testGetEndDatesByType(
94 string $type,
95 \DateTimeImmutable $dateTime,
96 \DateTimeInterface $expectedDateTime
97 ): void {
98 $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dateTime);
99
100 static::assertEquals($expectedDateTime, $endDateTime);
101 }
102
103 public function testGetEndDatesByTypeExceptionUnknownType(): void
104 {
105 $this->expectException(\Exception::class);
106 $this->expectExceptionMessage('Unsupported daily format type');
107
108 DailyPageHelper::getEndDateTimeByType('nope', new \DateTimeImmutable());
109 }
110
111 /**
112 * @dataProvider getDescriptionsByType
113 */
114 public function testGeDescriptionsByType(
115 string $type,
116 \DateTimeImmutable $dateTime,
117 string $expectedDescription
118 ): void {
119 $description = DailyPageHelper::getDescriptionByType($type, $dateTime);
120
121 static::assertEquals($expectedDescription, $description);
122 }
123
124 /**
125 * @dataProvider getDescriptionsByTypeNotIncludeRelative
126 */
127 public function testGeDescriptionsByTypeNotIncludeRelative(
128 string $type,
129 \DateTimeImmutable $dateTime,
130 string $expectedDescription
131 ): void {
132 $description = DailyPageHelper::getDescriptionByType($type, $dateTime, false);
133
134 static::assertEquals($expectedDescription, $description);
135 }
136
137 public function getDescriptionByTypeExceptionUnknownType(): void
138 {
139 $this->expectException(\Exception::class);
140 $this->expectExceptionMessage('Unsupported daily format type');
141
142 DailyPageHelper::getDescriptionByType('nope', new \DateTimeImmutable());
143 }
144
145 /**
146 * @dataProvider getRssLengthsByType
147 */
148 public function testGeRssLengthsByType(string $type): void {
149 $length = DailyPageHelper::getRssLengthByType($type);
150
151 static::assertIsInt($length);
152 }
153
154 public function testGeRssLengthsByTypeExceptionUnknownType(): void
155 {
156 $this->expectException(\Exception::class);
157 $this->expectExceptionMessage('Unsupported daily format type');
158
159 DailyPageHelper::getRssLengthByType('nope');
160 }
161
162 /**
163 * Data provider for testExtractRequestedType() test method.
164 */
165 public function getRequestedTypes(): array
166 {
167 return [
168 [['month' => null], DailyPageHelper::DAY],
169 [['month' => ''], DailyPageHelper::MONTH],
170 [['month' => 'content'], DailyPageHelper::MONTH],
171 [['week' => null], DailyPageHelper::DAY],
172 [['week' => ''], DailyPageHelper::WEEK],
173 [['week' => 'content'], DailyPageHelper::WEEK],
174 [['day' => null], DailyPageHelper::DAY],
175 [['day' => ''], DailyPageHelper::DAY],
176 [['day' => 'content'], DailyPageHelper::DAY],
177 ];
178 }
179
180 /**
181 * Data provider for testExtractRequestedDateTime() test method.
182 */
183 public function getRequestedDateTimes(): array
184 {
185 return [
186 [DailyPageHelper::DAY, '20201013', null, new \DateTime('2020-10-13')],
187 [
188 DailyPageHelper::DAY,
189 '',
190 (new Bookmark())->setCreated($date = new \DateTime('2020-10-13 12:05:31')),
191 $date,
192 ],
193 [DailyPageHelper::DAY, '', null, new \DateTime()],
194 [DailyPageHelper::WEEK, '202030', null, new \DateTime('2020-07-20')],
195 [
196 DailyPageHelper::WEEK,
197 '',
198 (new Bookmark())->setCreated($date = new \DateTime('2020-10-13 12:05:31')),
199 new \DateTime('2020-10-13'),
200 ],
201 [DailyPageHelper::WEEK, '', null, new \DateTime(), 'Ym'],
202 [DailyPageHelper::MONTH, '202008', null, new \DateTime('2020-08-01'), 'Ym'],
203 [
204 DailyPageHelper::MONTH,
205 '',
206 (new Bookmark())->setCreated($date = new \DateTime('2020-10-13 12:05:31')),
207 new \DateTime('2020-10-13'),
208 'Ym'
209 ],
210 [DailyPageHelper::MONTH, '', null, new \DateTime(), 'Ym'],
211 ];
212 }
213
214 /**
215 * Data provider for testGetFormatByType() test method.
216 */
217 public function getFormatsByType(): array
218 {
219 return [
220 [DailyPageHelper::DAY, 'Ymd'],
221 [DailyPageHelper::WEEK, 'YW'],
222 [DailyPageHelper::MONTH, 'Ym'],
223 ];
224 }
225
226 /**
227 * Data provider for testGetStartDatesByType() test method.
228 */
229 public function getStartDatesByType(): array
230 {
231 return [
232 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 00:00:00')],
233 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-05 00:00:00')],
234 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-01 00:00:00')],
235 ];
236 }
237
238 /**
239 * Data provider for testGetEndDatesByType() test method.
240 */
241 public function getEndDatesByType(): array
242 {
243 return [
244 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 23:59:59')],
245 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-11 23:59:59')],
246 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-31 23:59:59')],
247 ];
248 }
249
250 /**
251 * Data provider for testGetDescriptionsByType() test method.
252 */
253 public function getDescriptionsByType(): array
254 {
255 return [
256 [DailyPageHelper::DAY, $date = new \DateTimeImmutable(), 'Today - ' . $date->format('F j, Y')],
257 [DailyPageHelper::DAY, $date = new \DateTimeImmutable('-1 day'), 'Yesterday - ' . $date->format('F j, Y')],
258 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'],
259 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'],
260 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
261 ];
262 }
263
264 /**
265 * Data provider for testGeDescriptionsByTypeNotIncludeRelative() test method.
266 */
267 public function getDescriptionsByTypeNotIncludeRelative(): array
268 {
269 return [
270 [DailyPageHelper::DAY, $date = new \DateTimeImmutable(), $date->format('F j, Y')],
271 [DailyPageHelper::DAY, $date = new \DateTimeImmutable('-1 day'), $date->format('F j, Y')],
272 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'],
273 [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'],
274 [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
275 ];
276 }
277
278 /**
279 * Data provider for testGetDescriptionsByType() test method.
280 */
281 public function getRssLengthsByType(): array
282 {
283 return [
284 [DailyPageHelper::DAY],
285 [DailyPageHelper::WEEK],
286 [DailyPageHelper::MONTH],
287 ];
288 }
289 }