Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add firstOrFail() and lastOrFail() methods #8

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/AbstractImmutableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ArrayIterator;
use Headsnet\Collections\Exception\ImmutabilityException;
use Headsnet\Collections\Exception\InvalidTypeException;
use Headsnet\Collections\Exception\ItemNotFoundException;
use Headsnet\Collections\Exception\OutOfRangeException;

/**
Expand Down Expand Up @@ -77,6 +78,14 @@ public function first()
return reset($this->items) ?: null;
}

/**
* @return TValue
*/
public function firstOrFail()
{
return reset($this->items) ?: throw new ItemNotFoundException();
}

/**
* @return TValue|null
*/
Expand All @@ -85,6 +94,14 @@ public function last()
return end($this->items) ?: null;
}

/**
* @return TValue
*/
public function lastOrFail()
{
return end($this->items) ?: throw new ItemNotFoundException();
}

/**
* @param AbstractImmutableCollection<TValue> $compareWith
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/ItemNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Headsnet\Collections\Exception;

final class ItemNotFoundException extends AbstractCollectionException
{
}
41 changes: 41 additions & 0 deletions tests/unit/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Headsnet\Collections\Test;

use Headsnet\Collections\Exception\InvalidTypeException;
use Headsnet\Collections\Exception\ItemNotFoundException;
use Headsnet\Collections\Test\Fixtures\DummyCollection;
use Headsnet\Collections\Test\Fixtures\DummyCollectionItem;
use Headsnet\Collections\Test\Fixtures\OtherCollectionItem;
Expand Down Expand Up @@ -60,6 +61,26 @@ public function test_first_returns_null_if_collection_is_empty(): void
$this->assertNull($sut->first());
}

public function test_first_or_fail_returns_first_item(): void
{
$collectionItem1 = new DummyCollectionItem();
$collectionItem2 = new DummyCollectionItem();
$collectionItem3 = new DummyCollectionItem();

$sut = new DummyCollection([$collectionItem1, $collectionItem2, $collectionItem3]);

$this->assertSame($collectionItem1, $sut->firstOrFail());
}

public function test_first_or_fail_throws_exception_if_collection_is_empty(): void
{
$sut = new DummyCollection([]);

$this->expectException(ItemNotFoundException::class);

$sut->firstOrFail();
}

public function test_last_returns_last_item(): void
{
$collectionItem1 = new DummyCollectionItem();
Expand All @@ -78,6 +99,26 @@ public function test_last_returns_null_if_collection_is_empty(): void
$this->assertNull($sut->last());
}

public function test_last_or_fail_returns_first_item(): void
{
$collectionItem1 = new DummyCollectionItem();
$collectionItem2 = new DummyCollectionItem();
$collectionItem3 = new DummyCollectionItem();

$sut = new DummyCollection([$collectionItem1, $collectionItem2, $collectionItem3]);

$this->assertSame($collectionItem3, $sut->lastOrFail());
}

public function test_last_or_fail_throws_exception_if_collection_is_empty(): void
{
$sut = new DummyCollection([]);

$this->expectException(ItemNotFoundException::class);

$sut->lastOrFail();
}

public function test_equality_check_succeeds_correctly(): void
{
$collectionItem1 = new DummyCollectionItem();
Expand Down