Skip to content

Commit

Permalink
Merge pull request #77 from chimeraphp/implement-input-extractors
Browse files Browse the repository at this point in the history
Provide interfaces for input extraction
  • Loading branch information
lcobucci authored Oct 26, 2020
2 parents 0151c3d + 3f6f171 commit df54320
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/MessageCreator/InputExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Chimera\MessageCreator;

use Chimera\Input;

interface InputExtractor
{
/** @return array<string, mixed> */
public function extractData(Input $input): array;
}
35 changes: 35 additions & 0 deletions src/MessageCreator/InputExtractor/AppendGeneratedIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace Chimera\MessageCreator\InputExtractor;

use Chimera\IdentifierGenerator;
use Chimera\Input;
use Chimera\MessageCreator\InputExtractor;

final class AppendGeneratedIdentifier implements InputExtractor
{
private const DEFAULT_NAME = 'id';

private InputExtractor $decorated;
private string $attributeName;

public function __construct(InputExtractor $decorated, string $attributeName = self::DEFAULT_NAME)
{
$this->decorated = $decorated;
$this->attributeName = $attributeName;
}

/** @inheritdoc */
public function extractData(Input $input): array
{
$id = $input->getAttribute(IdentifierGenerator::class);
$data = $this->decorated->extractData($input);

if ($id === null) {
return $data;
}

return [$this->attributeName => $id] + $data;
}
}
16 changes: 16 additions & 0 deletions src/MessageCreator/InputExtractor/UseInputData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Chimera\MessageCreator\InputExtractor;

use Chimera\Input;
use Chimera\MessageCreator\InputExtractor;

final class UseInputData implements InputExtractor
{
/** @inheritdoc */
public function extractData(Input $input): array
{
return $input->getData();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);

namespace Chimera\MessageCreator\Tests\Unit\InputExtractor;

use Chimera\Input;
use Chimera\MessageCreator\InputExtractor\AppendGeneratedIdentifier;
use Chimera\MessageCreator\InputExtractor\UseInputData;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;

/**
* @coversDefaultClass \Chimera\MessageCreator\InputExtractor\AppendGeneratedIdentifier
*
* @uses \Chimera\MessageCreator\InputExtractor\UseInputData
*/
final class AppendGeneratedIdentifierTest extends TestCase
{
/**
* @test
*
* @covers ::__construct
* @covers ::extractData
*/
public function extractDataShouldAddTheIdentifierAttributeWhenItExists(): void
{
$id = Uuid::uuid4();

$input = $this->createMock(Input::class);
$input->method('getData')->willReturn(['testing' => 1]);
$input->method('getAttribute')->willReturn($id);

$extractor = new AppendGeneratedIdentifier(new UseInputData());

self::assertSame(['id' => $id, 'testing' => 1], $extractor->extractData($input));
}

/**
* @test
*
* @covers ::__construct
* @covers ::extractData
*/
public function extractDataShouldOverrideInputData(): void
{
$id = Uuid::uuid4();

$input = $this->createMock(Input::class);
$input->method('getData')->willReturn(['testing' => 1, 'id' => '123']);
$input->method('getAttribute')->willReturn($id);

$extractor = new AppendGeneratedIdentifier(new UseInputData());

self::assertSame(['id' => $id, 'testing' => 1], $extractor->extractData($input));
}

/**
* @test
*
* @covers ::__construct
* @covers ::extractData
*/
public function extractDataShouldReturnTheInputDataWhenIdentifierAttributeDoesNotExist(): void
{
$input = $this->createMock(Input::class);
$input->method('getData')->willReturn(['testing' => 1, 'id' => '123']);
$input->method('getAttribute')->willReturn(null);

$extractor = new AppendGeneratedIdentifier(new UseInputData());

self::assertSame(['testing' => 1, 'id' => '123'], $extractor->extractData($input));
}
}
27 changes: 27 additions & 0 deletions tests/MessageCreator/InputExtractor/UseInputDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace Chimera\MessageCreator\Tests\Unit\InputExtractor;

use Chimera\Input;
use Chimera\MessageCreator\InputExtractor\UseInputData;
use PHPUnit\Framework\TestCase;

/** @coversDefaultClass \Chimera\MessageCreator\InputExtractor\UseInputData */
final class UseInputDataTest extends TestCase
{
/**
* @test
*
* @covers ::extractData
*/
public function extractDataShouldReturnTheInputData(): void
{
$input = $this->createMock(Input::class);
$input->method('getData')->willReturn(['testing' => 1]);

$extractor = new UseInputData();

self::assertSame(['testing' => 1], $extractor->extractData($input));
}
}

0 comments on commit df54320

Please sign in to comment.