Skip to content

Commit 00f17af

Browse files
carlbennettclaude
andcommitted
Add PHPUnit tests for Tag\Types, Packet layers, and Discord Embed classes
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9e545c1 commit 00f17af

File tree

7 files changed

+877
-0
lines changed

7 files changed

+877
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Discord;
4+
5+
use \BNETDocs\Libraries\Discord\EmbedAuthor;
6+
use \LengthException;
7+
use \PHPUnit\Framework\TestCase;
8+
9+
class EmbedAuthorTest extends TestCase
10+
{
11+
// Constructor
12+
13+
public function testConstructorSetsName(): void
14+
{
15+
$a = new EmbedAuthor('BNETDocs');
16+
$data = $a->jsonSerialize();
17+
$this->assertSame('BNETDocs', $data['name']);
18+
}
19+
20+
public function testConstructorOptionalUrlAndIconUrl(): void
21+
{
22+
$a = new EmbedAuthor('BNETDocs', 'https://example.com', 'https://example.com/icon.png');
23+
$data = $a->jsonSerialize();
24+
$this->assertSame('https://example.com', $data['url']);
25+
$this->assertSame('https://example.com/icon.png', $data['icon_url']);
26+
}
27+
28+
// setName
29+
30+
public function testSetNameEmptyThrowsLengthException(): void
31+
{
32+
$this->expectException(LengthException::class);
33+
new EmbedAuthor('');
34+
}
35+
36+
public function testSetNameTooLongThrowsLengthException(): void
37+
{
38+
$this->expectException(LengthException::class);
39+
new EmbedAuthor(str_repeat('x', EmbedAuthor::MAX_NAME + 1));
40+
}
41+
42+
public function testSetNameAtMaxLengthIsAllowed(): void
43+
{
44+
$a = new EmbedAuthor(str_repeat('x', EmbedAuthor::MAX_NAME));
45+
$data = $a->jsonSerialize();
46+
$this->assertSame(EmbedAuthor::MAX_NAME, strlen($data['name']));
47+
}
48+
49+
// jsonSerialize — empty optional fields are stripped
50+
51+
public function testJsonSerializeOmitsEmptyUrl(): void
52+
{
53+
$a = new EmbedAuthor('Name');
54+
$data = $a->jsonSerialize();
55+
$this->assertArrayNotHasKey('url', $data);
56+
}
57+
58+
public function testJsonSerializeOmitsEmptyIconUrl(): void
59+
{
60+
$a = new EmbedAuthor('Name');
61+
$data = $a->jsonSerialize();
62+
$this->assertArrayNotHasKey('icon_url', $data);
63+
}
64+
65+
public function testJsonSerializeOmitsEmptyProxyIconUrl(): void
66+
{
67+
$a = new EmbedAuthor('Name');
68+
$data = $a->jsonSerialize();
69+
$this->assertArrayNotHasKey('proxy_icon_url', $data);
70+
}
71+
72+
public function testJsonSerializeAlwaysIncludesName(): void
73+
{
74+
$a = new EmbedAuthor('Name');
75+
$data = $a->jsonSerialize();
76+
$this->assertArrayHasKey('name', $data);
77+
}
78+
79+
// Constants
80+
81+
public function testMaxNameConstant(): void
82+
{
83+
$this->assertSame(256, EmbedAuthor::MAX_NAME);
84+
}
85+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Discord;
4+
5+
use \BNETDocs\Libraries\Discord\EmbedField;
6+
use \LengthException;
7+
use \PHPUnit\Framework\TestCase;
8+
9+
class EmbedFieldTest extends TestCase
10+
{
11+
// Constructor
12+
13+
public function testConstructorSetsProperties(): void
14+
{
15+
$f = new EmbedField('Name', 'Value', true);
16+
$data = $f->jsonSerialize();
17+
$this->assertSame('Name', $data['name']);
18+
$this->assertSame('Value', $data['value']);
19+
$this->assertTrue($data['inline']);
20+
}
21+
22+
// setName
23+
24+
public function testSetNameTooLongThrowsLengthException(): void
25+
{
26+
$this->expectException(LengthException::class);
27+
new EmbedField(str_repeat('x', EmbedField::MAX_NAME + 1), 'value', true);
28+
}
29+
30+
public function testSetNameAtMaxLengthIsAllowed(): void
31+
{
32+
$f = new EmbedField(str_repeat('x', EmbedField::MAX_NAME), 'value', true);
33+
$data = $f->jsonSerialize();
34+
$this->assertSame(EmbedField::MAX_NAME, strlen($data['name']));
35+
}
36+
37+
// setValue
38+
39+
public function testSetValueTooLongThrowsLengthException(): void
40+
{
41+
$this->expectException(LengthException::class);
42+
new EmbedField('Name', str_repeat('x', EmbedField::MAX_VALUE + 1), true);
43+
}
44+
45+
public function testSetValueTooShortThrowsLengthException(): void
46+
{
47+
// Empty string is shorter than MIN_VALUE (1)
48+
$this->expectException(LengthException::class);
49+
new EmbedField('Name', '', true);
50+
}
51+
52+
public function testSetValueAtMaxLengthIsAllowed(): void
53+
{
54+
$f = new EmbedField('Name', str_repeat('x', EmbedField::MAX_VALUE), true);
55+
$data = $f->jsonSerialize();
56+
$this->assertSame(EmbedField::MAX_VALUE, strlen((string) $data['value']));
57+
}
58+
59+
public function testSetValueWithInteger(): void
60+
{
61+
// The property is typed `string`, so int is coerced on assignment.
62+
$f = new EmbedField('Count', 42, true);
63+
$data = $f->jsonSerialize();
64+
$this->assertSame('42', $data['value']);
65+
}
66+
67+
public function testSetValueWithFloat(): void
68+
{
69+
// The property is typed `string`, so float is coerced on assignment.
70+
$f = new EmbedField('Ratio', 3.14, true);
71+
$data = $f->jsonSerialize();
72+
$this->assertSame('3.14', $data['value']);
73+
}
74+
75+
// jsonSerialize — inline=false is stripped by empty()
76+
77+
public function testJsonSerializeInlineFalseIsStrippedByEmpty(): void
78+
{
79+
// empty(false) === true, so inline=false is removed from the serialized output.
80+
// This is a known quirk of the implementation.
81+
$f = new EmbedField('Name', 'Value', false);
82+
$data = $f->jsonSerialize();
83+
$this->assertArrayNotHasKey('inline', $data);
84+
}
85+
86+
public function testJsonSerializeInlineTrueIsPresent(): void
87+
{
88+
$f = new EmbedField('Name', 'Value', true);
89+
$data = $f->jsonSerialize();
90+
$this->assertArrayHasKey('inline', $data);
91+
$this->assertTrue($data['inline']);
92+
}
93+
94+
// Constants
95+
96+
public function testMaxNameConstant(): void
97+
{
98+
$this->assertSame(256, EmbedField::MAX_NAME);
99+
}
100+
101+
public function testMaxValueConstant(): void
102+
{
103+
$this->assertSame(1024, EmbedField::MAX_VALUE);
104+
}
105+
106+
public function testMinValueConstant(): void
107+
{
108+
$this->assertSame(1, EmbedField::MIN_VALUE);
109+
}
110+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Discord;
4+
5+
use \BNETDocs\Libraries\Discord\EmbedFooter;
6+
use \LengthException;
7+
use \PHPUnit\Framework\TestCase;
8+
9+
class EmbedFooterTest extends TestCase
10+
{
11+
// Constructor
12+
13+
public function testConstructorSetsText(): void
14+
{
15+
$f = new EmbedFooter('Footer text');
16+
$data = $f->jsonSerialize();
17+
$this->assertSame('Footer text', $data['text']);
18+
}
19+
20+
public function testConstructorOptionalIconUrl(): void
21+
{
22+
$f = new EmbedFooter('Text', 'https://example.com/icon.png');
23+
$data = $f->jsonSerialize();
24+
$this->assertSame('https://example.com/icon.png', $data['icon_url']);
25+
}
26+
27+
public function testConstructorOptionalProxyIconUrl(): void
28+
{
29+
$f = new EmbedFooter('Text', '', 'https://proxy.example.com/icon.png');
30+
$data = $f->jsonSerialize();
31+
$this->assertSame('https://proxy.example.com/icon.png', $data['proxy_icon_url']);
32+
}
33+
34+
// setText
35+
36+
public function testSetTextTooLongThrowsLengthException(): void
37+
{
38+
$this->expectException(LengthException::class);
39+
new EmbedFooter(str_repeat('x', EmbedFooter::MAX_TEXT + 1));
40+
}
41+
42+
public function testSetTextAtMaxLengthIsAllowed(): void
43+
{
44+
$f = new EmbedFooter(str_repeat('x', EmbedFooter::MAX_TEXT));
45+
$data = $f->jsonSerialize();
46+
$this->assertSame(EmbedFooter::MAX_TEXT, strlen($data['text']));
47+
}
48+
49+
// jsonSerialize — empty optional fields are stripped
50+
51+
public function testJsonSerializeOmitsEmptyIconUrl(): void
52+
{
53+
$f = new EmbedFooter('Text');
54+
$data = $f->jsonSerialize();
55+
$this->assertArrayNotHasKey('icon_url', $data);
56+
}
57+
58+
public function testJsonSerializeOmitsEmptyProxyIconUrl(): void
59+
{
60+
$f = new EmbedFooter('Text');
61+
$data = $f->jsonSerialize();
62+
$this->assertArrayNotHasKey('proxy_icon_url', $data);
63+
}
64+
65+
public function testJsonSerializeIncludesNonEmptyText(): void
66+
{
67+
$f = new EmbedFooter('My Footer');
68+
$data = $f->jsonSerialize();
69+
$this->assertArrayHasKey('text', $data);
70+
}
71+
72+
// Constants
73+
74+
public function testMaxTextConstant(): void
75+
{
76+
$this->assertSame(2048, EmbedFooter::MAX_TEXT);
77+
}
78+
}

0 commit comments

Comments
 (0)