aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/tests/Service/BirthdateParserServiceTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/user_ldap/tests/Service/BirthdateParserServiceTest.php')
-rw-r--r--apps/user_ldap/tests/Service/BirthdateParserServiceTest.php51
1 files changed, 51 insertions, 0 deletions
diff --git a/apps/user_ldap/tests/Service/BirthdateParserServiceTest.php b/apps/user_ldap/tests/Service/BirthdateParserServiceTest.php
new file mode 100644
index 00000000000..061118f10c1
--- /dev/null
+++ b/apps/user_ldap/tests/Service/BirthdateParserServiceTest.php
@@ -0,0 +1,51 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\User_LDAP\Tests\Service;
+
+use DateTimeImmutable;
+use OCA\User_LDAP\Service\BirthdateParserService;
+use PHPUnit\Framework\TestCase;
+
+class BirthdateParserServiceTest extends TestCase {
+ private BirthdateParserService $service;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->service = new BirthdateParserService();
+ }
+
+ public static function parseBirthdateDataProvider(): array {
+ return [
+ ['2024-01-01', new DateTimeImmutable('2024-01-01'), false],
+ ['20240101', new DateTimeImmutable('2024-01-01'), false],
+ ['199412161032Z', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time
+ ['199412160532-0500', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time
+ ['2023-07-31T00:60:59.000Z', null, true],
+ ['01.01.2024', null, true],
+ ['01/01/2024', null, true],
+ ['01 01 2024', null, true],
+ ['foobar', null, true],
+ ];
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('parseBirthdateDataProvider')]
+ public function testParseBirthdate(
+ string $value,
+ ?DateTimeImmutable $expected,
+ bool $shouldThrow,
+ ): void {
+ if ($shouldThrow) {
+ $this->expectException(\InvalidArgumentException::class);
+ }
+
+ $actual = $this->service->parseBirthdate($value);
+ $this->assertEquals($expected, $actual);
+ }
+}