aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/helper/DailyPageHelperTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/helper/DailyPageHelperTest.php')
-rw-r--r--tests/helper/DailyPageHelperTest.php94
1 files changed, 73 insertions, 21 deletions
diff --git a/tests/helper/DailyPageHelperTest.php b/tests/helper/DailyPageHelperTest.php
index 6238e648..2d745800 100644
--- a/tests/helper/DailyPageHelperTest.php
+++ b/tests/helper/DailyPageHelperTest.php
@@ -4,6 +4,8 @@ declare(strict_types=1);
4 4
5namespace Shaarli\Helper; 5namespace Shaarli\Helper;
6 6
7use DateTimeImmutable;
8use DateTimeInterface;
7use Shaarli\Bookmark\Bookmark; 9use Shaarli\Bookmark\Bookmark;
8use Shaarli\TestCase; 10use Shaarli\TestCase;
9use Slim\Http\Request; 11use Slim\Http\Request;
@@ -32,7 +34,7 @@ class DailyPageHelperTest extends TestCase
32 string $type, 34 string $type,
33 string $input, 35 string $input,
34 ?Bookmark $bookmark, 36 ?Bookmark $bookmark,
35 \DateTimeInterface $expectedDateTime, 37 DateTimeInterface $expectedDateTime,
36 string $compareFormat = 'Ymd' 38 string $compareFormat = 'Ymd'
37 ): void { 39 ): void {
38 $dateTime = DailyPageHelper::extractRequestedDateTime($type, $input, $bookmark); 40 $dateTime = DailyPageHelper::extractRequestedDateTime($type, $input, $bookmark);
@@ -71,8 +73,8 @@ class DailyPageHelperTest extends TestCase
71 */ 73 */
72 public function testGetStartDatesByType( 74 public function testGetStartDatesByType(
73 string $type, 75 string $type,
74 \DateTimeImmutable $dateTime, 76 DateTimeImmutable $dateTime,
75 \DateTimeInterface $expectedDateTime 77 DateTimeInterface $expectedDateTime
76 ): void { 78 ): void {
77 $startDateTime = DailyPageHelper::getStartDateTimeByType($type, $dateTime); 79 $startDateTime = DailyPageHelper::getStartDateTimeByType($type, $dateTime);
78 80
@@ -84,7 +86,7 @@ class DailyPageHelperTest extends TestCase
84 $this->expectException(\Exception::class); 86 $this->expectException(\Exception::class);
85 $this->expectExceptionMessage('Unsupported daily format type'); 87 $this->expectExceptionMessage('Unsupported daily format type');
86 88
87 DailyPageHelper::getStartDateTimeByType('nope', new \DateTimeImmutable()); 89 DailyPageHelper::getStartDateTimeByType('nope', new DateTimeImmutable());
88 } 90 }
89 91
90 /** 92 /**
@@ -92,8 +94,8 @@ class DailyPageHelperTest extends TestCase
92 */ 94 */
93 public function testGetEndDatesByType( 95 public function testGetEndDatesByType(
94 string $type, 96 string $type,
95 \DateTimeImmutable $dateTime, 97 DateTimeImmutable $dateTime,
96 \DateTimeInterface $expectedDateTime 98 DateTimeInterface $expectedDateTime
97 ): void { 99 ): void {
98 $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dateTime); 100 $endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dateTime);
99 101
@@ -105,7 +107,7 @@ class DailyPageHelperTest extends TestCase
105 $this->expectException(\Exception::class); 107 $this->expectException(\Exception::class);
106 $this->expectExceptionMessage('Unsupported daily format type'); 108 $this->expectExceptionMessage('Unsupported daily format type');
107 109
108 DailyPageHelper::getEndDateTimeByType('nope', new \DateTimeImmutable()); 110 DailyPageHelper::getEndDateTimeByType('nope', new DateTimeImmutable());
109 } 111 }
110 112
111 /** 113 /**
@@ -113,7 +115,7 @@ class DailyPageHelperTest extends TestCase
113 */ 115 */
114 public function testGeDescriptionsByType( 116 public function testGeDescriptionsByType(
115 string $type, 117 string $type,
116 \DateTimeImmutable $dateTime, 118 DateTimeImmutable $dateTime,
117 string $expectedDescription 119 string $expectedDescription
118 ): void { 120 ): void {
119 $description = DailyPageHelper::getDescriptionByType($type, $dateTime); 121 $description = DailyPageHelper::getDescriptionByType($type, $dateTime);
@@ -139,7 +141,7 @@ class DailyPageHelperTest extends TestCase
139 $this->expectException(\Exception::class); 141 $this->expectException(\Exception::class);
140 $this->expectExceptionMessage('Unsupported daily format type'); 142 $this->expectExceptionMessage('Unsupported daily format type');
141 143
142 DailyPageHelper::getDescriptionByType('nope', new \DateTimeImmutable()); 144 DailyPageHelper::getDescriptionByType('nope', new DateTimeImmutable());
143 } 145 }
144 146
145 /** 147 /**
@@ -160,6 +162,29 @@ class DailyPageHelperTest extends TestCase
160 } 162 }
161 163
162 /** 164 /**
165 * @dataProvider getCacheDatePeriodByType
166 */
167 public function testGetCacheDatePeriodByType(
168 string $type,
169 DateTimeImmutable $requested,
170 DateTimeInterface $start,
171 DateTimeInterface $end
172 ): void {
173 $period = DailyPageHelper::getCacheDatePeriodByType($type, $requested);
174
175 static::assertEquals($start, $period->getStartDate());
176 static::assertEquals($end, $period->getEndDate());
177 }
178
179 public function testGetCacheDatePeriodByTypeExceptionUnknownType(): void
180 {
181 $this->expectException(\Exception::class);
182 $this->expectExceptionMessage('Unsupported daily format type');
183
184 DailyPageHelper::getCacheDatePeriodByType('nope');
185 }
186
187 /**
163 * Data provider for testExtractRequestedType() test method. 188 * Data provider for testExtractRequestedType() test method.
164 */ 189 */
165 public function getRequestedTypes(): array 190 public function getRequestedTypes(): array
@@ -229,9 +254,9 @@ class DailyPageHelperTest extends TestCase
229 public function getStartDatesByType(): array 254 public function getStartDatesByType(): array
230 { 255 {
231 return [ 256 return [
232 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 00:00:00')], 257 [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')], 258 [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')], 259 [DailyPageHelper::MONTH, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-01 00:00:00')],
235 ]; 260 ];
236 } 261 }
237 262
@@ -241,9 +266,9 @@ class DailyPageHelperTest extends TestCase
241 public function getEndDatesByType(): array 266 public function getEndDatesByType(): array
242 { 267 {
243 return [ 268 return [
244 [DailyPageHelper::DAY, new \DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-09 23:59:59')], 269 [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')], 270 [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')], 271 [DailyPageHelper::MONTH, new DateTimeImmutable('2020-10-09 04:05:06'), new \DateTime('2020-10-31 23:59:59')],
247 ]; 272 ];
248 } 273 }
249 274
@@ -253,11 +278,11 @@ class DailyPageHelperTest extends TestCase
253 public function getDescriptionsByType(): array 278 public function getDescriptionsByType(): array
254 { 279 {
255 return [ 280 return [
256 [DailyPageHelper::DAY, $date = new \DateTimeImmutable(), 'Today - ' . $date->format('F j, Y')], 281 [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')], 282 [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'], 283 [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)'], 284 [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'], 285 [DailyPageHelper::MONTH, new DateTimeImmutable('2020-10-09 04:05:06'), 'October, 2020'],
261 ]; 286 ];
262 } 287 }
263 288
@@ -276,7 +301,7 @@ class DailyPageHelperTest extends TestCase
276 } 301 }
277 302
278 /** 303 /**
279 * Data provider for testGetDescriptionsByType() test method. 304 * Data provider for testGetRssLengthsByType() test method.
280 */ 305 */
281 public function getRssLengthsByType(): array 306 public function getRssLengthsByType(): array
282 { 307 {
@@ -286,4 +311,31 @@ class DailyPageHelperTest extends TestCase
286 [DailyPageHelper::MONTH], 311 [DailyPageHelper::MONTH],
287 ]; 312 ];
288 } 313 }
314
315 /**
316 * Data provider for testGetCacheDatePeriodByType() test method.
317 */
318 public function getCacheDatePeriodByType(): array
319 {
320 return [
321 [
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'),
326 ],
327 [
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'),
332 ],
333 [
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'),
338 ],
339 ];
340 }
289} 341}