Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds extra attributes optional at Domain Event class #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ language: php

matrix:
include:
- php: 5.5
env:
- RABBITMQ_HOST=127.0.0.1
- RABBITMQ_PORT=5672
- RABBITMQ_USER=guest
- RABBITMQ_PASSWORD=guest
- RABBITMQ_VHOST=/
- php: 5.6
env:
- RABBITMQ_HOST=127.0.0.1
Expand Down
82 changes: 56 additions & 26 deletions src/Cmp/Queues/Domain/Event/DomainEvent.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Cmp\Queues\Domain\Event;

use Cmp\Queues\Domain\Event\Exception\DomainEventException;
Expand Down Expand Up @@ -26,6 +27,11 @@ class DomainEvent implements Message
*/
protected $occurredOn;

/**
* @var array|null
*/
protected $extraAttributes;

/**
* @var array
*/
Expand All @@ -47,14 +53,15 @@ class DomainEvent implements Message
protected $correlationId;

/**
* @param string $origin
* @param string $name
* @param string $version
* @param int $occurredOn
* @param array $body
* @param string $id
* @param bool $isDeprecated
* @param string $origin
* @param string $name
* @param string $version
* @param int $occurredOn
* @param array $body
* @param string $id
* @param bool $isDeprecated
* @param string|null $correlationId
* @param array|null $extraAttributes
* @throws DomainEventException
*/
public function __construct(
Expand All @@ -65,16 +72,18 @@ public function __construct(
array $body = [],
$id = null,
$isDeprecated = false,
$correlationId = null
$correlationId = null,
$extraAttributes = []
) {
$this->setOrigin($origin)
->setName($name)
->setVersion($version)
->setOccurredOn($occurredOn);
->setOccurredOn($occurredOn)
->setExtraAttributes($extraAttributes);

$this->body = $body;
$this->id = $id;
$this->isDeprecated = $isDeprecated;
$this->body = $body;
$this->id = $id;
$this->isDeprecated = $isDeprecated;
$this->correlationId = $correlationId;
}

Expand Down Expand Up @@ -112,6 +121,14 @@ public function getOccurredOn()
return $this->occurredOn;
}

/**
* @return array|null
*/
public function getExtraAttributes()
{
return $this->extraAttributes;
}

/**
* @return array
*/
Expand Down Expand Up @@ -154,7 +171,7 @@ public function getCorrelationID()

/**
* @param string $key
* @param mixed $default
* @param mixed $default
*
* @return mixed
*/
Expand Down Expand Up @@ -190,10 +207,11 @@ public function getBodyValueOrFail($key)
*/
protected function setOrigin($origin)
{
if(empty($origin)) {
if (empty($origin)) {
throw new DomainEventException('DomainEvent origin cannot be empty');
}
$this->origin = $origin;

return $this;
}

Expand All @@ -204,10 +222,11 @@ protected function setOrigin($origin)
*/
protected function setName($name)
{
if(empty($name)) {
if (empty($name)) {
throw new DomainEventException('DomainEvent name cannot be empty');
}
$this->name = $name;

return $this;
}

Expand All @@ -218,10 +237,11 @@ protected function setName($name)
*/
protected function setVersion($version)
{
if(empty($version)) {
if (empty($version)) {
throw new DomainEventException('DomainEvent version cannot be empty');
}
$this->version = $version;

return $this;
}

Expand All @@ -232,7 +252,7 @@ protected function setVersion($version)
*/
protected function setOccurredOn($occurredOn)
{
if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
if (!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds
throw new DomainEventException("$occurredOn is not a valid unix timestamp.");
}

Expand All @@ -241,23 +261,33 @@ protected function setOccurredOn($occurredOn)
}

$this->occurredOn = $occurredOn;

return $this;
}

/**
* @param array|null $extraAttributes
*/
protected function setExtraAttributes($extraAttributes)
{
$this->extraAttributes = $extraAttributes;
}

/**
* @return array
*/
public function jsonSerialize()
{
return [
'origin' => $this->origin,
'name' => $this->name,
'version' => $this->version,
'occurredOn' => $this->occurredOn,
'body' => $this->body,
'id' => $this->id,
'isDeprecated' => $this->isDeprecated,
'correlationId' => $this->correlationId,
'origin' => $this->origin,
'name' => $this->name,
'version' => $this->version,
'occurredOn' => $this->occurredOn,
'body' => $this->body,
'id' => $this->id,
'isDeprecated' => $this->isDeprecated,
'correlationId' => $this->correlationId,
'extraAttributes' => $this->extraAttributes,
];
}
}
}
1 change: 1 addition & 0 deletions src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function create($json)
isset($domainEventArray['id']) ? $domainEventArray['id'] : null,
isset($domainEventArray['isDeprecated']) ? $domainEventArray['isDeprecated'] : false,
isset($domainEventArray['correlationId']) ? $domainEventArray['correlationId'] : null
// isset($domainEventArray['extraAttributes']) ? $domainEventArray['extraAttributes'] : null
);
} catch (DomainEventException $e) {
throw new InvalidJSONDomainEventException("Failed creating DomainEvent instance", 0, $e);
Expand Down
25 changes: 23 additions & 2 deletions tests/behat/features/bootstrap/Context/DomainContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ public function iUseProvider($provider)
public function iSendARandomDomainEvent()
{
$this->startDomainEventConsumer();
$this->domainEvent = new DomainEvent('behat', 'behat.test', self::VERSION, time(), array(1,2,3,4,5));
$this->domainEvent = new DomainEvent(
'behat',
'behat.test',
self::VERSION,
time(),
array(1,2,3,4,5),
null,
false,
null
// ['attribute1', 'attribute2']
);
$publisher = $this->getDomainEventPublisher();
$publisher->add($this->domainEvent);
$publisher->publish();
Expand All @@ -128,6 +138,7 @@ public function iShouldConsumeTheRandomDomainEvent()
assert($this->domainEvent->getBody() === $incomingDomainEvent->getBody(), 'Body doesnt match');
assert($this->domainEvent->getVersion() === $incomingDomainEvent->getVersion(), 'Version doesnt match');
assert($this->domainEvent->getOccurredOn() === $incomingDomainEvent->getOccurredOn(), 'OccurredOn doesnt match');
// assert($this->domainEvent->getExtraAttributes() === $incomingDomainEvent->getExtraAttributes(), 'ExtraAttributes doesnt match');
}

/**
Expand All @@ -136,7 +147,17 @@ public function iShouldConsumeTheRandomDomainEvent()
public function iSendARandomDomainEventWithAnUnwantedTopic()
{
$this->startDomainEventConsumer();
$this->domainEvent = new DomainEvent('behat', 'unwanted.topic', self::VERSION, time(), array(1,2,3,4,5));
$this->domainEvent = new DomainEvent(
'behat',
'unwanted.topic',
self::VERSION,
time(),
array(1,2,3,4,5),
null,
false,
null
// ['attribute1', 'attribute2']
);
$publisher = $this->getDomainEventPublisher();
$publisher->add($this->domainEvent);
$publisher->publish();
Expand Down
17 changes: 16 additions & 1 deletion tests/spec/Cmp/Queues/Domain/Event/DomainEventSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ class DomainEventSpec extends ObjectBehavior
function let()
{
$this->time = microtime(true)-10;
$this->beConstructedWith('origin', 'name', '1.0.0', $this->time, array("foo" => "bar", "empty" => null), 'uuid', true, 'correlation');
$this->beConstructedWith(
'origin',
'name',
'1.0.0',
$this->time,
array("foo" => "bar", "empty" => null),
'uuid',
true,
'correlation'
// ['attribute1', 'attribute2']
);
}

function it_is_initializable()
Expand Down Expand Up @@ -102,6 +112,11 @@ function it_should_have_the_correlation_id()
$this->getCorrelationId()->shouldBe("correlation");
}

/* function it_should_have_the_extra_attributes()
{
$this->getExtraAttributes()->shouldBe(['attribute1', 'attribute2']);
}*/

function it_can_get_body_values()
{
$this->getBodyValue("foo")->shouldBe("bar");
Expand Down
15 changes: 13 additions & 2 deletions tests/spec/Cmp/Queues/Domain/Event/JSONDomainEventFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,19 @@ function it_can_detect_a_optional_fields()
$id = "abc";
$isDeprecated = true;
$correlationId = "def";

$taskPreFactory = new DomainEvent('origin', 'name', '1.0.0', time(), array(1,2,3,4,5), $id, $isDeprecated, $correlationId);
// $extraAttributes = ['attribute1', 'attribute2'];

$taskPreFactory = new DomainEvent(
'origin',
'name',
'1.0.0',
time(),
array(1,2,3,4,5),
$id,
$isDeprecated,
$correlationId
// $extraAttributes
);
$this->create(json_encode($taskPreFactory))->shouldBeLike($taskPreFactory);
}

Expand Down