diff options
Diffstat (limited to 'tests/helper/DailyPageHelperTest.php')
-rw-r--r-- | tests/helper/DailyPageHelperTest.php | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/tests/helper/DailyPageHelperTest.php b/tests/helper/DailyPageHelperTest.php new file mode 100644 index 00000000..5255b7b1 --- /dev/null +++ b/tests/helper/DailyPageHelperTest.php | |||
@@ -0,0 +1,262 @@ | |||
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 | public function getDescriptionByTypeExceptionUnknownType(): void | ||
125 | { | ||
126 | $this->expectException(\Exception::class); | ||
127 | $this->expectExceptionMessage('Unsupported daily format type'); | ||
128 | |||
129 | DailyPageHelper::getDescriptionByType('nope', new \DateTimeImmutable()); | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * @dataProvider getRssLengthsByType | ||
134 | */ | ||
135 | public function testGeRssLengthsByType(string $type): void { | ||
136 | $length = DailyPageHelper::getRssLengthByType($type); | ||
137 | |||
138 | static::assertIsInt($length); | ||
139 | } | ||
140 | |||
141 | public function testGeRssLengthsByTypeExceptionUnknownType(): void | ||
142 | { | ||
143 | $this->expectException(\Exception::class); | ||
144 | $this->expectExceptionMessage('Unsupported daily format type'); | ||
145 | |||
146 | DailyPageHelper::getRssLengthByType('nope'); | ||
147 | } | ||
148 | |||
149 | /** | ||
150 | * Data provider for testExtractRequestedType() test method. | ||
151 | */ | ||
152 | public function getRequestedTypes(): array | ||
153 | { | ||
154 | return [ | ||
155 | [['month' => null], DailyPageHelper::DAY], | ||
156 | [['month' => ''], DailyPageHelper::MONTH], | ||
157 | [['month' => 'content'], DailyPageHelper::MONTH], | ||
158 | [['week' => null], DailyPageHelper::DAY], | ||
159 | [['week' => ''], DailyPageHelper::WEEK], | ||
160 | [['week' => 'content'], DailyPageHelper::WEEK], | ||
161 | [['day' => null], DailyPageHelper::DAY], | ||
162 | [['day' => ''], DailyPageHelper::DAY], | ||
163 | [['day' => 'content'], DailyPageHelper::DAY], | ||
164 | ]; | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * Data provider for testExtractRequestedDateTime() test method. | ||
169 | */ | ||
170 | public function getRequestedDateTimes(): array | ||
171 | { | ||
172 | return [ | ||
173 | [DailyPageHelper::DAY, '20201013', null, new \DateTime('2020-10-13')], | ||
174 | [ | ||
175 | DailyPageHelper::DAY, | ||
176 | '', | ||
177 | (new Bookmark())->setCreated($date = new \DateTime('2020-10-13 12:05:31')), | ||
178 | $date, | ||
179 | ], | ||
180 | [DailyPageHelper::DAY, '', null, new \DateTime()], | ||
181 | [DailyPageHelper::WEEK, '202030', null, new \DateTime('2020-07-20')], | ||
182 | [ | ||
183 | DailyPageHelper::WEEK, | ||
184 | '', | ||
185 | (new Bookmark())->setCreated($date = new \DateTime('2020-10-13 12:05:31')), | ||
186 | new \DateTime('2020-10-13'), | ||
187 | ], | ||
188 | [DailyPageHelper::WEEK, '', null, new \DateTime(), 'Ym'], | ||
189 | [DailyPageHelper::MONTH, '202008', null, new \DateTime('2020-08-01'), 'Ym'], | ||
190 | [ | ||
191 | DailyPageHelper::MONTH, | ||
192 | '', | ||
193 | (new Bookmark())->setCreated($date = new \DateTime('2020-10-13 12:05:31')), | ||
194 | new \DateTime('2020-10-13'), | ||
195 | 'Ym' | ||
196 | ], | ||
197 | [DailyPageHelper::MONTH, '', null, new \DateTime(), 'Ym'], | ||
198 | ]; | ||
199 | } | ||
200 | |||
201 | /** | ||
202 | * Data provider for testGetFormatByType() test method. | ||
203 | */ | ||
204 | public function getFormatsByType(): array | ||
205 | { | ||
206 | return [ | ||
207 | [DailyPageHelper::DAY, 'Ymd'], | ||
208 | [DailyPageHelper::WEEK, 'YW'], | ||
209 | [DailyPageHelper::MONTH, 'Ym'], | ||
210 | ]; | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * Data provider for testGetStartDatesByType() test method. | ||
215 | */ | ||
216 | public function getStartDatesByType(): array | ||
217 | { | ||
218 | return [ | ||
219 | [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 00:00:00')], | ||
220 | [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-05 00:00:00')], | ||
221 | [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-01 00:00:00')], | ||
222 | ]; | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * Data provider for testGetEndDatesByType() test method. | ||
227 | */ | ||
228 | public function getEndDatesByType(): array | ||
229 | { | ||
230 | return [ | ||
231 | [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 23:59:59')], | ||
232 | [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-11 23:59:59')], | ||
233 | [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-31 23:59:59')], | ||
234 | ]; | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * Data provider for testGetDescriptionsByType() test method. | ||
239 | */ | ||
240 | public function getDescriptionsByType(): array | ||
241 | { | ||
242 | return [ | ||
243 | [DailyPageHelper::DAY, $date = new \DateTimeImmutable(), 'Today - ' . $date->format('F j, Y')], | ||
244 | [DailyPageHelper::DAY, $date = new \DateTimeImmutable('-1 day'), 'Yesterday - ' . $date->format('F j, Y')], | ||
245 | [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'], | ||
246 | [DailyPageHelper::WEEK, new \DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'], | ||
247 | [DailyPageHelper::MONTH, new \DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'], | ||
248 | ]; | ||
249 | } | ||
250 | |||
251 | /** | ||
252 | * Data provider for testGetDescriptionsByType() test method. | ||
253 | */ | ||
254 | public function getRssLengthsByType(): array | ||
255 | { | ||
256 | return [ | ||
257 | [DailyPageHelper::DAY], | ||
258 | [DailyPageHelper::WEEK], | ||
259 | [DailyPageHelper::MONTH], | ||
260 | ]; | ||
261 | } | ||
262 | } | ||