diff --git a/src/Exception/CollectionIsEmpty.php b/src/Exception/CollectionIsEmpty.php new file mode 100644 index 0000000..1b442b6 --- /dev/null +++ b/src/Exception/CollectionIsEmpty.php @@ -0,0 +1,20 @@ +versions); } - public function first(): ?Version + public function first(): Version { - return $this->versions[0] ?? null; + if (empty($this->versions)) { + throw CollectionIsEmpty::cannotGetFirst(); + } + + return reset($this->versions); } - public function last(): ?Version + public function last(): Version { - return $this->versions[count($this->versions) - 1] ?? null; + if (empty($this->versions)) { + throw CollectionIsEmpty::cannotGetLast(); + } + + $version = end($this->versions); + reset($this->versions); + + return $version; } public function getIterator(): Traversable diff --git a/tests/VersionCollectionTest.php b/tests/VersionCollectionTest.php index d4f035b..d901e06 100644 --- a/tests/VersionCollectionTest.php +++ b/tests/VersionCollectionTest.php @@ -5,6 +5,7 @@ namespace Version\Tests; use PHPUnit\Framework\TestCase; +use Version\Exception\CollectionIsEmpty; use Version\Tests\TestAsset\VersionIsIdentical; use Version\Tests\TestAsset\VersionCollectionIsIdentical; use Version\VersionCollection; @@ -72,6 +73,22 @@ public function it_gets_first_version(): void $this->assertThat($version, new VersionIsIdentical(1, 0, 0)); } + /** + * @test + */ + public function it_raises_exception_when_getting_first_item_of_empty_collection(): void + { + $versions = new VersionCollection(); + + try { + $versions->first(); + + $this->fail('Exception should have been raised'); + } catch (CollectionIsEmpty $ex) { + $this->assertSame('Cannot get the first Version from an empty collection', $ex->getMessage()); + } + } + /** * @test */ @@ -92,12 +109,17 @@ public function it_gets_last_version(): void /** * @test */ - public function it_doesnt_return_first_last_versions_if_empty(): void + public function it_raises_exception_when_getting_last_item_of_empty_collection(): void { $versions = new VersionCollection(); - $this->assertNull($versions->first()); - $this->assertNull($versions->last()); + try { + $versions->last(); + + $this->fail('Exception should have been raised'); + } catch (CollectionIsEmpty $ex) { + $this->assertSame('Cannot get the last Version from an empty collection', $ex->getMessage()); + } } /**