Skip to content

Commit

Permalink
Merge pull request #2 from rayrutjes/add-metadata-support
Browse files Browse the repository at this point in the history
Fixed event metadata push and retrieval.
  • Loading branch information
rayrutjes committed Mar 3, 2016
2 parents 5a49894 + 79b5a3c commit d650eb7
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/Client/Http/AbstractResponseInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ protected function decodeResponseBody(ResponseInterface $response): array
*/
protected function decodeData(string $data): array
{
return json_decode($data, true);
$decoded = json_decode($data, true);

return !is_array($decoded) ? [] : $decoded;
}
}
3 changes: 3 additions & 0 deletions src/Client/Http/AppendMultipleToStreamRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use RayRutjes\GetEventStore\EventData;
use RayRutjes\GetEventStore\EventDataCollection;
use RayRutjes\GetEventStore\ExpectedVersion;
use RayRutjes\GetEventStore\StreamId;
Expand Down Expand Up @@ -63,10 +64,12 @@ private function buildBody(): string
{
$data = [];
foreach ($this->events as $event) {
/* @var $event EventData */
$data[] = [
'eventId' => $event->getEventId()->toString(),
'eventType' => $event->getType(),
'data' => $event->getData(),
'metadata' => $event->getMetadata(),
];
}

Expand Down
7 changes: 1 addition & 6 deletions src/Client/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,10 @@ public function appendToStream(string $streamId, int $expectedVersion, array $ev
if ($streamId->isSystem()) {
throw new \InvalidArgumentException(sprintf('Can not append to system stream %s', $streamId));
}
// todo: what about metadata stream?

$expectedVersion = new ExpectedVersion($expectedVersion);

if (1 === $events->count()) {
$request = new AppendSingleToStreamRequestFactory($streamId, $expectedVersion, $events->current());
} else {
$request = new AppendMultipleToStreamRequestFactory($streamId, $expectedVersion, $events);
}
$request = new AppendMultipleToStreamRequestFactory($streamId, $expectedVersion, $events);

$this->send($request->buildRequest(), new AppendToStreamResponseInspector());
}
Expand Down
3 changes: 1 addition & 2 deletions src/Client/Http/ReadEventStreamFeedResponseInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function inspect(ResponseInterface $response)
$number = $entry['eventNumber'];
$type = $entry['eventType'];
$eventData = isset($entry['data']) ? $this->decodeData($entry['data']) : [];
$eventMetadata = isset($entry['metadata']) ? $this->decodeData($entry['metadata']) : [];
// todo: figure out why metadata is always empty.
$eventMetadata = isset($entry['metaData']) ? $this->decodeData($entry['metaData']) : [];

$events[] = new EventRecord($streamId, $number, $type, $eventData, $eventMetadata);
}
Expand Down
10 changes: 5 additions & 5 deletions src/EventRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,39 @@ public function __construct(string $streamId, int $number, string $type, array $
/**
* @return string
*/
public function getStreamId()
public function getStreamId(): string
{
return $this->streamId;
}

/**
* @return int
*/
public function getNumber()
public function getNumber(): int
{
return $this->number;
}

/**
* @return string
*/
public function getType()
public function getType(): string
{
return $this->type;
}

/**
* @return array
*/
public function getData()
public function getData(): array
{
return $this->data;
}

/**
* @return array
*/
public function getMetadata()
public function getMetadata(): array
{
return $this->metadata;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RayRutjes\GetEventStore\Test\Integration;

use RayRutjes\GetEventStore\Client\Http\HttpClient;
use RayRutjes\GetEventStore\ClientInterface;
use RayRutjes\GetEventStore\EventData;
use RayRutjes\GetEventStore\EventRecordCollection;
use RayRutjes\GetEventStore\Test\TestCase;
Expand All @@ -22,7 +23,7 @@ public function setUp()
* @param float $connectTimeout
* @param array $options
*
* @return Client
* @return ClientInterface
*/
protected function buildClient(UserCredentials $credentials = null, float $connectTimeout = 0, array $options = [])
{
Expand Down Expand Up @@ -85,9 +86,9 @@ protected function assertEventDataMatchesEventRecords(array $expectedEvents, Eve
$this->assertEquals($data->getType(), $record->getType());
$this->assertEquals($key + $offset, $record->getNumber());
$this->assertEquals($streamId, $record->getStreamId());
$this->assertEquals($data->getMetadata(), $record->getMetadata());

// todo: Add the metadata and event id tests.
// $this->assertEquals($data->getMetadata(), $record->getMetadata());
// todo: Add event id tests.
// $this->assertEquals($data->getEventId(), $record->getEventId());
}

Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Http/AppendMultipleToStreamRequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ public function testCanBuildARequest()
$uuid2 = $this->newUuid();
$type2 = 'RayRutjes\GetEventStore\FakeEvent2';
$data2 = ['a' => 'test2'];
$metadata2 = [];
$metadata2 = ['test' => 'value'];
$event2 = new EventData($uuid2, $type2, $data2, $metadata2);

$expectedBody = <<<'EOD'
[
{
"eventId": "%s",
"eventType": "RayRutjes\\GetEventStore\\FakeEvent1",
"data": {"a":"test1"}
"data": {"a":"test1"},
"metadata":[]
},
{
"eventId": "%s",
"eventType": "RayRutjes\\GetEventStore\\FakeEvent2",
"data": {"a":"test2"}
"data": {"a":"test2"},
"metadata": {"test":"value"}
}
]
EOD;
Expand Down

0 comments on commit d650eb7

Please sign in to comment.