|
| 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 | +} |
0 commit comments