| Total Complexity | 54 |
| Total Lines | 740 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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() |
||
| 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() |
||
| 369 | } |
||
| 370 | |||
| 371 | public function testIndices() |
||
| 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 { |
||
| 412 | } |
||
| 413 | |||
| 414 | public function testMapTo() |
||
| 415 | { |
||
| 416 | $a = Sequence::ints(1, 2, 3, 4); |
||
| 417 | $b = $a->mapTo('string', fn($i) => (string) $i); |
||
| 418 | |||
| 419 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 420 | $this->assertNotSame($a, $b); |
||
| 421 | $this->assertSame('string', $b->type()); |
||
| 422 | $this->assertSame(['1', '2', '3', '4'], unwrap($b)); |
||
| 423 | } |
||
| 424 | |||
| 425 | public function testThrowWhenTryingToModifyValueTypeInMap() |
||
| 426 | { |
||
| 427 | $this->expectException(\TypeError::class); |
||
| 428 | $this->expectExceptionMessage('Argument 1 must be of type int, string given'); |
||
| 429 | |||
| 430 | Sequence::of('int') |
||
| 431 | ->add(1) |
||
| 432 | ->add(2) |
||
| 433 | ->add(3) |
||
| 434 | ->add(4) |
||
| 435 | ->map(function(int $value) { |
||
| 436 | return (string) $value; |
||
| 437 | }); |
||
| 438 | } |
||
| 439 | |||
| 440 | public function testPad() |
||
| 441 | { |
||
| 442 | $a = Sequence::of('int') |
||
| 443 | ->add(1) |
||
| 444 | ->add(2); |
||
| 445 | $b = $a->pad(4, 0); |
||
| 446 | |||
| 447 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 448 | $this->assertNotSame($a, $b); |
||
| 449 | $this->assertSame('int', $a->type()); |
||
| 450 | $this->assertSame('int', $b->type()); |
||
| 451 | $this->assertSame([1, 2], unwrap($a)); |
||
| 452 | $this->assertSame([1, 2, 0, 0], unwrap($b)); |
||
| 453 | } |
||
| 454 | |||
| 455 | public function testThrowWhenPaddingWithDifferentType() |
||
| 456 | { |
||
| 457 | $this->expectException(\TypeError::class); |
||
| 458 | $this->expectExceptionMessage('Argument 2 must be of type int, string given'); |
||
| 459 | |||
| 460 | Sequence::of('int')->pad(2, '0'); |
||
| 461 | } |
||
| 462 | |||
| 463 | public function testPartition() |
||
| 464 | { |
||
| 465 | $map = Sequence::of('int') |
||
| 466 | ->add(1) |
||
| 467 | ->add(2) |
||
| 468 | ->add(3) |
||
| 469 | ->add(4) |
||
| 470 | ->partition(function(int $value): bool { |
||
| 471 | return $value % 2 === 0; |
||
| 472 | }); |
||
| 473 | |||
| 474 | $this->assertInstanceOf(Map::class, $map); |
||
| 475 | $this->assertSame('bool', $map->keyType()); |
||
| 476 | $this->assertSame(Sequence::class, $map->valueType()); |
||
| 477 | $this->assertSame('int', $map->get(true)->type()); |
||
| 478 | $this->assertSame('int', $map->get(false)->type()); |
||
| 479 | $this->assertSame([2, 4], unwrap($map->get(true))); |
||
| 480 | $this->assertSame([1, 3], unwrap($map->get(false))); |
||
| 481 | } |
||
| 482 | |||
| 483 | public function testSlice() |
||
| 484 | { |
||
| 485 | $a = Sequence::of('int') |
||
| 486 | ->add(1) |
||
| 487 | ->add(2) |
||
| 488 | ->add(3) |
||
| 489 | ->add(4); |
||
| 490 | $b = $a->slice(1, 3); |
||
| 491 | |||
| 492 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 493 | $this->assertNotSame($a, $b); |
||
| 494 | $this->assertSame('int', $a->type()); |
||
| 495 | $this->assertSame('int', $b->type()); |
||
| 496 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 497 | $this->assertSame([2, 3], unwrap($b)); |
||
| 498 | } |
||
| 499 | |||
| 500 | public function testSplitAt() |
||
| 501 | { |
||
| 502 | $a = Sequence::of('int') |
||
| 503 | ->add(1) |
||
| 504 | ->add(2) |
||
| 505 | ->add(3) |
||
| 506 | ->add(4); |
||
| 507 | $b = $a->splitAt(2); |
||
| 508 | |||
| 509 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 510 | $this->assertNotSame($a, $b); |
||
| 511 | $this->assertSame('int', $a->type()); |
||
| 512 | $this->assertSame(Sequence::class, $b->type()); |
||
| 513 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 514 | $this->assertSame('int', $b->first()->type()); |
||
| 515 | $this->assertSame('int', $b->last()->type()); |
||
| 516 | $this->assertSame([1, 2], unwrap($b->first())); |
||
| 517 | $this->assertSame([3, 4], unwrap($b->last())); |
||
| 518 | } |
||
| 519 | |||
| 520 | public function testTake() |
||
| 521 | { |
||
| 522 | $a = Sequence::of('int') |
||
| 523 | ->add(1) |
||
| 524 | ->add(2) |
||
| 525 | ->add(3) |
||
| 526 | ->add(4); |
||
| 527 | $b = $a->take(2); |
||
| 528 | |||
| 529 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 530 | $this->assertNotSame($a, $b); |
||
| 531 | $this->assertSame('int', $a->type()); |
||
| 532 | $this->assertSame('int', $b->type()); |
||
| 533 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 534 | $this->assertSame([1, 2], unwrap($b)); |
||
| 535 | } |
||
| 536 | |||
| 537 | public function testTakeEnd() |
||
| 538 | { |
||
| 539 | $a = Sequence::of('int') |
||
| 540 | ->add(1) |
||
| 541 | ->add(2) |
||
| 542 | ->add(3) |
||
| 543 | ->add(4); |
||
| 544 | $b = $a->takeEnd(2); |
||
| 545 | |||
| 546 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 547 | $this->assertNotSame($a, $b); |
||
| 548 | $this->assertSame('int', $a->type()); |
||
| 549 | $this->assertSame('int', $b->type()); |
||
| 550 | $this->assertSame([1, 2, 3, 4], unwrap($a)); |
||
| 551 | $this->assertSame([3, 4], unwrap($b)); |
||
| 552 | } |
||
| 553 | |||
| 554 | public function testAppend() |
||
| 555 | { |
||
| 556 | $a = Sequence::of('int') |
||
| 557 | ->add(1) |
||
| 558 | ->add(2); |
||
| 559 | $b = Sequence::of('int') |
||
| 560 | ->add(3) |
||
| 561 | ->add(4); |
||
| 562 | $c = $b->append($a); |
||
| 563 | |||
| 564 | $this->assertInstanceOf(Sequence::class, $c); |
||
| 565 | $this->assertNotSame($c, $a); |
||
| 566 | $this->assertNotSame($c, $b); |
||
| 567 | $this->assertSame('int', $a->type()); |
||
| 568 | $this->assertSame('int', $b->type()); |
||
| 569 | $this->assertSame('int', $c->type()); |
||
| 570 | $this->assertSame([1, 2], unwrap($a)); |
||
| 571 | $this->assertSame([3, 4], unwrap($b)); |
||
| 572 | $this->assertSame([3, 4, 1, 2], unwrap($c)); |
||
| 573 | } |
||
| 574 | |||
| 575 | public function testThrowWhenAppendingDifferentTypes() |
||
| 576 | { |
||
| 577 | $this->expectException(\TypeError::class); |
||
| 578 | $this->expectExceptionMessage('Argument 1 must be of type Sequence<int>, Sequence<stdClass> given'); |
||
| 579 | |||
| 580 | Sequence::of('int')->append(Sequence::of('stdClass')); |
||
| 581 | } |
||
| 582 | |||
| 583 | public function testIntersect() |
||
| 584 | { |
||
| 585 | $a = Sequence::of('int') |
||
| 586 | ->add(1) |
||
| 587 | ->add(2); |
||
| 588 | $b = Sequence::of('int') |
||
| 589 | ->add(2) |
||
| 590 | ->add(3); |
||
| 591 | $c = $b->intersect($a); |
||
| 592 | |||
| 593 | $this->assertInstanceOf(Sequence::class, $c); |
||
| 594 | $this->assertNotSame($c, $a); |
||
| 595 | $this->assertNotSame($c, $b); |
||
| 596 | $this->assertSame('int', $a->type()); |
||
| 597 | $this->assertSame('int', $b->type()); |
||
| 598 | $this->assertSame('int', $c->type()); |
||
| 599 | $this->assertSame([1, 2], unwrap($a)); |
||
| 600 | $this->assertSame([2, 3], unwrap($b)); |
||
| 601 | $this->assertSame([2], unwrap($c)); |
||
| 602 | } |
||
| 603 | |||
| 604 | public function testThrowWhenIntersectingDifferentTypes() |
||
| 605 | { |
||
| 606 | $this->expectException(\TypeError::class); |
||
| 607 | $this->expectExceptionMessage('Argument 1 must be of type Sequence<int>, Sequence<stdClass> given'); |
||
| 608 | |||
| 609 | Sequence::of('int')->intersect(Sequence::of('stdClass')); |
||
| 610 | } |
||
| 611 | |||
| 612 | public function testAdd() |
||
| 613 | { |
||
| 614 | $a = Sequence::of('int'); |
||
| 615 | $b = $a->add(1); |
||
| 616 | |||
| 617 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 618 | $this->assertNotSame($a, $b); |
||
| 619 | $this->assertSame('int', $a->type()); |
||
| 620 | $this->assertSame('int', $b->type()); |
||
| 621 | $this->assertSame([], unwrap($a)); |
||
| 622 | $this->assertSame([1], unwrap($b)); |
||
| 623 | |||
| 624 | $this->assertSame( |
||
| 625 | [1, 2, 3], |
||
| 626 | unwrap(Sequence::ints(1)(2)(3)), |
||
| 627 | ); |
||
| 628 | } |
||
| 629 | |||
| 630 | public function testThrowWhenAddingInvalidType() |
||
| 631 | { |
||
| 632 | $this->expectException(\TypeError::class); |
||
| 633 | $this->expectExceptionMessage('Argument 1 must be of type int, float given'); |
||
| 634 | |||
| 635 | Sequence::of('int')->add(4.2); |
||
| 636 | } |
||
| 637 | |||
| 638 | public function testSort() |
||
| 639 | { |
||
| 640 | $a = Sequence::of('int') |
||
| 641 | ->add(1) |
||
| 642 | ->add(2) |
||
| 643 | ->add(3) |
||
| 644 | ->add(3) |
||
| 645 | ->add(4); |
||
| 646 | $b = $a->sort(function(int $a, int $b): bool { |
||
| 647 | return $b > $a; |
||
| 648 | }); |
||
| 649 | |||
| 650 | $this->assertInstanceOf(Sequence::class, $b); |
||
| 651 | $this->assertNotSame($a, $b); |
||
| 652 | $this->assertSame('int', $a->type()); |
||
| 653 | $this->assertSame('int', $b->type()); |
||
| 654 | $this->assertSame([1, 2, 3, 3, 4], unwrap($a)); |
||
| 655 | $this->assertSame([4, 3, 3, 2, 1], unwrap($b)); |
||
| 656 | } |
||
| 657 | |||
| 658 | public function testReduce() |
||
| 659 | { |
||
| 660 | $value = Sequence::of('int') |
||
| 661 | ->add(1) |
||
| 662 | ->add(2) |
||
| 663 | ->add(3) |
||
| 664 | ->add(4) |
||
| 665 | ->reduce( |
||
| 666 | 0, |
||
| 667 | function(int $carry, int $value): int { |
||
| 668 | return $carry + $value; |
||
| 669 | } |
||
| 670 | ); |
||
| 671 | |||
| 672 | $this->assertSame(10, $value); |
||
| 673 | } |
||
| 674 | |||
| 675 | public function testClear() |
||
| 687 | } |
||
| 688 | |||
| 689 | public function testReverse() |
||
| 690 | { |
||
| 691 | $sequence = Sequence::of('int') |
||
| 692 | ->add(1) |
||
| 693 | ->add(3) |
||
| 694 | ->add(4) |
||
| 695 | ->add(2); |
||
| 696 | $reverse = $sequence->reverse(); |
||
| 697 | |||
| 698 | $this->assertInstanceOf(Sequence::class, $reverse); |
||
| 699 | $this->assertNotSame($sequence, $reverse); |
||
| 700 | $this->assertSame([1, 3, 4, 2], unwrap($sequence)); |
||
| 701 | $this->assertSame([2, 4, 3, 1], unwrap($reverse)); |
||
| 702 | } |
||
| 703 | |||
| 704 | public function testEmpty() |
||
| 705 | { |
||
| 706 | $this->assertTrue(Sequence::of('int')->empty()); |
||
| 707 | $this->assertFalse(Sequence::of('int', 1)->empty()); |
||
| 708 | } |
||
| 709 | |||
| 710 | public function testToSequenceOf() |
||
| 711 | { |
||
| 712 | $initial = Sequence::ints(1, 2, 3); |
||
| 713 | $sequence = $initial->toSequenceOf('string|int', function($i) { |
||
| 714 | yield (string) $i; |
||
| 715 | yield $i; |
||
| 716 | }); |
||
| 717 | |||
| 718 | $this->assertInstanceOf(Sequence::class, $sequence); |
||
| 719 | $this->assertSame( |
||
| 720 | ['1', 1, '2', 2, '3', 3], |
||
| 721 | unwrap($sequence), |
||
| 722 | ); |
||
| 723 | $this->assertSame( |
||
| 724 | [1, 2, 3], |
||
| 725 | unwrap($initial->toSequenceOf('int')), |
||
| 726 | ); |
||
| 727 | } |
||
| 728 | |||
| 729 | public function testToSetOf() |
||
| 730 | { |
||
| 731 | $sequence = Sequence::ints(1, 2, 3); |
||
| 732 | $set = $sequence->toSetOf('string|int', function($i) { |
||
| 733 | yield (string) $i; |
||
| 734 | yield $i; |
||
| 735 | }); |
||
| 736 | |||
| 737 | $this->assertInstanceOf(Set::class, $set); |
||
| 738 | $this->assertSame( |
||
| 739 | ['1', 1, '2', 2, '3', 3], |
||
| 740 | unwrap($set), |
||
| 741 | ); |
||
| 742 | $this->assertSame( |
||
| 743 | [1, 2, 3], |
||
| 744 | unwrap($sequence->add(1)->toSetOf('int')), |
||
| 745 | ); |
||
| 746 | } |
||
| 747 | |||
| 748 | public function testToMapOf() |
||
| 758 | } |
||
| 759 | } |
||
| 760 |