Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#32] create and test ChainFeatureRepository
- Loading branch information
Showing
5 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pheature\Core\Toggle\Write; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class ChainFeatureRepository implements FeatureRepository | ||
{ | ||
/** @var FeatureRepository[] */ | ||
private array $featureRepositories; | ||
|
||
public function __construct(FeatureRepository ...$featureRepositories) | ||
{ | ||
$this->featureRepositories = $featureRepositories; | ||
} | ||
|
||
public function save(Feature $feature): void | ||
{ | ||
foreach ($this->featureRepositories as $featureRepository) { | ||
$featureRepository->save($feature); | ||
} | ||
} | ||
|
||
public function remove(Feature $feature): void | ||
{ | ||
foreach ($this->featureRepositories as $featureRepository) { | ||
$featureRepository->remove($feature); | ||
} | ||
} | ||
|
||
public function get(FeatureId $featureId): Feature | ||
{ | ||
foreach ($this->featureRepositories as $featureRepository) { | ||
try { | ||
$feature = $featureRepository->get($featureId); | ||
} catch (InvalidArgumentException $exception) { | ||
continue; | ||
} | ||
|
||
return $feature; | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf('Feature with id %s not found.', $featureId->value())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pheature\Test\Core\Toggle\Write; | ||
|
||
use InvalidArgumentException; | ||
use Pheature\Core\Toggle\Write\ChainFeatureRepository; | ||
use Pheature\Core\Toggle\Write\Feature; | ||
use Pheature\Core\Toggle\Write\FeatureId; | ||
use Pheature\Core\Toggle\Write\FeatureRepository; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ChainFeatureRepositoryTest extends TestCase | ||
{ | ||
const FEATURE_ID = 'some_id'; | ||
|
||
public function testItShouldThrowExceptionGWhenFeatureDoesNotExistInAnyOfGivenFeatureRepositories(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Feature with id some_id not found.'); | ||
$firstRepository = $this->createMock(FeatureRepository::class); | ||
$firstRepository->expects(static::once()) | ||
->method('get') | ||
->with(static::isInstanceOf(FeatureId::class)) | ||
->willThrowException($this->createMock(InvalidArgumentException::class)); | ||
$secondRepository = $this->createMock(FeatureRepository::class); | ||
$secondRepository->expects(static::once()) | ||
->method('get') | ||
->with(static::isInstanceOf(FeatureId::class)) | ||
->willThrowException($this->createMock(InvalidArgumentException::class)); | ||
|
||
$chainFeatureRepository = new ChainFeatureRepository( | ||
$firstRepository, | ||
$secondRepository | ||
); | ||
|
||
$chainFeatureRepository->get(FeatureId::fromString(self::FEATURE_ID)); | ||
} | ||
|
||
public function testItShouldCreateInstanceOfChainFeatureRepositoryAndGetSAFeatureFromGivenFeatureRepositories(): void | ||
{ | ||
$feature = new Feature( | ||
FeatureId::fromString(self::FEATURE_ID), | ||
true | ||
); | ||
$firstRepository = $this->createMock(FeatureRepository::class); | ||
$firstRepository->expects(static::once()) | ||
->method('get') | ||
->with(static::isInstanceOf(FeatureId::class)) | ||
->willThrowException($this->createMock(InvalidArgumentException::class)); | ||
$secondRepository = $this->createMock(FeatureRepository::class); | ||
$secondRepository->expects(static::once()) | ||
->method('get') | ||
->with(static::isInstanceOf(FeatureId::class)) | ||
->willReturn($feature); | ||
|
||
$chainFeatureRepository = new ChainFeatureRepository( | ||
$firstRepository, | ||
$secondRepository | ||
); | ||
|
||
$chainFeatureRepository->get(FeatureId::fromString(self::FEATURE_ID)); | ||
} | ||
|
||
public function testItShouldCreateInstanceOfChainFeatureRepositoryAndSaveAFeatureInEveryGivenFeatureRepositories(): void | ||
{ | ||
$feature = new Feature( | ||
FeatureId::fromString(self::FEATURE_ID), | ||
true | ||
); | ||
$firstRepository = $this->createMock(FeatureRepository::class); | ||
$firstRepository->expects(static::once()) | ||
->method('save') | ||
->with($feature); | ||
$secondRepository = $this->createMock(FeatureRepository::class); | ||
$secondRepository->expects(static::once()) | ||
->method('save') | ||
->with($feature); | ||
|
||
$chainFeatureRepository = new ChainFeatureRepository( | ||
$firstRepository, | ||
$secondRepository | ||
); | ||
|
||
$chainFeatureRepository->save($feature); | ||
} | ||
|
||
public function testItShouldCreateInstanceOfChainFeatureRepositoryAndRemoveAFeatureInEveryGivenFeatureRepositories(): void | ||
{ | ||
$feature = new Feature( | ||
FeatureId::fromString(self::FEATURE_ID), | ||
true | ||
); | ||
$firstRepository = $this->createMock(FeatureRepository::class); | ||
$firstRepository->expects(static::once()) | ||
->method('remove') | ||
->with($feature); | ||
$secondRepository = $this->createMock(FeatureRepository::class); | ||
$secondRepository->expects(static::once()) | ||
->method('remove') | ||
->with($feature); | ||
|
||
$chainFeatureRepository = new ChainFeatureRepository( | ||
$firstRepository, | ||
$secondRepository | ||
); | ||
|
||
$chainFeatureRepository->remove($feature); | ||
} | ||
} | ||
|