Skip to content

Commit

Permalink
Raise exception when doing first/last operations on an empty Version …
Browse files Browse the repository at this point in the history
…collection
  • Loading branch information
nikolaposa committed Dec 29, 2019
1 parent fcfaba8 commit 3dad7e5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/Exception/CollectionIsEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Version\Exception;

use OutOfBoundsException;

final class CollectionIsEmpty extends OutOfBoundsException implements VersionException
{
public static function cannotGetFirst(): self
{
return new self('Cannot get the first Version from an empty collection');
}

public static function cannotGetLast(): self
{
return new self('Cannot get the last Version from an empty collection');
}
}
20 changes: 16 additions & 4 deletions src/VersionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ArrayIterator;
use Traversable;
use Version\Comparison\Constraint\Constraint;
use Version\Exception\CollectionIsEmpty;

class VersionCollection implements Countable, IteratorAggregate
{
Expand All @@ -29,14 +30,25 @@ public function isEmpty(): bool
return empty($this->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
Expand Down
28 changes: 25 additions & 3 deletions tests/VersionCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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());
}
}

/**
Expand Down

0 comments on commit 3dad7e5

Please sign in to comment.