Skip to content

Commit 9e545c1

Browse files
carlbennettclaude
andcommitted
Add PHPUnit tests for DateTime, DateTimeImmutable, ExceptionHandler, and Router
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 6a97515 commit 9e545c1

4 files changed

Lines changed: 447 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Core;
4+
5+
use \BNETDocs\Libraries\Core\DateTimeImmutable;
6+
use \DateTimeInterface;
7+
use \DateTimeZone;
8+
use \PHPUnit\Framework\TestCase;
9+
10+
class DateTimeImmutableTest extends TestCase
11+
{
12+
private DateTimeImmutable $dt;
13+
14+
protected function setUp(): void
15+
{
16+
$this->dt = new DateTimeImmutable('2024-01-15 12:00:00', new DateTimeZone('UTC'));
17+
}
18+
19+
// jsonSerialize
20+
21+
public function testJsonSerializeHasRequiredKeys(): void
22+
{
23+
$data = $this->dt->jsonSerialize();
24+
$this->assertArrayHasKey('iso', $data);
25+
$this->assertArrayHasKey('tz', $data);
26+
$this->assertArrayHasKey('unix', $data);
27+
}
28+
29+
public function testJsonSerializeIsoIsRfc2822(): void
30+
{
31+
$data = $this->dt->jsonSerialize();
32+
$this->assertSame($this->dt->format(DateTimeInterface::RFC2822), $data['iso']);
33+
}
34+
35+
public function testJsonSerializeTzIsTimezoneName(): void
36+
{
37+
$data = $this->dt->jsonSerialize();
38+
$this->assertSame($this->dt->format('e'), $data['tz']);
39+
}
40+
41+
public function testJsonSerializeUnixIsInt(): void
42+
{
43+
$data = $this->dt->jsonSerialize();
44+
$this->assertIsInt($data['unix']);
45+
}
46+
47+
public function testJsonSerializeUnixMatchesTimestamp(): void
48+
{
49+
$data = $this->dt->jsonSerialize();
50+
$this->assertSame((int) $this->dt->format('U'), $data['unix']);
51+
}
52+
53+
public function testJsonSerializeUtcTimezone(): void
54+
{
55+
$data = $this->dt->jsonSerialize();
56+
$this->assertSame('UTC', $data['tz']);
57+
}
58+
59+
public function testJsonSerializeNonUtcTimezone(): void
60+
{
61+
$dt = new DateTimeImmutable('2024-06-01 00:00:00', new DateTimeZone('America/New_York'));
62+
$data = $dt->jsonSerialize();
63+
$this->assertSame('America/New_York', $data['tz']);
64+
}
65+
66+
// __toString
67+
68+
public function testToStringFormat(): void
69+
{
70+
$expected = sprintf('%d %s', $this->dt->format('U'), $this->dt->format(DateTimeInterface::RFC2822));
71+
$this->assertSame($expected, (string) $this->dt);
72+
}
73+
74+
public function testToStringStartsWithUnixTimestamp(): void
75+
{
76+
$str = (string) $this->dt;
77+
$parts = explode(' ', $str, 2);
78+
$this->assertSame($this->dt->format('U'), $parts[0]);
79+
}
80+
81+
public function testToStringContainsRfc2822(): void
82+
{
83+
$str = (string) $this->dt;
84+
$this->assertStringContainsString($this->dt->format(DateTimeInterface::RFC2822), $str);
85+
}
86+
87+
// Immutability — modifiers return a new instance
88+
89+
public function testModifyReturnsNewInstance(): void
90+
{
91+
$modified = $this->dt->modify('+1 day');
92+
$this->assertNotSame($this->dt, $modified);
93+
$this->assertInstanceOf(DateTimeImmutable::class, $modified);
94+
}
95+
96+
public function testOriginalUnchangedAfterModify(): void
97+
{
98+
$original_ts = (int) $this->dt->format('U');
99+
$this->dt->modify('+1 day');
100+
$this->assertSame($original_ts, (int) $this->dt->format('U'));
101+
}
102+
103+
// Extends \DateTimeImmutable
104+
105+
public function testExtendsPhpDateTimeImmutable(): void
106+
{
107+
$this->assertInstanceOf(\DateTimeImmutable::class, $this->dt);
108+
}
109+
110+
public function testImplementsJsonSerializable(): void
111+
{
112+
$this->assertInstanceOf(\JsonSerializable::class, $this->dt);
113+
}
114+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Core;
4+
5+
use \BNETDocs\Libraries\Core\DateTime;
6+
use \DateTimeInterface;
7+
use \DateTimeZone;
8+
use \PHPUnit\Framework\TestCase;
9+
10+
class DateTimeTest extends TestCase
11+
{
12+
private DateTime $dt;
13+
14+
protected function setUp(): void
15+
{
16+
$this->dt = new DateTime('2024-01-15 12:00:00', new DateTimeZone('UTC'));
17+
}
18+
19+
// jsonSerialize
20+
21+
public function testJsonSerializeHasRequiredKeys(): void
22+
{
23+
$data = $this->dt->jsonSerialize();
24+
$this->assertArrayHasKey('iso', $data);
25+
$this->assertArrayHasKey('tz', $data);
26+
$this->assertArrayHasKey('unix', $data);
27+
}
28+
29+
public function testJsonSerializeIsoIsRfc2822(): void
30+
{
31+
$data = $this->dt->jsonSerialize();
32+
$this->assertSame($this->dt->format(DateTimeInterface::RFC2822), $data['iso']);
33+
}
34+
35+
public function testJsonSerializeTzIsTimezoneName(): void
36+
{
37+
$data = $this->dt->jsonSerialize();
38+
$this->assertSame($this->dt->format('e'), $data['tz']);
39+
}
40+
41+
public function testJsonSerializeUnixIsInt(): void
42+
{
43+
$data = $this->dt->jsonSerialize();
44+
$this->assertIsInt($data['unix']);
45+
}
46+
47+
public function testJsonSerializeUnixMatchesTimestamp(): void
48+
{
49+
$data = $this->dt->jsonSerialize();
50+
$this->assertSame((int) $this->dt->format('U'), $data['unix']);
51+
}
52+
53+
public function testJsonSerializeUtcTimezone(): void
54+
{
55+
$data = $this->dt->jsonSerialize();
56+
$this->assertSame('UTC', $data['tz']);
57+
}
58+
59+
public function testJsonSerializeNonUtcTimezone(): void
60+
{
61+
$dt = new DateTime('2024-06-01 00:00:00', new DateTimeZone('America/New_York'));
62+
$data = $dt->jsonSerialize();
63+
$this->assertSame('America/New_York', $data['tz']);
64+
}
65+
66+
// __toString
67+
68+
public function testToStringFormat(): void
69+
{
70+
$expected = sprintf('%d %s', $this->dt->format('U'), $this->dt->format(DateTimeInterface::RFC2822));
71+
$this->assertSame($expected, (string) $this->dt);
72+
}
73+
74+
public function testToStringStartsWithUnixTimestamp(): void
75+
{
76+
$str = (string) $this->dt;
77+
$parts = explode(' ', $str, 2);
78+
$this->assertSame($this->dt->format('U'), $parts[0]);
79+
}
80+
81+
public function testToStringContainsRfc2822(): void
82+
{
83+
$str = (string) $this->dt;
84+
$this->assertStringContainsString($this->dt->format(DateTimeInterface::RFC2822), $str);
85+
}
86+
87+
// Extends \DateTime
88+
89+
public function testExtendsPhpDateTime(): void
90+
{
91+
$this->assertInstanceOf(\DateTime::class, $this->dt);
92+
}
93+
94+
public function testImplementsJsonSerializable(): void
95+
{
96+
$this->assertInstanceOf(\JsonSerializable::class, $this->dt);
97+
}
98+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Core;
4+
5+
use \BNETDocs\Libraries\Core\ExceptionHandler;
6+
use \Error;
7+
use \PHPUnit\Framework\TestCase;
8+
9+
class ExceptionHandlerTest extends TestCase
10+
{
11+
// Constructor
12+
13+
public function testConstructorThrowsError(): void
14+
{
15+
// Constructor is private; PHP enforces visibility before the body runs.
16+
$this->expectException(Error::class);
17+
new ExceptionHandler();
18+
}
19+
20+
// phpErrorName — known constants
21+
22+
public function testPhpErrorNameEError(): void
23+
{
24+
$this->assertSame('E_ERROR', ExceptionHandler::phpErrorName(E_ERROR));
25+
}
26+
27+
public function testPhpErrorNameEWarning(): void
28+
{
29+
$this->assertSame('E_WARNING', ExceptionHandler::phpErrorName(E_WARNING));
30+
}
31+
32+
public function testPhpErrorNameEParse(): void
33+
{
34+
$this->assertSame('E_PARSE', ExceptionHandler::phpErrorName(E_PARSE));
35+
}
36+
37+
public function testPhpErrorNameENotice(): void
38+
{
39+
$this->assertSame('E_NOTICE', ExceptionHandler::phpErrorName(E_NOTICE));
40+
}
41+
42+
public function testPhpErrorNameECoreError(): void
43+
{
44+
$this->assertSame('E_CORE_ERROR', ExceptionHandler::phpErrorName(E_CORE_ERROR));
45+
}
46+
47+
public function testPhpErrorNameECoreWarning(): void
48+
{
49+
$this->assertSame('E_CORE_WARNING', ExceptionHandler::phpErrorName(E_CORE_WARNING));
50+
}
51+
52+
public function testPhpErrorNameECompileError(): void
53+
{
54+
$this->assertSame('E_COMPILE_ERROR', ExceptionHandler::phpErrorName(E_COMPILE_ERROR));
55+
}
56+
57+
public function testPhpErrorNameECompileWarning(): void
58+
{
59+
$this->assertSame('E_COMPILE_WARNING', ExceptionHandler::phpErrorName(E_COMPILE_WARNING));
60+
}
61+
62+
public function testPhpErrorNameEUserError(): void
63+
{
64+
$this->assertSame('E_USER_ERROR', ExceptionHandler::phpErrorName(E_USER_ERROR));
65+
}
66+
67+
public function testPhpErrorNameEUserWarning(): void
68+
{
69+
$this->assertSame('E_USER_WARNING', ExceptionHandler::phpErrorName(E_USER_WARNING));
70+
}
71+
72+
public function testPhpErrorNameEUserNotice(): void
73+
{
74+
$this->assertSame('E_USER_NOTICE', ExceptionHandler::phpErrorName(E_USER_NOTICE));
75+
}
76+
77+
public function testPhpErrorNameEStrict(): void
78+
{
79+
// E_STRICT (2048) is deprecated as a constant in PHP 8.4; use the integer value directly.
80+
$this->assertSame('E_STRICT', ExceptionHandler::phpErrorName(2048));
81+
}
82+
83+
public function testPhpErrorNameERecoverableError(): void
84+
{
85+
$this->assertSame('E_RECOVERABLE_ERROR', ExceptionHandler::phpErrorName(E_RECOVERABLE_ERROR));
86+
}
87+
88+
public function testPhpErrorNameEDeprecated(): void
89+
{
90+
$this->assertSame('E_DEPRECATED', ExceptionHandler::phpErrorName(E_DEPRECATED));
91+
}
92+
93+
public function testPhpErrorNameEUserDeprecated(): void
94+
{
95+
$this->assertSame('E_USER_DEPRECATED', ExceptionHandler::phpErrorName(E_USER_DEPRECATED));
96+
}
97+
98+
public function testPhpErrorNameEAll(): void
99+
{
100+
$this->assertSame('E_ALL', ExceptionHandler::phpErrorName(E_ALL));
101+
}
102+
103+
// phpErrorName — unknown value
104+
105+
public function testPhpErrorNameUnknownReturnsEUnknown(): void
106+
{
107+
$this->assertSame('E_UNKNOWN', ExceptionHandler::phpErrorName(99999));
108+
}
109+
110+
public function testPhpErrorNameZeroReturnsEUnknown(): void
111+
{
112+
$this->assertSame('E_UNKNOWN', ExceptionHandler::phpErrorName(0));
113+
}
114+
}

0 commit comments

Comments
 (0)