BirthdateParserServiceTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\user_ldap\tests\Service;
  7. use DateTimeImmutable;
  8. use OCA\User_LDAP\Service\BirthdateParserService;
  9. use PHPUnit\Framework\TestCase;
  10. class BirthdateParserServiceTest extends TestCase {
  11. private BirthdateParserService $service;
  12. protected function setUp(): void {
  13. parent::setUp();
  14. $this->service = new BirthdateParserService();
  15. }
  16. public function parseBirthdateDataProvider(): array {
  17. return [
  18. ['2024-01-01', new DateTimeImmutable('2024-01-01'), false],
  19. ['20240101', new DateTimeImmutable('2024-01-01'), false],
  20. ['199412161032Z', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time
  21. ['199412160532-0500', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time
  22. ['2023-07-31T00:60:59.000Z', null, true],
  23. ['01.01.2024', null, true],
  24. ['01/01/2024', null, true],
  25. ['01 01 2024', null, true],
  26. ['foobar', null, true],
  27. ];
  28. }
  29. /**
  30. * @dataProvider parseBirthdateDataProvider
  31. */
  32. public function testParseBirthdate(
  33. string $value,
  34. ?DateTimeImmutable $expected,
  35. bool $shouldThrow,
  36. ): void {
  37. if ($shouldThrow) {
  38. $this->expectException(\InvalidArgumentException::class);
  39. }
  40. $actual = $this->service->parseBirthdate($value);
  41. $this->assertEquals($expected, $actual);
  42. }
  43. }