aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/tests/Service/BirthdateParserServiceTest.php
blob: 79450d6913ed2a5ae2052a6d3fd205830cbda040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

/**
 * 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 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],
		];
	}

	/**
	 * @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);
	}
}