Skip to content

Commit ea64455

Browse files
carlbennettclaude
andcommitted
Add PHPUnit tests for Pagination, ArrayFlattener, ArrayTypeCheck, and Gravatar
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent fdd5ed4 commit ea64455

4 files changed

Lines changed: 577 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Core;
4+
5+
use \BNETDocs\Libraries\Core\ArrayFlattener;
6+
use \DateTime;
7+
use \DateTimeInterface;
8+
use \Error;
9+
use \PHPUnit\Framework\TestCase;
10+
11+
class ArrayFlattenerTest extends TestCase
12+
{
13+
// Constructor
14+
15+
public function testConstructorThrowsError(): void
16+
{
17+
// Constructor is private; PHP enforces visibility before the body runs,
18+
// so an Error is thrown rather than the LogicException in the body.
19+
$this->expectException(Error::class);
20+
new ArrayFlattener();
21+
}
22+
23+
// flatten — scalar types
24+
25+
public function testFlattenStringValue(): void
26+
{
27+
$data = ['key' => 'hello'];
28+
$result = ArrayFlattener::flatten($data);
29+
$this->assertSame('key hello' . PHP_EOL, $result);
30+
}
31+
32+
public function testFlattenIntegerValue(): void
33+
{
34+
$data = ['count' => 42];
35+
$result = ArrayFlattener::flatten($data);
36+
$this->assertSame('count 42' . PHP_EOL, $result);
37+
}
38+
39+
public function testFlattenFloatValue(): void
40+
{
41+
$data = ['ratio' => 3.14];
42+
$result = ArrayFlattener::flatten($data);
43+
$this->assertSame('ratio 3.14' . PHP_EOL, $result);
44+
}
45+
46+
// flatten — null and bool
47+
48+
public function testFlattenNullValue(): void
49+
{
50+
$data = ['field' => null];
51+
$result = ArrayFlattener::flatten($data);
52+
$this->assertSame('field null' . PHP_EOL, $result);
53+
}
54+
55+
public function testFlattenBoolTrue(): void
56+
{
57+
$data = ['enabled' => true];
58+
$result = ArrayFlattener::flatten($data);
59+
$this->assertSame('enabled true' . PHP_EOL, $result);
60+
}
61+
62+
public function testFlattenBoolFalse(): void
63+
{
64+
$data = ['enabled' => false];
65+
$result = ArrayFlattener::flatten($data);
66+
$this->assertSame('enabled false' . PHP_EOL, $result);
67+
}
68+
69+
// flatten — empty array value
70+
71+
public function testFlattenEmptyArrayValue(): void
72+
{
73+
$data = ['items' => []];
74+
$result = ArrayFlattener::flatten($data);
75+
$this->assertSame('items' . PHP_EOL, $result);
76+
}
77+
78+
// flatten — nested array
79+
80+
public function testFlattenNestedArray(): void
81+
{
82+
$data = ['parent' => ['child' => 'value']];
83+
$result = ArrayFlattener::flatten($data);
84+
$this->assertSame('parent_child value' . PHP_EOL, $result);
85+
}
86+
87+
// flatten — multiple top-level keys
88+
89+
public function testFlattenMultipleKeys(): void
90+
{
91+
$data = ['a' => 1, 'b' => 2];
92+
$result = ArrayFlattener::flatten($data);
93+
$this->assertSame('a 1' . PHP_EOL . 'b 2' . PHP_EOL, $result);
94+
}
95+
96+
// flatten — empty top-level array
97+
98+
public function testFlattenEmptyArray(): void
99+
{
100+
$data = [];
101+
$result = ArrayFlattener::flatten($data);
102+
$this->assertSame('', $result);
103+
}
104+
105+
// flatten — DateTimeInterface value
106+
107+
public function testFlattenDateTimeProducesThreeLines(): void
108+
{
109+
$dt = new DateTime('2024-01-15 12:00:00', new \DateTimeZone('UTC'));
110+
$data = ['ts' => $dt];
111+
$result = ArrayFlattener::flatten($data);
112+
113+
$expectedIso = 'ts_iso ' . $dt->format(DateTimeInterface::RFC2822) . PHP_EOL;
114+
$expectedTz = 'ts_tz ' . $dt->format('e') . PHP_EOL;
115+
$expectedUnix = 'ts_unix ' . $dt->format('U') . PHP_EOL;
116+
117+
$this->assertSame($expectedIso . $expectedTz . $expectedUnix, $result);
118+
}
119+
120+
// flatten — object
121+
122+
public function testFlattenObject(): void
123+
{
124+
$obj = new \stdClass();
125+
$obj->name = 'test';
126+
$result = ArrayFlattener::flatten($obj);
127+
$this->assertSame('name test' . PHP_EOL, $result);
128+
}
129+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace BNETDocs\Tests\Libraries\Core;
4+
5+
use \BNETDocs\Libraries\Core\ArrayTypeCheck;
6+
use \PHPUnit\Framework\TestCase;
7+
8+
class ArrayTypeCheckTest extends TestCase
9+
{
10+
// values()
11+
12+
public function testValuesAllStrings(): void
13+
{
14+
$this->assertTrue(ArrayTypeCheck::values(['a', 'b', 'c'], 'string'));
15+
}
16+
17+
public function testValuesAllIntegers(): void
18+
{
19+
$this->assertTrue(ArrayTypeCheck::values([1, 2, 3], 'integer'));
20+
}
21+
22+
public function testValuesMixedTypesReturnsFalse(): void
23+
{
24+
$this->assertFalse(ArrayTypeCheck::values([1, 'two', 3], 'integer'));
25+
}
26+
27+
public function testValuesEmptyArrayReturnsTrue(): void
28+
{
29+
$this->assertTrue(ArrayTypeCheck::values([], 'string'));
30+
}
31+
32+
public function testValuesAllBooleans(): void
33+
{
34+
$this->assertTrue(ArrayTypeCheck::values([true, false, true], 'boolean'));
35+
}
36+
37+
public function testValuesAllDoubles(): void
38+
{
39+
$this->assertTrue(ArrayTypeCheck::values([1.1, 2.2], 'double'));
40+
}
41+
42+
public function testValuesObjectType(): void
43+
{
44+
$obj1 = new \stdClass();
45+
$obj2 = new \stdClass();
46+
$this->assertTrue(ArrayTypeCheck::values([$obj1, $obj2], 'stdClass'));
47+
}
48+
49+
public function testValuesObjectTypeMismatch(): void
50+
{
51+
$obj = new \stdClass();
52+
$this->assertFalse(ArrayTypeCheck::values([$obj], 'SomeOtherClass'));
53+
}
54+
55+
// keys()
56+
57+
public function testKeysStringKeys(): void
58+
{
59+
$this->assertTrue(ArrayTypeCheck::keys(['foo' => 1, 'bar' => 2], 'string'));
60+
}
61+
62+
public function testKeysIntegerKeys(): void
63+
{
64+
// PHP sequential arrays use integer keys
65+
$this->assertTrue(ArrayTypeCheck::keys([0 => 'a', 1 => 'b'], 'integer'));
66+
}
67+
68+
public function testKeysStringKeysWhenExpectingInteger(): void
69+
{
70+
$this->assertFalse(ArrayTypeCheck::keys(['foo' => 1], 'integer'));
71+
}
72+
73+
public function testKeysIntegerKeysWhenExpectingString(): void
74+
{
75+
$this->assertFalse(ArrayTypeCheck::keys([0 => 'a', 1 => 'b'], 'string'));
76+
}
77+
78+
public function testKeysEmptyArrayReturnsTrue(): void
79+
{
80+
$this->assertTrue(ArrayTypeCheck::keys([], 'string'));
81+
}
82+
83+
// keysAndValues()
84+
// Returns false only when NEITHER the key NOR the value matches the type.
85+
86+
public function testKeysAndValuesBothStringMatch(): void
87+
{
88+
$this->assertTrue(ArrayTypeCheck::keysAndValues(['hello' => 'world'], 'string'));
89+
}
90+
91+
public function testKeysAndValuesEmptyArrayReturnsTrue(): void
92+
{
93+
$this->assertTrue(ArrayTypeCheck::keysAndValues([], 'string'));
94+
}
95+
96+
public function testKeysAndValuesKeyMatchesSuffices(): void
97+
{
98+
// String key with integer value: key passes, so the entry is not rejected.
99+
$this->assertTrue(ArrayTypeCheck::keysAndValues(['foo' => 42], 'string'));
100+
}
101+
102+
public function testKeysAndValuesValueMatchesSuffices(): void
103+
{
104+
// Integer key with string value: value passes, so the entry is not rejected.
105+
$this->assertTrue(ArrayTypeCheck::keysAndValues([0 => 'bar'], 'string'));
106+
}
107+
108+
public function testKeysAndValuesBothMismatchReturnsFalse(): void
109+
{
110+
// Integer key with integer value when expecting 'string': both fail → false.
111+
$this->assertFalse(ArrayTypeCheck::keysAndValues([0 => 1], 'string'));
112+
}
113+
}

0 commit comments

Comments
 (0)