|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Smoren\ArrayView\Tests\Unit\ArrayView; |
| 6 | + |
| 7 | +use Smoren\ArrayView\Exceptions\IndexError; |
| 8 | +use Smoren\ArrayView\Exceptions\KeyError; |
| 9 | +use Smoren\ArrayView\Views\ArrayView; |
| 10 | + |
| 11 | +class IndexTest extends \Codeception\Test\Unit |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @dataProvider dataProviderForPositiveIndex |
| 15 | + * @param array $array |
| 16 | + * @param int $i |
| 17 | + * @param int $expected |
| 18 | + * @return void |
| 19 | + */ |
| 20 | + public function testPositiveIndex(array $array, int $i, int $expected): void |
| 21 | + { |
| 22 | + // Given |
| 23 | + $arrayView = ArrayView::toView($array); |
| 24 | + |
| 25 | + // When |
| 26 | + $number = $arrayView[$i]; |
| 27 | + |
| 28 | + // Then |
| 29 | + $this->assertSame($expected, $number); |
| 30 | + } |
| 31 | + |
| 32 | + public static function dataProviderForPositiveIndex(): array |
| 33 | + { |
| 34 | + return [ |
| 35 | + [ |
| 36 | + [10, 20, 30, 40, 50], |
| 37 | + 0, |
| 38 | + 10 |
| 39 | + ], |
| 40 | + [ |
| 41 | + [10, 20, 30, 40, 50], |
| 42 | + 1, |
| 43 | + 20 |
| 44 | + ], |
| 45 | + [ |
| 46 | + [10, 20, 30, 40, 50], |
| 47 | + 2, |
| 48 | + 30 |
| 49 | + ], |
| 50 | + [ |
| 51 | + [10, 20, 30, 40, 50], |
| 52 | + 3, |
| 53 | + 40 |
| 54 | + ], |
| 55 | + [ |
| 56 | + [10, 20, 30, 40, 50], |
| 57 | + 4, |
| 58 | + 50 |
| 59 | + ], |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dataProvider dataProviderForPositiveStringIndex |
| 65 | + * @param array $array |
| 66 | + * @param string $i |
| 67 | + * @param int $expected |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testPositiveStringIndex(array $array, string $i, int $expected): void |
| 71 | + { |
| 72 | + // Given |
| 73 | + $arrayView = ArrayView::toView($array); |
| 74 | + |
| 75 | + // When |
| 76 | + $number = $arrayView[$i]; |
| 77 | + |
| 78 | + // Then |
| 79 | + $this->assertSame($expected, $number); |
| 80 | + } |
| 81 | + |
| 82 | + public static function dataProviderForPositiveStringIndex(): array |
| 83 | + { |
| 84 | + return [ |
| 85 | + [ |
| 86 | + [10, 20, 30, 40, 50], |
| 87 | + '0', |
| 88 | + 10 |
| 89 | + ], |
| 90 | + [ |
| 91 | + [10, 20, 30, 40, 50], |
| 92 | + '1', |
| 93 | + 20 |
| 94 | + ], |
| 95 | + [ |
| 96 | + [10, 20, 30, 40, 50], |
| 97 | + '2', |
| 98 | + 30 |
| 99 | + ], |
| 100 | + [ |
| 101 | + [10, 20, 30, 40, 50], |
| 102 | + '3', |
| 103 | + 40 |
| 104 | + ], |
| 105 | + [ |
| 106 | + [10, 20, 30, 40, 50], |
| 107 | + '4', |
| 108 | + 50 |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @dataProvider dataProviderForNegativeIndex |
| 115 | + * @param array $array |
| 116 | + * @param int $i |
| 117 | + * @param int $expected |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + public function testNegativeIndex(array $array, int $i, int $expected): void |
| 121 | + { |
| 122 | + // Given |
| 123 | + $arrayView = ArrayView::toView($array); |
| 124 | + |
| 125 | + // When |
| 126 | + $number = $arrayView[$i]; |
| 127 | + |
| 128 | + // Then |
| 129 | + $this->assertSame($expected, $number); |
| 130 | + } |
| 131 | + |
| 132 | + public static function dataProviderForNegativeIndex(): array |
| 133 | + { |
| 134 | + return [ |
| 135 | + [ |
| 136 | + [10, 20, 30, 40, 50], |
| 137 | + -0, |
| 138 | + 10 |
| 139 | + ], |
| 140 | + [ |
| 141 | + [10, 20, 30, 40, 50], |
| 142 | + -1, |
| 143 | + 50 |
| 144 | + ], |
| 145 | + [ |
| 146 | + [10, 20, 30, 40, 50], |
| 147 | + -2, |
| 148 | + 40 |
| 149 | + ], |
| 150 | + [ |
| 151 | + [10, 20, 30, 40, 50], |
| 152 | + -3, |
| 153 | + 30 |
| 154 | + ], |
| 155 | + [ |
| 156 | + [10, 20, 30, 40, 50], |
| 157 | + -4, |
| 158 | + 20 |
| 159 | + ], |
| 160 | + [ |
| 161 | + [10, 20, 30, 40, 50], |
| 162 | + -5, |
| 163 | + 10 |
| 164 | + ], |
| 165 | + ]; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @dataProvider dataProviderForNegativeStringIndex |
| 170 | + * @param array $array |
| 171 | + * @param string $i |
| 172 | + * @param int $expected |
| 173 | + * @return void |
| 174 | + */ |
| 175 | + public function testNegativeStringIndex(array $array, string $i, int $expected): void |
| 176 | + { |
| 177 | + // Given |
| 178 | + $arrayView = ArrayView::toView($array); |
| 179 | + |
| 180 | + // When |
| 181 | + $number = $arrayView[$i]; |
| 182 | + |
| 183 | + // Then |
| 184 | + $this->assertSame($expected, $number); |
| 185 | + } |
| 186 | + |
| 187 | + public static function dataProviderForNegativeStringIndex(): array |
| 188 | + { |
| 189 | + return [ |
| 190 | + [ |
| 191 | + [10, 20, 30, 40, 50], |
| 192 | + '-0', |
| 193 | + 10 |
| 194 | + ], |
| 195 | + [ |
| 196 | + [10, 20, 30, 40, 50], |
| 197 | + '-1', |
| 198 | + 50 |
| 199 | + ], |
| 200 | + [ |
| 201 | + [10, 20, 30, 40, 50], |
| 202 | + '-2', |
| 203 | + 40 |
| 204 | + ], |
| 205 | + [ |
| 206 | + [10, 20, 30, 40, 50], |
| 207 | + '-3', |
| 208 | + 30 |
| 209 | + ], |
| 210 | + [ |
| 211 | + [10, 20, 30, 40, 50], |
| 212 | + '-4', |
| 213 | + 20 |
| 214 | + ], |
| 215 | + [ |
| 216 | + [10, 20, 30, 40, 50], |
| 217 | + '-5', |
| 218 | + 10 |
| 219 | + ], |
| 220 | + ]; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * @dataProvider dataProviderForIndexesLargerThanTwo |
| 225 | + * @param mixed $i |
| 226 | + * @return void |
| 227 | + */ |
| 228 | + public function testPositiveIndexError($i): void |
| 229 | + { |
| 230 | + // Given |
| 231 | + $array = [10, 20, 30]; |
| 232 | + $arrayView = ArrayView::toView($array); |
| 233 | + |
| 234 | + // Then |
| 235 | + $this->expectException(IndexError::class); |
| 236 | + $this->expectExceptionMessageMatches('/Index \d+ is out of range/'); |
| 237 | + |
| 238 | + // When |
| 239 | + $slice = $arrayView[$i]; |
| 240 | + } |
| 241 | + |
| 242 | + public static function dataProviderForIndexesLargerThanTwo(): array |
| 243 | + { |
| 244 | + return [ |
| 245 | + [3], |
| 246 | + ['3'], |
| 247 | + [4], |
| 248 | + ['4'], |
| 249 | + [100], |
| 250 | + ['100'], |
| 251 | + ]; |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * @dataProvider dataProviderForIndexesSmallerThanThanNegativeThree |
| 256 | + * @param mixed $i |
| 257 | + * @return void |
| 258 | + */ |
| 259 | + public function testNegativeIndexError($i): void |
| 260 | + { |
| 261 | + // Given |
| 262 | + $array = [10, 20, 30]; |
| 263 | + $arrayView = ArrayView::toView($array); |
| 264 | + |
| 265 | + // Then |
| 266 | + $this->expectException(IndexError::class); |
| 267 | + $this->expectExceptionMessageMatches('/Index -\d+ is out of range/'); |
| 268 | + |
| 269 | + // When |
| 270 | + $slice = $arrayView[$i]; |
| 271 | + } |
| 272 | + |
| 273 | + public static function dataProviderForIndexesSmallerThanThanNegativeThree(): array |
| 274 | + { |
| 275 | + return [ |
| 276 | + [-4], |
| 277 | + ['-4'], |
| 278 | + [-5], |
| 279 | + ['-5'], |
| 280 | + [-100], |
| 281 | + ['-100'], |
| 282 | + ]; |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * @dataProvider dataProviderForNonIntegerIndexes |
| 287 | + * @param mixed $i |
| 288 | + * @return void |
| 289 | + */ |
| 290 | + public function testNonIntegerIndexError($i): void |
| 291 | + { |
| 292 | + // Given |
| 293 | + $array = [10, 20, 30]; |
| 294 | + $arrayView = ArrayView::toView($array); |
| 295 | + |
| 296 | + // Then |
| 297 | + $this->expectException(KeyError::class); |
| 298 | + |
| 299 | + // When |
| 300 | + $slice = $arrayView[$i]; |
| 301 | + var_dump($slice); |
| 302 | + } |
| 303 | + |
| 304 | + public static function dataProviderForNonIntegerIndexes(): array |
| 305 | + { |
| 306 | + return [ |
| 307 | + ['a'], |
| 308 | + ['1'], |
| 309 | + ['Ⅱ'], |
| 310 | + ['三'], |
| 311 | + ['④'], |
| 312 | + ['V'], |
| 313 | + ['six'], |
| 314 | + ]; |
| 315 | + } |
| 316 | +} |
0 commit comments