|
4 | 4 | use Gt\FileCache\Cache; |
5 | 5 | use Gt\FileCache\FileAccess; |
6 | 6 | use PHPUnit\Framework\TestCase; |
| 7 | +use SplFileInfo; |
| 8 | +use SplFixedArray; |
7 | 9 | use stdClass; |
| 10 | +use TypeError; |
8 | 11 |
|
9 | 12 | class CacheTest extends TestCase { |
10 | 13 | public function tearDown():void { |
@@ -105,12 +108,94 @@ public function testGetArray():void { |
105 | 108 | self::assertSame($value, $sut->getArray("numbers", fn() => [])); |
106 | 109 | } |
107 | 110 |
|
| 111 | + public function testGetTypedArray_int():void { |
| 112 | + $value = [1, "2", 3.000]; |
| 113 | + $sut = $this->getSut([ |
| 114 | + "numbers" => $value, |
| 115 | + ]); |
| 116 | + $typedArray = $sut->getTypedArray("numbers", "int", fn() => []); |
| 117 | + foreach($typedArray as $value) { |
| 118 | + self::assertIsInt($value); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public function testGetTypedArray_intFailure():void { |
| 123 | + $value = [1, "2", 3.000, "four"]; |
| 124 | + $sut = $this->getSut([ |
| 125 | + "numbers" => $value, |
| 126 | + ]); |
| 127 | + self::expectException(TypeError::class); |
| 128 | + $sut->getTypedArray("numbers", "int", fn() => []); |
| 129 | + } |
| 130 | + |
| 131 | + public function testGetTypedArray_float():void { |
| 132 | + $value = [1, "2", 3.000]; |
| 133 | + $sut = $this->getSut([ |
| 134 | + "numbers" => $value, |
| 135 | + ]); |
| 136 | + $typedArray = $sut->getTypedArray("numbers", "float", fn() => []); |
| 137 | + foreach($typedArray as $value) { |
| 138 | + self::assertIsFloat($value); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public function testGetTypedArray_floatFailure():void { |
| 143 | + $value = [1, "2", 3.000, "four"]; |
| 144 | + $sut = $this->getSut([ |
| 145 | + "numbers" => $value, |
| 146 | + ]); |
| 147 | + self::expectException(TypeError::class); |
| 148 | + $sut->getTypedArray("numbers", "float", fn() => []); |
| 149 | + } |
| 150 | + |
| 151 | + public function testGetTypedArray_string():void { |
| 152 | + $value = [1, "2", 3.000, "four"]; |
| 153 | + $sut = $this->getSut([ |
| 154 | + "numbers" => $value, |
| 155 | + ]); |
| 156 | + $typedArray= $sut->getTypedArray("numbers", "string", fn() => []); |
| 157 | + foreach($typedArray as $value) { |
| 158 | + self::assertIsString($value); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public function testGetTypedArray_bool():void { |
| 163 | + $value = [0, "1", false, true, [], new StdClass()]; |
| 164 | + $sut = $this->getSut([ |
| 165 | + "booleans" => $value, |
| 166 | + ]); |
| 167 | + $typedArray= $sut->getTypedArray("booleans", "bool", fn() => []); |
| 168 | + foreach($typedArray as $i => $value) { |
| 169 | + self::assertSame((bool)($i % 2), $value, $i); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public function testGetTypedArray_class():void { |
| 174 | + $value = [new SplFileInfo(__FILE__), new SplFileInfo(__DIR__)]; |
| 175 | + $sut = $this->getSut([ |
| 176 | + "files" => $value, |
| 177 | + ]); |
| 178 | + $typedArray= $sut->getTypedArray("files", SplFileInfo::class, fn() => []); |
| 179 | + foreach($typedArray as $value) { |
| 180 | + self::assertInstanceOf(SplFileInfo::class, $value); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public function testGetTypedArray_classError():void { |
| 185 | + $value = [new SplFileInfo(__FILE__), new SplFixedArray(), new SplFileInfo(__DIR__)]; |
| 186 | + $sut = $this->getSut([ |
| 187 | + "files" => $value, |
| 188 | + ]); |
| 189 | + self::expectException(TypeError::class); |
| 190 | + $sut->getTypedArray("files", SplFileInfo::class, fn() => []); |
| 191 | + } |
| 192 | + |
108 | 193 | public function testGetArray_notArray():void { |
109 | 194 | $value = (object)[1, 2, 3]; |
110 | 195 | $sut = $this->getSut([ |
111 | 196 | "numbers" => $value, |
112 | 197 | ]); |
113 | | - self::expectException(\TypeError::class); |
| 198 | + self::expectException(TypeError::class); |
114 | 199 | self::expectExceptionMessage("Value with key 'numbers' is not an array"); |
115 | 200 | $sut->getArray("numbers", fn() => []); |
116 | 201 | } |
|
0 commit comments