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);
124 public function getDescriptionByTypeExceptionUnknownType(): void
126 $this->expectException(\Exception
::class);
127 $this->expectExceptionMessage('Unsupported daily format type');
129 DailyPageHelper
::getDescriptionByType('nope', new \
DateTimeImmutable());
133 * @dataProvider getRssLengthsByType
135 public function testGeRssLengthsByType(string $type): void {
136 $length = DailyPageHelper
::getRssLengthByType($type);
138 static::assertIsInt($length);
141 public function testGeRssLengthsByTypeExceptionUnknownType(): void
143 $this->expectException(\Exception
::class);
144 $this->expectExceptionMessage('Unsupported daily format type');
146 DailyPageHelper
::getRssLengthByType('nope');
150 * Data provider for testExtractRequestedType() test method.
152 public function getRequestedTypes(): array
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
],
168 * Data provider for testExtractRequestedDateTime() test method.
170 public function getRequestedDateTimes(): array
173 [DailyPageHelper
::DAY
, '20201013', null, new \
DateTime('2020-10-13')],
175 DailyPageHelper
::DAY
,
177 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
180 [DailyPageHelper
::DAY
, '', null, new \
DateTime()],
181 [DailyPageHelper
::WEEK
, '202030', null, new \
DateTime('2020-07-20')],
183 DailyPageHelper
::WEEK
,
185 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
186 new \
DateTime('2020-10-13'),
188 [DailyPageHelper
::WEEK
, '', null, new \
DateTime(), 'Ym'],
189 [DailyPageHelper
::MONTH
, '202008', null, new \
DateTime('2020-08-01'), 'Ym'],
191 DailyPageHelper
::MONTH
,
193 (new Bookmark())->setCreated($date = new \
DateTime('2020-10-13 12:05:31')),
194 new \
DateTime('2020-10-13'),
197 [DailyPageHelper
::MONTH
, '', null, new \
DateTime(), 'Ym'],
202 * Data provider for testGetFormatByType() test method.
204 public function getFormatsByType(): array
207 [DailyPageHelper
::DAY
, 'Ymd'],
208 [DailyPageHelper
::WEEK
, 'YW'],
209 [DailyPageHelper
::MONTH
, 'Ym'],
214 * Data provider for testGetStartDatesByType() test method.
216 public function getStartDatesByType(): array
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')],
226 * Data provider for testGetEndDatesByType() test method.
228 public function getEndDatesByType(): array
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')],
238 * Data provider for testGetDescriptionsByType() test method.
240 public function getDescriptionsByType(): array
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'],
252 * Data provider for testGetDescriptionsByType() test method.
254 public function getRssLengthsByType(): array
257 [DailyPageHelper
::DAY
],
258 [DailyPageHelper
::WEEK
],
259 [DailyPageHelper
::MONTH
],