| Total Complexity | 53 |
| Total Lines | 729 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 2 |
Complex classes like SequenceTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SequenceTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SequenceTest extends TestCase |
||
| 19 | { |
||
| 20 | public function testInterface() |
||
| 21 | { |
||
| 22 | $sequence = Sequence::of('int'); |
||
| 23 | |||
| 24 | $this->assertInstanceOf(\Countable::class, $sequence); |
||
| 25 | $this->assertSame([], unwrap($sequence)); |
||
|
|
|||
| 26 | } |
||
| 27 | |||
| 28 | public function testOf() |
||
| 29 | { |
||
| 30 | $this->assertTrue( |
||
| 31 | Sequence::of('int', 1, 2, 3)->equals( |
||
| 32 | Sequence::of('int') |
||
| 33 | ->add(1) |
||
| 34 | ->add(2) |
||
| 35 | ->add(3) |
||
| 36 | ) |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testDefer() |
||
| 41 | { |
||
| 42 | $loaded = false; |
||
| 43 | $sequence = Sequence::defer('int', (function() use (&$loaded) { |
||
| 44 | yield 1; |
||
| 45 | yield 2; |
||
| 46 | yield 3; |
||
| 47 | $loaded = true; |
||
| 48 | })()); |
||
| 49 | |||
| 50 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 51 | $this->assertFalse($loaded); |
||
| 52 | $this->assertSame([1, 2, 3], unwrap($sequence)); |
||
| 53 | $this->assertTrue($loaded); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testLazy() |
||
| 57 | { |
||
| 58 | $loaded = false; |
||
| 59 | $sequence = Sequence::lazy('int', function() use (&$loaded) { |
||
| 60 | yield 1; |
||
| 61 | yield 2; |
||
| 62 | yield 3; |
||
| 63 | $loaded = true; |
||
| 64 | }); |
||
| 65 | |||
| 66 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 67 | $this->assertFalse($loaded); |
||
| 68 | $this->assertSame([1, 2, 3], unwrap($sequence)); |
||
| 69 | $this->assertTrue($loaded); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function testMixed() |
||
| 73 | { |
||
| 74 | $sequence = Sequence::mixed(1, '2', 3); |
||
| 75 | |||
| 76 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 77 | $this->assertSame('mixed', $sequence->type()); |
||
| 78 | $this->assertSame([1, '2', 3], unwrap($sequence)); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function testInts() |
||
| 82 | { |
||
| 83 | $sequence = Sequence::ints(1, 2, 3); |
||
| 84 | |||
| 85 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 86 | $this->assertSame('int', $sequence->type()); |
||
| 87 | $this->assertSame([1, 2, 3], unwrap($sequence)); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testFloats() |
||
| 91 | { |
||
| 92 | $sequence = Sequence::floats(1, 2, 3.2); |
||
| 93 | |||
| 94 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 95 | $this->assertSame('float', $sequence->type()); |
||
| 96 | $this->assertSame([1.0, 2.0, 3.2], unwrap($sequence)); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function testStrings() |
||
| 100 | { |
||
| 101 | $sequence = Sequence::strings('1', '2', '3'); |
||
| 102 | |||
| 103 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 104 | $this->assertSame('string', $sequence->type()); |
||
| 105 | $this->assertSame(['1', '2', '3'], unwrap($sequence)); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function testObjects() |
||
| 109 | { |
||
| 110 | $a = new \stdClass; |
||
| 111 | $b = new \stdClass; |
||
| 112 | $c = new \stdClass; |
||
| 113 | $sequence = Sequence::objects($a, $b, $c); |
||
| 114 | |||
| 115 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 116 | $this->assertSame('object', $sequence->type()); |
||
| 117 | $this->assertSame([$a, $b, $c], unwrap($sequence)); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testType() |
||
| 121 | { |
||
| 122 | $type = Sequence::of('int')->type(); |
||
| 123 | |||
| 124 | $this->assertSame('int', $type); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function testSize() |
||
| 128 | { |
||
| 129 | $this->assertSame( |
||
| 130 | 2, |
||
| 131 | Sequence::of('int') |
||
| 132 | ->add(1) |
||
| 133 | ->add(2) |
||
| 134 | ->size() |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function testCount() |
||
| 139 | { |
||
| 140 | $this->assertCount( |
||
| 141 | 2, |
||
| 142 | Sequence::of('int') |
||
| 143 | ->add(1) |
||
| 144 | ->add(2) |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function testGet() |
||
| 149 | { |
||
| 150 | $this->assertSame( |
||
| 151 | 1, |
||
| 152 | Sequence::of('int')->add(1)->get(0) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function testThrowWhenGettingUnknownIndex() |
||
| 157 | { |
||
| 158 | $this->expectException(OutOfBoundException::class); |
||
| 159 | |||
| 160 | Sequence::of('int')->get(0); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function testDiff() |
||
| 164 | { |
||
| 165 | $a = Sequence::of('int') |
||
| 166 | ->add(1) |
||
| 167 | ->add(2) |
||
| 168 | ->add(3); |
||
| 169 | $b = Sequence::of('int') |
||
| 170 | ->add(3) |
||
| 171 | ->add(4) |
||
| 172 | ->add(5); |
||
| 173 | $c = $a->diff($b); |
||
| 174 | |||
| 175 | $this->assertInstanceOf(Sequence::class, $c); |
||
| 176 | $this->assertNotSame($c, $a); |
||
| 177 | $this->assertNotSame($c, $b); |
||
| 178 | $this->assertSame('int', $a->type()); |
||
| 179 | $this->assertSame('int', $b->type()); |
||
| 180 | $this->assertSame('int', $c->type()); |
||
| 181 | $this->assertSame([1, 2, 3], unwrap($a)); |
||
| 182 | $this->assertSame([3, 4, 5], unwrap($b)); |
||
| 183 | $this->assertSame([1, 2], unwrap($c)); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function testDistinct() |
||
| 187 | { |
||
| 188 | $a = Sequence::of('int') |
||
| 189 | ->add(1) |
||
| 190 | ->add(1) |
||
| 191 | ->add(1); |
||
| 192 | $b = $a->distinct(); |
||
| 193 | |||
| 194 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 195 | $this->assertNotSame($a, $b); |
||
| 196 | $this->assertSame('int', $a->type()); |
||
| 197 | $this->assertSame('int', $b->type()); |
||
| 198 | $this->assertSame([1, 1, 1], unwrap($a)); |
||
| 199 | $this->assertSame([1], unwrap($b)); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function testDrop() |
||
| 203 | { |
||
| 204 | $a = Sequence::of('int') |
||
| 205 | ->add(1) |
||
| 206 | ->add(3) |
||
| 207 | ->add(5); |
||
| 208 | $b = $a->drop(2); |
||
| 209 | |||
| 210 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 211 | $this->assertNotSame($a, $b); |
||
| 212 | $this->assertSame('int', $a->type()); |
||
| 213 | $this->assertSame('int', $b->type()); |
||
| 214 | $this->assertSame([1, 3, 5], unwrap($a)); |
||
| 215 | $this->assertSame([5], unwrap($b)); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function testDropEnd() |
||
| 219 | { |
||
| 220 | $a = Sequence::of('int') |
||
| 221 | ->add(1) |
||
| 222 | ->add(3) |
||
| 223 | ->add(5); |
||
| 224 | $b = $a->dropEnd(2); |
||
| 225 | |||
| 226 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 227 | $this->assertNotSame($a, $b); |
||
| 228 | $this->assertSame('int', $a->type()); |
||
| 229 | $this->assertSame('int', $b->type()); |
||
| 230 | $this->assertSame([1, 3, 5], unwrap($a)); |
||
| 231 | $this->assertSame([1], unwrap($b)); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function testEquals() |
||
| 235 | { |
||
| 236 | $a = Sequence::of('int') |
||
| 237 | ->add(1) |
||
| 238 | ->add(3) |
||
| 239 | ->add(5); |
||
| 240 | $b = Sequence::of('int') |
||
| 241 | ->add(1) |
||
| 242 | ->add(5); |
||
| 243 | $c = Sequence::of('int') |
||
| 244 | ->add(1) |
||
| 245 | ->add(3) |
||
| 246 | ->add(5); |
||
| 247 | |||
| 248 | $this->assertTrue($a->equals($c)); |
||
| 249 | $this->assertTrue($a->equals($a)); |
||
| 250 | $this->assertFalse($a->equals($b)); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function testThrowWhenTryingToTestEqualityForDifferentTypes() |
||
| 254 | { |
||
| 255 | $this->expectException(\TypeError::class); |
||
| 256 | $this->expectExceptionMessage('Argument 1 must be of type Sequence<int>, Sequence<stdClass> given'); |
||
| 257 | |||
| 258 | Sequence::of('int')->equals(Sequence::of('stdClass')); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function testFilter() |
||
| 262 | { |
||
| 263 | $a = Sequence::of('int') |
||
| 264 | ->add(1) |
||
| 265 | ->add(2) |
||
| 266 | ->add(3) |
||
| 267 | ->add(4); |
||
| 268 | $b = $a->filter(function(int $value): bool { |
||
| 269 | return $value % 2 === 0; |
||
| 270 | }); |
||
| 271 | |||
| 272 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 273 | $this->assertNotSame($a, $b); |
||
| 274 | $this->assertSame('int', $a->type()); |
||
| 275 | $this->assertSame('int', $b->type()); |
||
| 276 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 277 | $this->assertSame([2, 4], unwrap($b)); |
||
| 278 | } |
||
| 279 | |||
| 280 | public function testForeach() |
||
| 281 | { |
||
| 282 | $sum = 0; |
||
| 283 | $sequence = Sequence::of('int') |
||
| 284 | ->add(1) |
||
| 285 | ->add(2) |
||
| 286 | ->add(3) |
||
| 287 | ->add(4) |
||
| 288 | ->foreach(function(int $value) use (&$sum) { |
||
| 289 | $sum += $value; |
||
| 290 | }); |
||
| 291 | |||
| 292 | $this->assertSame(10, $sum); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function testGroupBy() |
||
| 296 | { |
||
| 297 | $sequence = Sequence::of('int') |
||
| 298 | ->add(1) |
||
| 299 | ->add(2) |
||
| 300 | ->add(3) |
||
| 301 | ->add(4); |
||
| 302 | $map = $sequence->groupBy(function(int $value): int { |
||
| 303 | return $value % 3; |
||
| 304 | }); |
||
| 305 | |||
| 306 | $this->assertInstanceOf(Map::class, $map); |
||
| 307 | $this->assertSame('int', $map->keyType()); |
||
| 308 | $this->assertSame(Sequence::class, $map->valueType()); |
||
| 309 | $this->assertCount(3, $map); |
||
| 310 | $this->assertSame('int', $map->get(0)->type()); |
||
| 311 | $this->assertSame('int', $map->get(1)->type()); |
||
| 312 | $this->assertSame('int', $map->get(2)->type()); |
||
| 313 | $this->assertSame([3], unwrap($map->get(0))); |
||
| 314 | $this->assertSame([1, 4], unwrap($map->get(1))); |
||
| 315 | $this->assertSame([2], unwrap($map->get(2))); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function testThrowWhenGroupingEmptySequence() |
||
| 319 | { |
||
| 320 | $this->expectException(CannotGroupEmptyStructure::class); |
||
| 321 | |||
| 322 | Sequence::of('int')->groupBy(function() {}); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function testFirst() |
||
| 326 | { |
||
| 327 | $sequence = Sequence::of('int') |
||
| 328 | ->add(1) |
||
| 329 | ->add(2) |
||
| 330 | ->add(3) |
||
| 331 | ->add(4); |
||
| 332 | |||
| 333 | $this->assertSame(1, $sequence->first()); |
||
| 334 | } |
||
| 335 | |||
| 336 | public function testLast() |
||
| 337 | { |
||
| 338 | $sequence = Sequence::of('int') |
||
| 339 | ->add(1) |
||
| 340 | ->add(2) |
||
| 341 | ->add(3) |
||
| 342 | ->add(4); |
||
| 343 | |||
| 344 | $this->assertSame(4, $sequence->last()); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function testContains() |
||
| 348 | { |
||
| 349 | $sequence = Sequence::of('int') |
||
| 350 | ->add(1) |
||
| 351 | ->add(2) |
||
| 352 | ->add(3) |
||
| 353 | ->add(4); |
||
| 354 | |||
| 355 | $this->assertTrue($sequence->contains(2)); |
||
| 356 | $this->assertFalse($sequence->contains(5)); |
||
| 357 | } |
||
| 358 | |||
| 359 | public function testIndexOf() |
||
| 360 | { |
||
| 361 | $sequence = Sequence::of('int') |
||
| 362 | ->add(1) |
||
| 363 | ->add(2) |
||
| 364 | ->add(3) |
||
| 365 | ->add(4); |
||
| 366 | |||
| 367 | $this->assertSame(0, $sequence->indexOf(1)); |
||
| 368 | $this->assertSame(3, $sequence->indexOf(4)); |
||
| 369 | } |
||
| 370 | |||
| 371 | public function testIndices() |
||
| 372 | { |
||
| 373 | $sequence = Sequence::of('int') |
||
| 374 | ->add(1) |
||
| 375 | ->add(2) |
||
| 376 | ->add(3) |
||
| 377 | ->add(4); |
||
| 378 | $indices = $sequence->indices(); |
||
| 379 | |||
| 380 | $this->assertInstanceOf(Sequence::class, $indices); |
||
| 381 | $this->assertSame('int', $indices->type()); |
||
| 382 | $this->assertSame([0, 1, 2, 3], unwrap($indices)); |
||
| 383 | } |
||
| 384 | |||
| 385 | public function testEmptyIndices() |
||
| 386 | { |
||
| 387 | $sequence = Sequence::of('int'); |
||
| 388 | $indices = $sequence->indices(); |
||
| 389 | |||
| 390 | $this->assertInstanceOf(Sequence::class, $indices); |
||
| 391 | $this->assertSame('int', $indices->type()); |
||
| 392 | $this->assertSame([], unwrap($indices)); |
||
| 393 | } |
||
| 394 | |||
| 395 | public function testMap() |
||
| 396 | { |
||
| 397 | $a = Sequence::of('int') |
||
| 398 | ->add(1) |
||
| 399 | ->add(2) |
||
| 400 | ->add(3) |
||
| 401 | ->add(4); |
||
| 402 | $b = $a->map(function(int $value): int { |
||
| 403 | return $value**2; |
||
| 404 | }); |
||
| 405 | |||
| 406 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 407 | $this->assertNotSame($a, $b); |
||
| 408 | $this->assertSame('int', $a->type()); |
||
| 409 | $this->assertSame('int', $b->type()); |
||
| 410 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 411 | $this->assertSame([1, 4, 9, 16], unwrap($b)); |
||
| 412 | } |
||
| 413 | |||
| 414 | public function testThrowWhenTryingToModifyValueTypeInMap() |
||
| 415 | { |
||
| 416 | $this->expectException(\TypeError::class); |
||
| 417 | $this->expectExceptionMessage('Argument 1 must be of type int, string given'); |
||
| 418 | |||
| 419 | Sequence::of('int') |
||
| 420 | ->add(1) |
||
| 421 | ->add(2) |
||
| 422 | ->add(3) |
||
| 423 | ->add(4) |
||
| 424 | ->map(function(int $value) { |
||
| 425 | return (string) $value; |
||
| 426 | }); |
||
| 427 | } |
||
| 428 | |||
| 429 | public function testPad() |
||
| 430 | { |
||
| 431 | $a = Sequence::of('int') |
||
| 432 | ->add(1) |
||
| 433 | ->add(2); |
||
| 434 | $b = $a->pad(4, 0); |
||
| 435 | |||
| 436 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 437 | $this->assertNotSame($a, $b); |
||
| 438 | $this->assertSame('int', $a->type()); |
||
| 439 | $this->assertSame('int', $b->type()); |
||
| 440 | $this->assertSame([1, 2], unwrap($a)); |
||
| 441 | $this->assertSame([1, 2, 0, 0], unwrap($b)); |
||
| 442 | } |
||
| 443 | |||
| 444 | public function testThrowWhenPaddingWithDifferentType() |
||
| 445 | { |
||
| 446 | $this->expectException(\TypeError::class); |
||
| 447 | $this->expectExceptionMessage('Argument 2 must be of type int, string given'); |
||
| 448 | |||
| 449 | Sequence::of('int')->pad(2, '0'); |
||
| 450 | } |
||
| 451 | |||
| 452 | public function testPartition() |
||
| 453 | { |
||
| 454 | $map = Sequence::of('int') |
||
| 455 | ->add(1) |
||
| 456 | ->add(2) |
||
| 457 | ->add(3) |
||
| 458 | ->add(4) |
||
| 459 | ->partition(function(int $value): bool { |
||
| 460 | return $value % 2 === 0; |
||
| 461 | }); |
||
| 462 | |||
| 463 | $this->assertInstanceOf(Map::class, $map); |
||
| 464 | $this->assertSame('bool', $map->keyType()); |
||
| 465 | $this->assertSame(Sequence::class, $map->valueType()); |
||
| 466 | $this->assertSame('int', $map->get(true)->type()); |
||
| 467 | $this->assertSame('int', $map->get(false)->type()); |
||
| 468 | $this->assertSame([2, 4], unwrap($map->get(true))); |
||
| 469 | $this->assertSame([1, 3], unwrap($map->get(false))); |
||
| 470 | } |
||
| 471 | |||
| 472 | public function testSlice() |
||
| 473 | { |
||
| 474 | $a = Sequence::of('int') |
||
| 475 | ->add(1) |
||
| 476 | ->add(2) |
||
| 477 | ->add(3) |
||
| 478 | ->add(4); |
||
| 479 | $b = $a->slice(1, 3); |
||
| 480 | |||
| 481 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 482 | $this->assertNotSame($a, $b); |
||
| 483 | $this->assertSame('int', $a->type()); |
||
| 484 | $this->assertSame('int', $b->type()); |
||
| 485 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 486 | $this->assertSame([2, 3], unwrap($b)); |
||
| 487 | } |
||
| 488 | |||
| 489 | public function testSplitAt() |
||
| 490 | { |
||
| 491 | $a = Sequence::of('int') |
||
| 492 | ->add(1) |
||
| 493 | ->add(2) |
||
| 494 | ->add(3) |
||
| 495 | ->add(4); |
||
| 496 | $b = $a->splitAt(2); |
||
| 497 | |||
| 498 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 499 | $this->assertNotSame($a, $b); |
||
| 500 | $this->assertSame('int', $a->type()); |
||
| 501 | $this->assertSame(Sequence::class, $b->type()); |
||
| 502 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 503 | $this->assertSame('int', $b->first()->type()); |
||
| 504 | $this->assertSame('int', $b->last()->type()); |
||
| 505 | $this->assertSame([1, 2], unwrap($b->first())); |
||
| 506 | $this->assertSame([3, 4], unwrap($b->last())); |
||
| 507 | } |
||
| 508 | |||
| 509 | public function testTake() |
||
| 510 | { |
||
| 511 | $a = Sequence::of('int') |
||
| 512 | ->add(1) |
||
| 513 | ->add(2) |
||
| 514 | ->add(3) |
||
| 515 | ->add(4); |
||
| 516 | $b = $a->take(2); |
||
| 517 | |||
| 518 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 519 | $this->assertNotSame($a, $b); |
||
| 520 | $this->assertSame('int', $a->type()); |
||
| 521 | $this->assertSame('int', $b->type()); |
||
| 522 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 523 | $this->assertSame([1, 2], unwrap($b)); |
||
| 524 | } |
||
| 525 | |||
| 526 | public function testTakeEnd() |
||
| 527 | { |
||
| 528 | $a = Sequence::of('int') |
||
| 529 | ->add(1) |
||
| 530 | ->add(2) |
||
| 531 | ->add(3) |
||
| 532 | ->add(4); |
||
| 533 | $b = $a->takeEnd(2); |
||
| 534 | |||
| 535 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 536 | $this->assertNotSame($a, $b); |
||
| 537 | $this->assertSame('int', $a->type()); |
||
| 538 | $this->assertSame('int', $b->type()); |
||
| 539 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 540 | $this->assertSame([3, 4], unwrap($b)); |
||
| 541 | } |
||
| 542 | |||
| 543 | public function testAppend() |
||
| 544 | { |
||
| 545 | $a = Sequence::of('int') |
||
| 546 | ->add(1) |
||
| 547 | ->add(2); |
||
| 548 | $b = Sequence::of('int') |
||
| 549 | ->add(3) |
||
| 550 | ->add(4); |
||
| 551 | $c = $b->append($a); |
||
| 552 | |||
| 553 | $this->assertInstanceOf(Sequence::class, $c); |
||
| 554 | $this->assertNotSame($c, $a); |
||
| 555 | $this->assertNotSame($c, $b); |
||
| 556 | $this->assertSame('int', $a->type()); |
||
| 557 | $this->assertSame('int', $b->type()); |
||
| 558 | $this->assertSame('int', $c->type()); |
||
| 559 | $this->assertSame([1, 2], unwrap($a)); |
||
| 560 | $this->assertSame([3, 4], unwrap($b)); |
||
| 561 | $this->assertSame([3, 4, 1, 2], unwrap($c)); |
||
| 562 | } |
||
| 563 | |||
| 564 | public function testThrowWhenAppendingDifferentTypes() |
||
| 565 | { |
||
| 566 | $this->expectException(\TypeError::class); |
||
| 567 | $this->expectExceptionMessage('Argument 1 must be of type Sequence<int>, Sequence<stdClass> given'); |
||
| 568 | |||
| 569 | Sequence::of('int')->append(Sequence::of('stdClass')); |
||
| 570 | } |
||
| 571 | |||
| 572 | public function testIntersect() |
||
| 573 | { |
||
| 574 | $a = Sequence::of('int') |
||
| 575 | ->add(1) |
||
| 576 | ->add(2); |
||
| 577 | $b = Sequence::of('int') |
||
| 578 | ->add(2) |
||
| 579 | ->add(3); |
||
| 580 | $c = $b->intersect($a); |
||
| 581 | |||
| 582 | $this->assertInstanceOf(Sequence::class, $c); |
||
| 583 | $this->assertNotSame($c, $a); |
||
| 584 | $this->assertNotSame($c, $b); |
||
| 585 | $this->assertSame('int', $a->type()); |
||
| 586 | $this->assertSame('int', $b->type()); |
||
| 587 | $this->assertSame('int', $c->type()); |
||
| 588 | $this->assertSame([1, 2], unwrap($a)); |
||
| 589 | $this->assertSame([2, 3], unwrap($b)); |
||
| 590 | $this->assertSame([2], unwrap($c)); |
||
| 591 | } |
||
| 592 | |||
| 593 | public function testThrowWhenIntersectingDifferentTypes() |
||
| 594 | { |
||
| 595 | $this->expectException(\TypeError::class); |
||
| 596 | $this->expectExceptionMessage('Argument 1 must be of type Sequence<int>, Sequence<stdClass> given'); |
||
| 597 | |||
| 598 | Sequence::of('int')->intersect(Sequence::of('stdClass')); |
||
| 599 | } |
||
| 600 | |||
| 601 | public function testAdd() |
||
| 602 | { |
||
| 603 | $a = Sequence::of('int'); |
||
| 604 | $b = $a->add(1); |
||
| 605 | |||
| 606 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 607 | $this->assertNotSame($a, $b); |
||
| 608 | $this->assertSame('int', $a->type()); |
||
| 609 | $this->assertSame('int', $b->type()); |
||
| 610 | $this->assertSame([], unwrap($a)); |
||
| 611 | $this->assertSame([1], unwrap($b)); |
||
| 612 | |||
| 613 | $this->assertSame( |
||
| 614 | [1, 2, 3], |
||
| 615 | unwrap(Sequence::ints(1)(2)(3)), |
||
| 616 | ); |
||
| 617 | } |
||
| 618 | |||
| 619 | public function testThrowWhenAddingInvalidType() |
||
| 620 | { |
||
| 621 | $this->expectException(\TypeError::class); |
||
| 622 | $this->expectExceptionMessage('Argument 1 must be of type int, float given'); |
||
| 623 | |||
| 624 | Sequence::of('int')->add(4.2); |
||
| 625 | } |
||
| 626 | |||
| 627 | public function testSort() |
||
| 628 | { |
||
| 629 | $a = Sequence::of('int') |
||
| 630 | ->add(1) |
||
| 631 | ->add(2) |
||
| 632 | ->add(3) |
||
| 633 | ->add(3) |
||
| 634 | ->add(4); |
||
| 635 | $b = $a->sort(function(int $a, int $b): bool { |
||
| 636 | return $b > $a; |
||
| 637 | }); |
||
| 638 | |||
| 639 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 640 | $this->assertNotSame($a, $b); |
||
| 641 | $this->assertSame('int', $a->type()); |
||
| 642 | $this->assertSame('int', $b->type()); |
||
| 643 | $this->assertSame([1, 2, 3, 3, 4], unwrap($a)); |
||
| 644 | $this->assertSame([4, 3, 3, 2, 1], unwrap($b)); |
||
| 645 | } |
||
| 646 | |||
| 647 | public function testReduce() |
||
| 648 | { |
||
| 649 | $value = Sequence::of('int') |
||
| 650 | ->add(1) |
||
| 651 | ->add(2) |
||
| 652 | ->add(3) |
||
| 653 | ->add(4) |
||
| 654 | ->reduce( |
||
| 655 | 0, |
||
| 656 | function(int $carry, int $value): int { |
||
| 657 | return $carry + $value; |
||
| 658 | } |
||
| 659 | ); |
||
| 660 | |||
| 661 | $this->assertSame(10, $value); |
||
| 662 | } |
||
| 663 | |||
| 664 | public function testClear() |
||
| 676 | } |
||
| 677 | |||
| 678 | public function testReverse() |
||
| 679 | { |
||
| 680 | $sequence = Sequence::of('int') |
||
| 681 | ->add(1) |
||
| 682 | ->add(3) |
||
| 683 | ->add(4) |
||
| 684 | ->add(2); |
||
| 685 | $reverse = $sequence->reverse(); |
||
| 686 | |||
| 687 | $this->assertInstanceOf(Sequence::class, $reverse); |
||
| 688 | $this->assertNotSame($sequence, $reverse); |
||
| 689 | $this->assertSame([1, 3, 4, 2], unwrap($sequence)); |
||
| 690 | $this->assertSame([2, 4, 3, 1], unwrap($reverse)); |
||
| 691 | } |
||
| 692 | |||
| 693 | public function testEmpty() |
||
| 694 | { |
||
| 695 | $this->assertTrue(Sequence::of('int')->empty()); |
||
| 696 | $this->assertFalse(Sequence::of('int', 1)->empty()); |
||
| 697 | } |
||
| 698 | |||
| 699 | public function testToSequenceOf() |
||
| 700 | { |
||
| 701 | $initial = Sequence::ints(1, 2, 3); |
||
| 702 | $sequence = $initial->toSequenceOf('string|int', function($i) { |
||
| 703 | yield (string) $i; |
||
| 704 | yield $i; |
||
| 705 | }); |
||
| 706 | |||
| 707 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 708 | $this->assertSame( |
||
| 709 | ['1', 1, '2', 2, '3', 3], |
||
| 710 | unwrap($sequence), |
||
| 711 | ); |
||
| 712 | $this->assertSame( |
||
| 713 | [1, 2, 3], |
||
| 714 | unwrap($initial->toSequenceOf('int')), |
||
| 715 | ); |
||
| 716 | } |
||
| 717 | |||
| 718 | public function testToSetOf() |
||
| 719 | { |
||
| 720 | $sequence = Sequence::ints(1, 2, 3); |
||
| 721 | $set = $sequence->toSetOf('string|int', function($i) { |
||
| 722 | yield (string) $i; |
||
| 723 | yield $i; |
||
| 724 | }); |
||
| 725 | |||
| 726 | $this->assertInstanceOf(Set::class, $set); |
||
| 727 | $this->assertSame( |
||
| 728 | ['1', 1, '2', 2, '3', 3], |
||
| 729 | unwrap($set), |
||
| 730 | ); |
||
| 731 | $this->assertSame( |
||
| 732 | [1, 2, 3], |
||
| 733 | unwrap($sequence->add(1)->toSetOf('int')), |
||
| 734 | ); |
||
| 735 | } |
||
| 736 | |||
| 737 | public function testToMapOf() |
||
| 747 | } |
||
| 748 | } |
||
| 749 |