Skip to content

Commit 399ac19

Browse files
carlbennettclaude
andcommitted
Add PHPUnit tests for Server\Type, Tag, Product, and Community\Credits
Covers null/StdClass construction, setter validation, Types enum integration in Tag, commit() always-false in Product, private constructor enforcement in Credits, and jsonSerialize() for all four. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cf1d4b9 commit 399ac19

4 files changed

Lines changed: 477 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Community;
4+
5+
use \BNETDocs\Libraries\Community\Credits;
6+
use \PHPUnit\Framework\TestCase;
7+
8+
class CreditsTest extends TestCase
9+
{
10+
// Constants
11+
12+
public function testDefaultLimitIsCorrect(): void
13+
{
14+
$this->assertSame(5, Credits::DEFAULT_LIMIT);
15+
}
16+
17+
public function testDefaultAnonymousIsCorrect(): void
18+
{
19+
$this->assertSame('Anonymous', Credits::DEFAULT_ANONYMOUS);
20+
}
21+
22+
// Private constructor prevents instantiation
23+
24+
public function testConstructorIsPrivate(): void
25+
{
26+
// PHP enforces visibility before executing the constructor body,
27+
// so calling a private constructor from outside the class throws
28+
// Error (not the LogicException defined inside the constructor).
29+
$this->expectException(\Error::class);
30+
$rc = new \ReflectionClass(Credits::class);
31+
$rc->newInstanceWithoutConstructor(); // this is fine; test the constructor directly:
32+
new Credits(); // @phpstan-ignore-line — intentionally testing private constructor
33+
}
34+
}

tests/Libraries/ProductTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries;
4+
5+
use \BNETDocs\Libraries\Product;
6+
use \PHPUnit\Framework\TestCase;
7+
use \stdClass;
8+
9+
class ProductTest extends TestCase
10+
{
11+
private Product $product;
12+
13+
protected function setUp(): void
14+
{
15+
// Construct via StdClass path (allocateObject), which requires no DB access.
16+
$obj = new stdClass();
17+
$obj->bnet_product_id = 0x01;
18+
$obj->bnet_product_raw = 'STAR';
19+
$obj->bnls_product_id = 1;
20+
$obj->label = 'StarCraft';
21+
$obj->sort = 0;
22+
$obj->version_byte = 0xCD;
23+
24+
$this->product = new Product($obj);
25+
}
26+
27+
// Getters reflect StdClass construction
28+
29+
public function testGetBnetProductId(): void
30+
{
31+
$this->assertSame(0x01, $this->product->getBnetProductId());
32+
}
33+
34+
public function testGetBnetProductRaw(): void
35+
{
36+
$this->assertSame('STAR', $this->product->getBnetProductRaw());
37+
}
38+
39+
public function testGetBnlsProductId(): void
40+
{
41+
$this->assertSame(1, $this->product->getBnlsProductId());
42+
}
43+
44+
public function testGetLabel(): void
45+
{
46+
$this->assertSame('StarCraft', $this->product->getLabel());
47+
}
48+
49+
public function testGetSort(): void
50+
{
51+
$this->assertSame(0, $this->product->getSort());
52+
}
53+
54+
public function testGetVersionByte(): void
55+
{
56+
$this->assertSame(0xCD, $this->product->getVersionByte());
57+
}
58+
59+
// Setters
60+
61+
public function testSetBnetProductRawChangesValue(): void
62+
{
63+
$this->product->setBnetProductRaw('W2BN');
64+
$this->assertSame('W2BN', $this->product->getBnetProductRaw());
65+
}
66+
67+
public function testSetBnlsProductIdChangesValue(): void
68+
{
69+
$this->product->setBnlsProductId(42);
70+
$this->assertSame(42, $this->product->getBnlsProductId());
71+
}
72+
73+
public function testSetLabelChangesValue(): void
74+
{
75+
$this->product->setLabel('Warcraft II');
76+
$this->assertSame('Warcraft II', $this->product->getLabel());
77+
}
78+
79+
public function testSetSortChangesValue(): void
80+
{
81+
$this->product->setSort(5);
82+
$this->assertSame(5, $this->product->getSort());
83+
}
84+
85+
public function testSetVersionByteChangesValue(): void
86+
{
87+
$this->product->setVersionByte(0xDD);
88+
$this->assertSame(0xDD, $this->product->getVersionByte());
89+
}
90+
91+
// commit() always returns false (read-only product table)
92+
93+
public function testCommitAlwaysReturnsFalse(): void
94+
{
95+
$this->assertFalse($this->product->commit());
96+
}
97+
98+
// jsonSerialize
99+
100+
public function testJsonSerializeHasExpectedKeys(): void
101+
{
102+
$data = $this->product->jsonSerialize();
103+
foreach ([
104+
'bnet_product_id', 'bnet_product_raw', 'bnls_product_id',
105+
'label', 'sort', 'version_byte',
106+
] as $key) {
107+
$this->assertArrayHasKey($key, $data, "Missing key: $key");
108+
}
109+
}
110+
111+
public function testJsonSerializeReflectsConstructedValues(): void
112+
{
113+
$data = $this->product->jsonSerialize();
114+
$this->assertSame(0x01, $data['bnet_product_id']);
115+
$this->assertSame('STAR', $data['bnet_product_raw']);
116+
$this->assertSame(1, $data['bnls_product_id']);
117+
$this->assertSame('StarCraft', $data['label']);
118+
$this->assertSame(0, $data['sort']);
119+
$this->assertSame(0xCD, $data['version_byte']);
120+
}
121+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Server;
4+
5+
use \BNETDocs\Libraries\Server\Type;
6+
use \OutOfBoundsException;
7+
use \PHPUnit\Framework\TestCase;
8+
use \stdClass;
9+
10+
class TypeTest extends TestCase
11+
{
12+
private Type $type;
13+
14+
protected function setUp(): void
15+
{
16+
// null id → allocate() sets label='' and returns true early; no DB access.
17+
$this->type = new Type(null);
18+
}
19+
20+
// Default state after null construction
21+
22+
public function testDefaultIdIsNull(): void
23+
{
24+
$this->assertNull($this->type->getId());
25+
}
26+
27+
public function testDefaultLabelIsEmpty(): void
28+
{
29+
$this->assertSame('', $this->type->getLabel());
30+
}
31+
32+
// StdClass construction path
33+
34+
public function testStdClassConstructionSetsFields(): void
35+
{
36+
$obj = new stdClass();
37+
$obj->id = 3;
38+
$obj->label = 'Battle.net Chat';
39+
40+
$t = new Type($obj);
41+
$this->assertSame(3, $t->getId());
42+
$this->assertSame('Battle.net Chat', $t->getLabel());
43+
}
44+
45+
// setLabel validation
46+
47+
public function testSetLabelEmptyStringIsAllowed(): void
48+
{
49+
$this->type->setLabel('');
50+
$this->assertSame('', $this->type->getLabel());
51+
}
52+
53+
public function testSetLabelAtMaxLengthIsAllowed(): void
54+
{
55+
$value = str_repeat('x', Type::MAX_LABEL);
56+
$this->type->setLabel($value);
57+
$this->assertSame($value, $this->type->getLabel());
58+
}
59+
60+
public function testSetLabelTooLongThrowsOutOfBounds(): void
61+
{
62+
$this->expectException(OutOfBoundsException::class);
63+
$this->type->setLabel(str_repeat('x', Type::MAX_LABEL + 1));
64+
}
65+
66+
public function testSetLabelChangesValue(): void
67+
{
68+
$this->type->setLabel('BNLS');
69+
$this->assertSame('BNLS', $this->type->getLabel());
70+
}
71+
72+
// setId validation
73+
74+
public function testSetIdNullIsAllowed(): void
75+
{
76+
$this->type->setId(null);
77+
$this->assertNull($this->type->getId());
78+
}
79+
80+
public function testSetIdPositiveIsAllowed(): void
81+
{
82+
$this->type->setId(7);
83+
$this->assertSame(7, $this->type->getId());
84+
}
85+
86+
public function testSetIdNegativeThrowsOutOfBounds(): void
87+
{
88+
$this->expectException(OutOfBoundsException::class);
89+
$this->type->setId(-1);
90+
}
91+
92+
// DB-free early-exit
93+
94+
public function testDeallocateReturnsFalseWhenIdIsNull(): void
95+
{
96+
$this->assertFalse($this->type->deallocate());
97+
}
98+
99+
// jsonSerialize
100+
101+
public function testJsonSerializeHasExpectedKeys(): void
102+
{
103+
$data = $this->type->jsonSerialize();
104+
$this->assertArrayHasKey('id', $data);
105+
$this->assertArrayHasKey('label', $data);
106+
}
107+
108+
public function testJsonSerializeDefaultValues(): void
109+
{
110+
$data = $this->type->jsonSerialize();
111+
$this->assertNull($data['id']);
112+
$this->assertSame('', $data['label']);
113+
}
114+
115+
public function testJsonSerializeReflectsSetValues(): void
116+
{
117+
$this->type->setLabel('BNLS');
118+
$data = $this->type->jsonSerialize();
119+
$this->assertSame('BNLS', $data['label']);
120+
}
121+
}

0 commit comments

Comments
 (0)