3 declare(strict_types
=1);
5 namespace Shaarli\Helper
;
9 use Shaarli\Bookmark\Bookmark
;
11 use Slim\Http\Request
;
13 class DailyPageHelperTest
extends TestCase
16 * @dataProvider getRequestedTypes
18 public function testExtractRequestedType(array $queryParams, string $expectedType): void
20 $request = $this->createMock(Request
::class);
21 $request->method('getQueryParam')->willReturnCallback(function ($key) use ($queryParams): ?string {
22 return $queryParams[$key] ?? null;
25 $type = DailyPageHelper
::extractRequestedType($request);
27 static::assertSame($type, $expectedType);
31 * @dataProvider getRequestedDateTimes
33 public function testExtractRequestedDateTime(
37 DateTimeInterface
$expectedDateTime,
38 string $compareFormat = 'Ymd'
40 $dateTime = DailyPageHelper
::extractRequestedDateTime($type, $input, $bookmark);
42 static::assertSame($dateTime->format($compareFormat), $expectedDateTime->format($compareFormat));
45 public function testExtractRequestedDateTimeExceptionUnknownType(): void
47 $this->expectException(\Exception
::class);
48 $this->expectExceptionMessage('Unsupported daily format type');
50 DailyPageHelper
::extractRequestedDateTime('nope', null, null);
54 * @dataProvider getFormatsByType
56 public function testGetFormatByType(string $type, string $expectedFormat): void
58 $format = DailyPageHelper
::getFormatByType($type);
60 static::assertSame($expectedFormat, $format);
63 public function testGetFormatByTypeExceptionUnknownType(): void
65 $this->expectException(\Exception
::class);
66 $this->expectExceptionMessage('Unsupported daily format type');
68 DailyPageHelper
::getFormatByType('nope');
72 * @dataProvider getStartDatesByType
74 public function testGetStartDatesByType(
76 DateTimeImmutable
$dateTime,
77 DateTimeInterface
$expectedDateTime
79 $startDateTime = DailyPageHelper
::getStartDateTimeByType($type, $dateTime);
81 static::assertEquals($expectedDateTime, $startDateTime);
84 public function testGetStartDatesByTypeExceptionUnknownType(): void
86 $this->expectException(\Exception
::class);
87 $this->expectExceptionMessage('Unsupported daily format type');
89 DailyPageHelper
::getStartDateTimeByType('nope', new DateTimeImmutable());
93 * @dataProvider getEndDatesByType
95 public function testGetEndDatesByType(
97 DateTimeImmutable
$dateTime,
98 DateTimeInterface
$expectedDateTime
100 $endDateTime = DailyPageHelper
::getEndDateTimeByType($type, $dateTime);
102 static::assertEquals($expectedDateTime, $endDateTime);
105 public function testGetEndDatesByTypeExceptionUnknownType(): void
107 $this->expectException(\Exception
::class);
108 $this->expectExceptionMessage('Unsupported daily format type');
110 DailyPageHelper
::getEndDateTimeByType('nope', new DateTimeImmutable());
114 * @dataProvider getDescriptionsByType
116 public function testGeDescriptionsByType(
118 DateTimeImmutable
$dateTime,
119 string $expectedDescription
121 $description = DailyPageHelper
::getDescriptionByType($type, $dateTime);
123 static::assertEquals($expectedDescription, $description);
127 * @dataProvider getDescriptionsByTypeNotIncludeRelative
129 public function testGeDescriptionsByTypeNotIncludeRelative(
131 \DateTimeImmutable
$dateTime,
132 string $expectedDescription
134 $description = DailyPageHelper
::getDescriptionByType($type, $dateTime, false);
136 static::assertEquals($expectedDescription, $description);
139 public function getDescriptionByTypeExceptionUnknownType(): void
141 $this->expectException(\Exception
::class);
142 $this->expectExceptionMessage('Unsupported daily format type');
144 DailyPageHelper
::getDescriptionByType('nope', new DateTimeImmutable());
148 * @dataProvider getRssLengthsByType
150 public function testGeRssLengthsByType(string $type): void {
151 $length = DailyPageHelper
::getRssLengthByType($type);
153 static::assertIsInt($length);
156 public function testGeRssLengthsByTypeExceptionUnknownType(): void
158 $this->expectException(\Exception
::class);
159 $this->expectExceptionMessage('Unsupported daily format type');
161 DailyPageHelper
::getRssLengthByType('nope');
165 * @dataProvider getCacheDatePeriodByType
167 public function testGetCacheDatePeriodByType(
169 DateTimeImmutable
$requested,
170 DateTimeInterface
$start,
171 DateTimeInterface
$end
173 $period = DailyPageHelper
::getCacheDatePeriodByType($type, $requested);
175 static::assertEquals($start, $period->getStartDate());
176 static::assertEquals($end, $period->getEndDate());
179 public function testGetCacheDatePeriodByTypeExceptionUnknownType(): void
181 $this->expectException(\Exception
::class);
182 $this->expectExceptionMessage('Unsupported daily format type');
184 DailyPageHelper
::getCacheDatePeriodByType('nope');
188 * Data provider for testExtractRequestedType() test method.
190 public function getRequestedTypes(): array
193 [['month' => null], DailyPageHelper
::DAY
],
194 [['month' => ''], DailyPageHelper
::MONTH
],
195 [['month' => 'content'], DailyPageHelper
::MONTH
],
196 [['week' => null], DailyPageHelper
::DAY
],
197 [['week' => ''], DailyPageHelper
::WEEK
],
198 [['week' => 'content'], DailyPageHelper
::WEEK
],
199 [['day' => null], DailyPageHelper
::DAY
],
200 [['day' => ''], DailyPageHelper
::DAY
],
201 [['day' => 'content'], DailyPageHelper
::DAY
],
206 * Data provider for testExtractRequestedDateTime() test method.
208 public function getRequestedDateTimes(): array
211 [DailyPageHelper
::DAY
, '20201013', null, new \
DateTime('2020-10-13')],
213 DailyPageHelper
::DAY
,
215 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
218 [DailyPageHelper
::DAY
, '', null, new \
DateTime()],
219 [DailyPageHelper
::WEEK
, '202030', null, new \
DateTime('2020-07-20')],
221 DailyPageHelper
::WEEK
,
223 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
224 new \
DateTime('2020-10-13'),
226 [DailyPageHelper
::WEEK
, '', null, new \
DateTime(), 'Ym'],
227 [DailyPageHelper
::MONTH
, '202008', null, new \
DateTime('2020-08-01'), 'Ym'],
229 DailyPageHelper
::MONTH
,
231 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
232 new \
DateTime('2020-10-13'),
235 [DailyPageHelper
::MONTH
, '', null, new \
DateTime(), 'Ym'],
240 * Data provider for testGetFormatByType() test method.
242 public function getFormatsByType(): array
245 [DailyPageHelper
::DAY
, 'Ymd'],
246 [DailyPageHelper
::WEEK
, 'YW'],
247 [DailyPageHelper
::MONTH
, 'Ym'],
252 * Data provider for testGetStartDatesByType() test method.
254 public function getStartDatesByType(): array
257 [DailyPageHelper
::DAY
, new DateTimeImmutable('2020-10-09 04:05:06'), new \
DateTime('2020-10-09 00:00:00')],
258 [DailyPageHelper
::WEEK
, new DateTimeImmutable('2020-10-09 04:05:06'), new \
DateTime('2020-10-05 00:00:00')],
259 [DailyPageHelper
::MONTH
, new DateTimeImmutable('2020-10-09 04:05:06'), new \
DateTime('2020-10-01 00:00:00')],
264 * Data provider for testGetEndDatesByType() test method.
266 public function getEndDatesByType(): array
269 [DailyPageHelper
::DAY
, new DateTimeImmutable('2020-10-09 04:05:06'), new \
DateTime('2020-10-09 23:59:59')],
270 [DailyPageHelper
::WEEK
, new DateTimeImmutable('2020-10-09 04:05:06'), new \
DateTime('2020-10-11 23:59:59')],
271 [DailyPageHelper
::MONTH
, new DateTimeImmutable('2020-10-09 04:05:06'), new \
DateTime('2020-10-31 23:59:59')],
276 * Data provider for testGetDescriptionsByType() test method.
278 public function getDescriptionsByType(): array
281 [DailyPageHelper
::DAY
, $date = new DateTimeImmutable(), 'Today - ' . $date->format('F j, Y')],
282 [DailyPageHelper
::DAY
, $date = new DateTimeImmutable('-1 day'), 'Yesterday - ' . $date->format('F j, Y')],
283 [DailyPageHelper
::DAY
, new DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'],
284 [DailyPageHelper
::WEEK
, new DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'],
285 [DailyPageHelper
::MONTH
, new DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
290 * Data provider for testGeDescriptionsByTypeNotIncludeRelative() test method.
292 public function getDescriptionsByTypeNotIncludeRelative(): array
295 [DailyPageHelper
::DAY
, $date = new \
DateTimeImmutable(), $date->format('F j, Y')],
296 [DailyPageHelper
::DAY
, $date = new \
DateTimeImmutable('-1 day'), $date->format('F j, Y')],
297 [DailyPageHelper
::DAY
, new \
DateTimeImmutable('2020-10-09 04:05:06'), 'October 9, 2020'],
298 [DailyPageHelper
::WEEK
, new \
DateTimeImmutable('2020-10-09 04:05:06'), 'Week 41 (October 5, 2020)'],
299 [DailyPageHelper
::MONTH
, new \
DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
304 * Data provider for testGetRssLengthsByType() test method.
306 public function getRssLengthsByType(): array
309 [DailyPageHelper
::DAY
],
310 [DailyPageHelper
::WEEK
],
311 [DailyPageHelper
::MONTH
],
316 * Data provider for testGetCacheDatePeriodByType() test method.
318 public function getCacheDatePeriodByType(): array
322 DailyPageHelper
::DAY
,
323 new DateTimeImmutable('2020-10-09 04:05:06'),
324 new \
DateTime('2020-10-09 00:00:00'),
325 new \
DateTime('2020-10-09 23:59:59'),
328 DailyPageHelper
::WEEK
,
329 new DateTimeImmutable('2020-10-09 04:05:06'),
330 new \
DateTime('2020-10-05 00:00:00'),
331 new \
DateTime('2020-10-11 23:59:59'),
334 DailyPageHelper
::MONTH
,
335 new DateTimeImmutable('2020-10-09 04:05:06'),
336 new \
DateTime('2020-10-01 00:00:00'),
337 new \
DateTime('2020-10-31 23:59:59'),