]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/DateTimeFormatTest.php
Merge pull request #11973 from MrPetovan/task/test-fixDateFormat
[friendica.git] / tests / src / Util / DateTimeFormatTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use Friendica\Test\MockedTest;
25 use Friendica\Util\DateTimeFormat;
26
27 class DateTimeFormatTest extends MockedTest
28 {
29         public function dataYearMonth()
30         {
31                 return [
32                         'validNormal' => [
33                                 'input' => '1990-10',
34                                 'assert' => true,
35                         ],
36                         'validOneCharMonth' => [
37                                 'input' => '1990-1',
38                                 'assert' => true,
39                         ],
40                         'validTwoCharMonth' => [
41                                 'input' => '1990-01',
42                                 'assert' => true,
43                         ],
44                         'invalidFormat' => [
45                                 'input' => '199-11',
46                                 'assert' => false,
47                         ],
48                         'invalidFormat2' => [
49                                 'input' => '1990-15',
50                                 'assert' => false,
51                         ],
52                         'invalidFormat3' => [
53                                 'input' => '99-101',
54                                 'assert' => false,
55                         ],
56                         'invalidFormat4' => [
57                                 'input' => '11-1990',
58                                 'assert' => false,
59                         ],
60                         'invalidFuture' => [
61                                 'input' => '3030-12',
62                                 'assert' => false,
63                         ],
64                         'invalidYear' => [
65                                 'input' => '-100-10',
66                                 'assert' => false,
67                         ],
68                 ];
69         }
70
71         /**
72          * @dataProvider dataYearMonth
73          */
74         public function testIsYearMonth(string $input, bool $assert)
75         {
76                 $dtFormat = new DateTimeFormat();
77
78                 self::assertEquals($assert, $dtFormat->isYearMonth($input));
79         }
80
81         /**
82          * Test the DateTimeFormat::API output.
83          *
84          * @return void
85          */
86         public function testApiDate()
87         {
88                 self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API));
89         }
90
91         public function dataFix(): array
92         {
93                 return [
94                         'Mo, 19 Sep 2022 14:51:00 +0200' => [
95                                 'expected' => '19 Sep 2022 14:51:00 +0200',
96                                 'dateString' => 'Mo, 19 Sep 2022 14:51:00 +0200',
97                         ],
98                         '2020-11-21T12:00:13.745339ZZ' => [
99                                 'expected' => '2020-11-21T12:00:13.745339Z',
100                                 'dateString' => '2020-11-21T12:00:13.745339ZZ',
101                         ],
102                         '2016-09-09T13:32:00ZZ' => [
103                                 'expected' => '2016-09-09T13:32:00Z',
104                                 'dateString' => '2016-09-09T13:32:00ZZ',
105                         ],
106                         '2021-09-09T16:19:00ZZ' => [
107                                 'expected' => '2021-09-09T16:19:00Z',
108                                 'dateString' => '2021-09-09T16:19:00ZZ',
109                         ],
110                         'Sun, 10/03/2021 - 12:41' => [
111                                 'expected' => 'Sun, 10/03/2021 12:41',
112                                 'dateString' => 'Sun, 10/03/2021 - 12:41',
113                         ],
114                         'Mon, 09/12/2022 - 09:02' => [
115                                 'expected' => 'Mon, 09/12/2022 09:02',
116                                 'dateString' => 'Mon, 09/12/2022 - 09:02',
117                         ],
118                         '4:30 PM, Sep 13, 2022' => [
119                                 'expected' => '4:30 PM Sep 13 2022',
120                                 'dateString' => '4:30 PM, Sep 13, 2022',
121                         ],
122                         'August 27, 2022 - 21:00' => [
123                                 'expected' => 'August 27, 2022, 21:00',
124                                 'dateString' => 'August 27, 2022 - 21:00',
125                         ],
126                         '2021-09-19T14:06:03&#x2B;00:00' => [
127                                 'expected' => '2021-09-19T14:06:03+00:00',
128                                 'dateString' => '2021-09-19T14:06:03&#x2B;00:00',
129                         ],
130                 ];
131         }
132
133         /**
134          * @dataProvider dataFix
135          *
136          * @param $expected
137          * @param $dateString
138          * @return void
139          */
140         public function testFix($expected, $dateString)
141         {
142                 $this->assertEquals($expected, DateTimeFormat::fix($dateString));
143         }
144 }