|
7 | 7 | use ArrayIterator;
|
8 | 8 | use Assert;
|
9 | 9 | use GeekCell\Ddd\Domain\Collection;
|
| 10 | +use InvalidArgumentException; |
10 | 11 | use PHPUnit\Framework\TestCase;
|
11 | 12 |
|
12 | 13 | /**
|
@@ -431,4 +432,98 @@ public function testSomeShortCircuitsOnFirstFalsyValue(): void
|
431 | 432 | return true;
|
432 | 433 | });
|
433 | 434 | }
|
| 435 | + |
| 436 | + public function testFirst(): void |
| 437 | + { |
| 438 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 439 | + $collection = new Collection($items); |
| 440 | + |
| 441 | + $this->assertSame(1, $collection->first()); |
| 442 | + } |
| 443 | + |
| 444 | + public function testFirstThrowsExceptionOnEmptyCollection(): void |
| 445 | + { |
| 446 | + $collection = new Collection([]); |
| 447 | + |
| 448 | + $this->expectException(InvalidArgumentException::class); |
| 449 | + $this->expectExceptionMessage('No items in collection'); |
| 450 | + $collection->first(); |
| 451 | + } |
| 452 | + |
| 453 | + public function testFirstThrowsExceptionIfCallbackIsNeverSatisfied(): void |
| 454 | + { |
| 455 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 456 | + $collection = new Collection($items); |
| 457 | + $this->expectException(InvalidArgumentException::class); |
| 458 | + $this->expectExceptionMessage('No item found in collection that satisfies first callback'); |
| 459 | + $collection->first(static fn () => false); |
| 460 | + } |
| 461 | + |
| 462 | + public function testFirstOrReturnsFirstValueInCollectionIfNoCallbackIsGiven(): void |
| 463 | + { |
| 464 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 465 | + $collection = new Collection($items); |
| 466 | + $this->assertSame(1, $collection->firstOr()); |
| 467 | + } |
| 468 | + |
| 469 | + public function testFirstOrReturnsFirstValueThatSatisfiesCallback(): void |
| 470 | + { |
| 471 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 472 | + $collection = new Collection($items); |
| 473 | + $this->assertSame(6, $collection->firstOr(static fn ($item) => $item > 5)); |
| 474 | + } |
| 475 | + |
| 476 | + public function testFirstOrReturnsFallbackValueIfCallbackIsNeverSatisfied(): void |
| 477 | + { |
| 478 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 479 | + $collection = new Collection($items); |
| 480 | + $this->assertSame(-1, $collection->firstOr(static fn ($item) => $item > 10, -1)); |
| 481 | + } |
| 482 | + |
| 483 | + public function testLast(): void |
| 484 | + { |
| 485 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 486 | + $collection = new Collection($items); |
| 487 | + |
| 488 | + $this->assertSame(10, $collection->last()); |
| 489 | + } |
| 490 | + |
| 491 | + public function testLastThrowsExceptionOnEmptyCollection(): void |
| 492 | + { |
| 493 | + $collection = new Collection([]); |
| 494 | + |
| 495 | + $this->expectException(InvalidArgumentException::class); |
| 496 | + $this->expectExceptionMessage('No items in collection'); |
| 497 | + $collection->last(); |
| 498 | + } |
| 499 | + |
| 500 | + public function testLastThrowsExceptionIfCallbackIsNeverSatisfied(): void |
| 501 | + { |
| 502 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 503 | + $collection = new Collection($items); |
| 504 | + $this->expectException(InvalidArgumentException::class); |
| 505 | + $this->expectExceptionMessage('No item found in collection that satisfies last callback'); |
| 506 | + $collection->last(static fn () => false); |
| 507 | + } |
| 508 | + |
| 509 | + public function testLastOrReturnsLastValueInCollectionIfNoCallbackIsGiven(): void |
| 510 | + { |
| 511 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 512 | + $collection = new Collection($items); |
| 513 | + $this->assertSame(10, $collection->lastOr()); |
| 514 | + } |
| 515 | + |
| 516 | + public function testLastOrReturnsLastValueThatSatisfiesCallback(): void |
| 517 | + { |
| 518 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 519 | + $collection = new Collection($items); |
| 520 | + $this->assertSame(10, $collection->lastOr(static fn ($item) => $item > 5)); |
| 521 | + } |
| 522 | + |
| 523 | + public function testLastOrReturnsFallbackValueIfCallbackIsNeverSatisfied(): void |
| 524 | + { |
| 525 | + $items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 526 | + $collection = new Collection($items); |
| 527 | + $this->assertSame(-1, $collection->lastOr(static fn ($item) => $item > 10, -1)); |
| 528 | + } |
434 | 529 | }
|
0 commit comments