|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Constructo\Test\_; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use stdClass; |
| 9 | +use Stringable; |
| 10 | + |
| 11 | +use function Constructo\Cast\arrayify; |
| 12 | +use function Constructo\Cast\boolify; |
| 13 | +use function Constructo\Cast\floatify; |
| 14 | +use function Constructo\Cast\integerify; |
| 15 | +use function Constructo\Cast\mapify; |
| 16 | +use function Constructo\Cast\stringify; |
| 17 | + |
| 18 | +final class CastFunctionsTest extends TestCase |
| 19 | +{ |
| 20 | + public function testToArrayReturnsArrayWhenValueIsArray(): void |
| 21 | + { |
| 22 | + $value = ['key' => 'value']; |
| 23 | + $result = arrayify($value); |
| 24 | + $this->assertEquals($value, $result); |
| 25 | + } |
| 26 | + |
| 27 | + public function testToArrayReturnsDefaultWhenValueIsNotArray(): void |
| 28 | + { |
| 29 | + $value = 'not an array'; |
| 30 | + $default = ['default']; |
| 31 | + $result = arrayify($value, $default); |
| 32 | + $this->assertEquals($default, $result); |
| 33 | + } |
| 34 | + |
| 35 | + public function testToStringReturnsStringWhenValueIsString(): void |
| 36 | + { |
| 37 | + $value = 'string'; |
| 38 | + $result = stringify($value); |
| 39 | + $this->assertEquals($value, $result); |
| 40 | + } |
| 41 | + |
| 42 | + public function testToStringReturnsDefaultWhenValueIsNotString(): void |
| 43 | + { |
| 44 | + $value = new stdClass(); |
| 45 | + $default = 'default'; |
| 46 | + $result = stringify($value, $default); |
| 47 | + $this->assertEquals($default, $result); |
| 48 | + } |
| 49 | + |
| 50 | + public function testToIntReturnsIntWhenValueIsInt(): void |
| 51 | + { |
| 52 | + $value = 123; |
| 53 | + $result = integerify($value); |
| 54 | + $this->assertEquals($value, $result); |
| 55 | + } |
| 56 | + |
| 57 | + public function testToIntReturnsDefaultWhenValueIsNotInt(): void |
| 58 | + { |
| 59 | + $value = 'not an int'; |
| 60 | + $default = 456; |
| 61 | + $result = integerify($value, $default); |
| 62 | + $this->assertEquals($default, $result); |
| 63 | + } |
| 64 | + |
| 65 | + public function testToBoolReturnsBoolWhenValueIsBool(): void |
| 66 | + { |
| 67 | + $value = true; |
| 68 | + $result = boolify($value); |
| 69 | + $this->assertEquals($value, $result); |
| 70 | + } |
| 71 | + |
| 72 | + public function testToBoolReturnsDefaultWhenValueIsNotBool(): void |
| 73 | + { |
| 74 | + $value = 'not a bool'; |
| 75 | + $default = true; |
| 76 | + $result = boolify($value, $default); |
| 77 | + $this->assertEquals(true, $result); |
| 78 | + } |
| 79 | + |
| 80 | + public function testFloatifyReturnsFloatWhenValueIsFloat(): void |
| 81 | + { |
| 82 | + $value = 123.45; |
| 83 | + $result = floatify($value); |
| 84 | + $this->assertEquals($value, $result); |
| 85 | + } |
| 86 | + |
| 87 | + public function testFloatifyReturnsDefaultWhenValueIsNotFloat(): void |
| 88 | + { |
| 89 | + $value = 'not a float'; |
| 90 | + $default = 456.78; |
| 91 | + $result = floatify($value, $default); |
| 92 | + $this->assertEquals($default, $result); |
| 93 | + } |
| 94 | + |
| 95 | + public function testFloatifyConvertsNumericStringsToFloat(): void |
| 96 | + { |
| 97 | + $value = '123.45'; |
| 98 | + $result = floatify($value); |
| 99 | + $this->assertEquals(123.45, $result); |
| 100 | + } |
| 101 | + |
| 102 | + public function testFloatifyConvertsIntegersToFloat(): void |
| 103 | + { |
| 104 | + $value = 123; |
| 105 | + $result = floatify($value); |
| 106 | + $this->assertEquals(123.0, $result); |
| 107 | + $this->assertIsFloat($result); |
| 108 | + } |
| 109 | + |
| 110 | + public function testMapifyConvertsObjectToArray(): void |
| 111 | + { |
| 112 | + $object = new stdClass(); |
| 113 | + $object->name = 'John'; |
| 114 | + $object->age = 30; |
| 115 | + $result = mapify($object); |
| 116 | + $this->assertEquals(['name' => 'John', 'age' => 30], $result); |
| 117 | + } |
| 118 | + |
| 119 | + public function testMapifyReturnsArrayWhenValueIsArray(): void |
| 120 | + { |
| 121 | + $array = ['key' => 'value', 'number' => 42]; |
| 122 | + $result = mapify($array); |
| 123 | + $this->assertEquals($array, $result); |
| 124 | + } |
| 125 | + |
| 126 | + public function testMapifyHandlesNumericKeys(): void |
| 127 | + { |
| 128 | + $array = [0 => 'first', 1 => 'second', 'name' => 'test']; |
| 129 | + $result = mapify($array); |
| 130 | + $expected = ['key_0' => 'first', 'key_1' => 'second', 'name' => 'test']; |
| 131 | + $this->assertEquals($expected, $result); |
| 132 | + } |
| 133 | + |
| 134 | + public function testMapifyReturnsDefaultWhenValueIsNotArrayOrObject(): void |
| 135 | + { |
| 136 | + $value = 'not an array or object'; |
| 137 | + $default = ['default' => 'value']; |
| 138 | + $result = mapify($value, $default); |
| 139 | + $this->assertEquals($default, $result); |
| 140 | + } |
| 141 | + |
| 142 | + public function testMapifyReturnsEmptyArrayByDefault(): void |
| 143 | + { |
| 144 | + $value = 'not an array or object'; |
| 145 | + $result = mapify($value); |
| 146 | + $this->assertEquals([], $result); |
| 147 | + } |
| 148 | + |
| 149 | + public function testStringifyWithStringableObject(): void |
| 150 | + { |
| 151 | + $stringable = new class implements Stringable { |
| 152 | + public function __toString(): string |
| 153 | + { |
| 154 | + return 'stringable object'; |
| 155 | + } |
| 156 | + }; |
| 157 | + $result = stringify($stringable); |
| 158 | + $this->assertEquals('stringable object', $result); |
| 159 | + } |
| 160 | + |
| 161 | + public function testStringifyWithToStringMethod(): void |
| 162 | + { |
| 163 | + $object = new class { |
| 164 | + public function __toString(): string |
| 165 | + { |
| 166 | + return 'object with toString'; |
| 167 | + } |
| 168 | + }; |
| 169 | + $result = stringify($object); |
| 170 | + $this->assertEquals('object with toString', $result); |
| 171 | + } |
| 172 | + |
| 173 | + public function testStringifyWithScalarValues(): void |
| 174 | + { |
| 175 | + $this->assertEquals('123', stringify(123)); |
| 176 | + $this->assertEquals('123.45', stringify(123.45)); |
| 177 | + $this->assertEquals('1', stringify(true)); |
| 178 | + $this->assertEquals('', stringify(false)); |
| 179 | + } |
| 180 | + |
| 181 | + public function testIntegerifyWithNumericString(): void |
| 182 | + { |
| 183 | + $this->assertEquals(123, integerify('123')); |
| 184 | + $this->assertEquals(123, integerify('123.45')); |
| 185 | + $this->assertEquals(0, integerify('not numeric')); |
| 186 | + } |
| 187 | +} |
0 commit comments