-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathDateTest.php
More file actions
191 lines (145 loc) · 6.53 KB
/
Copy pathDateTest.php
File metadata and controls
191 lines (145 loc) · 6.53 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace phpMyFAQ;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
#[AllowMockObjectsWithoutExpectations]
class DateTest extends TestCase
{
private Configuration $mockConfiguration;
private Date $dateInstance;
protected function setUp(): void
{
parent::setUp();
$this->mockConfiguration = $this->createMock(Configuration::class);
$this->mockConfiguration->method('get')->willReturn('Y-m-d H:i:s');
$this->dateInstance = new Date($this->mockConfiguration);
}
public function testCreateIsoDateWithPhpMyFaqFormat(): void
{
$pmfDate = '20231225143000'; // 2023-12-25 14:30:00
$result = Date::createIsoDate($pmfDate);
$this->assertEquals('2023-12-25 14:30', $result);
}
public function testCreateIsoDateWithCustomFormat(): void
{
$pmfDate = '20231225143000';
$customFormat = 'd.m.Y H:i';
$result = Date::createIsoDate($pmfDate, $customFormat);
$this->assertEquals('25.12.2023 14:30', $result);
}
public function testCreateIsoDateWithUnixTimestamp(): void
{
$timestamp = '1703511000'; // 2023-12-25 14:30:00
$result = Date::createIsoDate($timestamp, 'Y-m-d H:i', false);
$this->assertIsString($result);
$this->assertStringContainsString('2023-12-25', $result);
}
public function testCreateIsoDateFromUnixTimestampWithIntegerTimestamp(): void
{
$result = Date::createIsoDateFromUnixTimestamp(1703514600, 'Y-m-d H:i');
$this->assertSame('2023-12-25 15:30', $result);
}
public function testCreateIsoDateFromUnixTimestampWithDateString(): void
{
$result = Date::createIsoDateFromUnixTimestamp('2024-02-29 12:34:00', 'Y-m-d H:i');
$this->assertSame('2024-02-29 12:34', $result);
}
public function testCreateIsoDateWithDifferentFormats(): void
{
$pmfDate = '20240101120000'; // 2024-01-01 12:00:00
$isoFormat = Date::createIsoDate($pmfDate, 'c'); // ISO 8601
$this->assertStringContainsString('2024-01-01T12:00:00', $isoFormat);
$shortFormat = Date::createIsoDate($pmfDate, 'Y-m-d');
$this->assertEquals('2024-01-01', $shortFormat);
$longFormat = Date::createIsoDate($pmfDate, 'l, F j, Y');
$this->assertStringContainsString('Monday', $longFormat);
$this->assertStringContainsString('January', $longFormat);
}
public function testFormatWithValidDate(): void
{
$inputDate = '2023-12-25 14:30:00';
$result = $this->dateInstance->format($inputDate);
$this->assertEquals('2023-12-25 14:30:00', $result);
}
public function testFormatWithDifferentDateFormats(): void
{
// Test with different configuration formats
$mockConfig = $this->createMock(Configuration::class);
$mockConfig->expects($this->once())->method('get')->with('main.dateFormat')->willReturn('d.m.Y H:i');
$dateInstance = new Date($mockConfig);
$inputDate = '2023-12-25 14:30:00';
$result = $dateInstance->format($inputDate);
$this->assertEquals('25.12.2023 14:30', $result);
}
public function testFormatWithInvalidDate(): void
{
// Mock logger to verify error logging - use Monolog\Logger instead of generic LoggerInterface
$mockLogger = $this->createMock(\Monolog\Logger::class);
$mockLogger->expects($this->once())->method('error')->with($this->isString());
$this->mockConfiguration->method('getLogger')->willReturn($mockLogger);
$invalidDate = 'not-a-date';
$result = $this->dateInstance->format($invalidDate);
$this->assertEquals('', $result);
}
public function testFormatWithEmptyDate(): void
{
// Empty string in DateTime constructor gets parsed as current date, not an error
// So we don't expect the logger to be called for an empty string
$emptyDate = '';
$result = $this->dateInstance->format($emptyDate);
// Empty string gets parsed as current date, so we expect a valid date string
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testFormatWithActualInvalidDate(): void
{
// Mock logger for error case - use Monolog\Logger
$mockLogger = $this->createMock(\Monolog\Logger::class);
$mockLogger->expects($this->once())->method('error');
$this->mockConfiguration->method('getLogger')->willReturn($mockLogger);
// Use a truly invalid date format that will cause DateTime to throw an exception
$invalidDate = 'totally-invalid-date-format-that-will-fail';
$result = $this->dateInstance->format($invalidDate);
$this->assertEquals('', $result);
}
public function testCreateIsoDateEdgeCases(): void
{
// Test with minimal valid phpMyFAQ date
$minimalDate = '20230101000000';
$result = Date::createIsoDate($minimalDate);
$this->assertEquals('2023-01-01 00:00', $result);
// Test with maximum values
$maxDate = '20231231235959';
$result = Date::createIsoDate($maxDate);
$this->assertEquals('2023-12-31 23:59', $result);
}
public function testDateInstanceIsReadonly(): void
{
// Test that Date class is readonly by checking if we can create it properly
$this->assertInstanceOf(Date::class, $this->dateInstance);
// Verify the configuration is properly set through constructor
$reflection = new \ReflectionClass($this->dateInstance);
$this->assertTrue($reflection->isReadOnly());
}
public function testGetTrackingFileDateStartReturnsTimestampForValidFilename(): void
{
$result = $this->dateInstance->getTrackingFileDateStart('tracking25012026');
$this->assertSame(gmmktime(0, 0, 0, 1, 25, 2026), $result);
}
public function testGetTrackingFileDateStartReturnsMinusOneForShortFilename(): void
{
$result = $this->dateInstance->getTrackingFileDateStart('tracking');
$this->assertSame(-1, $result);
}
public function testGetTrackingFileDateEndReturnsTimestampForValidFilename(): void
{
$result = $this->dateInstance->getTrackingFileDateEnd('tracking25012026');
$this->assertSame(gmmktime(23, 59, 59, 1, 25, 2026), $result);
}
public function testGetTrackingFileDateEndReturnsMinusOneForShortFilename(): void
{
$result = $this->dateInstance->getTrackingFileDateEnd('tracking');
$this->assertSame(-1, $result);
}
}