3 declare(strict_types
=1);
5 namespace Shaarli\Helper
;
7 use Shaarli\Bookmark\Bookmark
;
11 class DailyPageHelperTest
extends TestCase
14 * @dataProvider getRequestedTypes
16 public function testExtractRequestedType(array $queryParams, string $expectedType): void
18 $request = $this->createMock(Request
::class);
19 $request->method('getQueryParam')->willReturnCallback(function ($key) use ($queryParams): ?string {
20 return $queryParams[$key] ?? null;
23 $type = DailyPageHelper
::extractRequestedType($request);
25 static::assertSame($type, $expectedType);
29 * @dataProvider getRequestedDateTimes
31 public function testExtractRequestedDateTime(
35 \DateTimeInterface
$expectedDateTime,
36 string $compareFormat = 'Ymd'
38 $dateTime = DailyPageHelper
::extractRequestedDateTime($type, $input, $bookmark);
40 static::assertSame($dateTime->format($compareFormat), $expectedDateTime->format($compareFormat));
43 public function testExtractRequestedDateTimeExceptionUnknownType(): void
45 $this->expectException(\Exception
::class);
46 $this->expectExceptionMessage('Unsupported daily format type');
48 DailyPageHelper
::extractRequestedDateTime('nope', null, null);
52 * @dataProvider getFormatsByType
54 public function testGetFormatByType(string $type, string $expectedFormat): void
56 $format = DailyPageHelper
::getFormatByType($type);
58 static::assertSame($expectedFormat, $format);
61 public function testGetFormatByTypeExceptionUnknownType(): void
63 $this->expectException(\Exception
::class);
64 $this->expectExceptionMessage('Unsupported daily format type');
66 DailyPageHelper
::getFormatByType('nope');
70 * @dataProvider getStartDatesByType
72 public function testGetStartDatesByType(
74 \DateTimeImmutable
$dateTime,
75 \DateTimeInterface
$expectedDateTime
77 $startDateTime = DailyPageHelper
::getStartDateTimeByType($type, $dateTime);
79 static::assertEquals($expectedDateTime, $startDateTime);
82 public function testGetStartDatesByTypeExceptionUnknownType(): void
84 $this->expectException(\Exception
::class);
85 $this->expectExceptionMessage('Unsupported daily format type');
87 DailyPageHelper
::getStartDateTimeByType('nope', new \
DateTimeImmutable());
91 * @dataProvider getEndDatesByType
93 public function testGetEndDatesByType(
95 \DateTimeImmutable
$dateTime,
96 \DateTimeInterface
$expectedDateTime
98 $endDateTime = DailyPageHelper
::getEndDateTimeByType($type, $dateTime);
100 static::assertEquals($expectedDateTime, $endDateTime);
103 public function testGetEndDatesByTypeExceptionUnknownType(): void
105 $this->expectException(\Exception
::class);
106 $this->expectExceptionMessage('Unsupported daily format type');
108 DailyPageHelper
::getEndDateTimeByType('nope', new \
DateTimeImmutable());
112 * @dataProvider getDescriptionsByType
114 public function testGeDescriptionsByType(
116 \DateTimeImmutable
$dateTime,
117 string $expectedDescription
119 $description = DailyPageHelper
::getDescriptionByType($type, $dateTime);
121 static::assertEquals($expectedDescription, $description);
125 * @dataProvider getDescriptionsByTypeNotIncludeRelative
127 public function testGeDescriptionsByTypeNotIncludeRelative(
129 \DateTimeImmutable
$dateTime,
130 string $expectedDescription
132 $description = DailyPageHelper
::getDescriptionByType($type, $dateTime, false);
134 static::assertEquals($expectedDescription, $description);
137 public function getDescriptionByTypeExceptionUnknownType(): void
139 $this->expectException(\Exception
::class);
140 $this->expectExceptionMessage('Unsupported daily format type');
142 DailyPageHelper
::getDescriptionByType('nope', new \
DateTimeImmutable());
146 * @dataProvider getRssLengthsByType
148 public function testGeRssLengthsByType(string $type): void {
149 $length = DailyPageHelper
::getRssLengthByType($type);
151 static::assertIsInt($length);
154 public function testGeRssLengthsByTypeExceptionUnknownType(): void
156 $this->expectException(\Exception
::class);
157 $this->expectExceptionMessage('Unsupported daily format type');
159 DailyPageHelper
::getRssLengthByType('nope');
163 * Data provider for testExtractRequestedType() test method.
165 public function getRequestedTypes(): array
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
],
181 * Data provider for testExtractRequestedDateTime() test method.
183 public function getRequestedDateTimes(): array
186 [DailyPageHelper
::DAY
, '20201013', null, new \
DateTime('2020-10-13')],
188 DailyPageHelper
::DAY
,
190 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
193 [DailyPageHelper
::DAY
, '', null, new \
DateTime()],
194 [DailyPageHelper
::WEEK
, '202030', null, new \
DateTime('2020-07-20')],
196 DailyPageHelper
::WEEK
,
198 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
199 new \
DateTime('2020-10-13'),
201 [DailyPageHelper
::WEEK
, '', null, new \
DateTime(), 'Ym'],
202 [DailyPageHelper
::MONTH
, '202008', null, new \
DateTime('2020-08-01'), 'Ym'],
204 DailyPageHelper
::MONTH
,
206 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
207 new \
DateTime('2020-10-13'),
210 [DailyPageHelper
::MONTH
, '', null, new \
DateTime(), 'Ym'],
215 * Data provider for testGetFormatByType() test method.
217 public function getFormatsByType(): array
220 [DailyPageHelper
::DAY
, 'Ymd'],
221 [DailyPageHelper
::WEEK
, 'YW'],
222 [DailyPageHelper
::MONTH
, 'Ym'],
227 * Data provider for testGetStartDatesByType() test method.
229 public function getStartDatesByType(): array
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')],
239 * Data provider for testGetEndDatesByType() test method.
241 public function getEndDatesByType(): array
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')],
251 * Data provider for testGetDescriptionsByType() test method.
253 public function getDescriptionsByType(): array
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'],
265 * Data provider for testGeDescriptionsByTypeNotIncludeRelative() test method.
267 public function getDescriptionsByTypeNotIncludeRelative(): array
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'],
279 * Data provider for testGetDescriptionsByType() test method.
281 public function getRssLengthsByType(): array
284 [DailyPageHelper
::DAY
],
285 [DailyPageHelper
::WEEK
],
286 [DailyPageHelper
::MONTH
],