Skip to content

Commit

Permalink
Merge pull request #6 from codeliner/patch-5
Browse files Browse the repository at this point in the history
Patch-5: Add message type Query
  • Loading branch information
codeliner committed May 22, 2015
2 parents 27cc974 + 52a123a commit 025246c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Messaging/DomainMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract class DomainMessage implements HasMessageName
protected $metadata = array();

/**
* Should be either MessageHeader::TYPE_COMMAND or MessageHeader::TYPE_EVENT
* Should be either MessageHeader::TYPE_COMMAND or MessageHeader::TYPE_EVENT or MessageHeader::TYPE_QUERY
*
* @return string
*/
Expand Down Expand Up @@ -105,7 +105,7 @@ public static function fromRemoteMessage(RemoteMessage $message)
}

/**
* We force implementors to provide a meaningful factory method or use the fromArray or fromRemoteMessage methods
* We force implementers to provide a meaningful factory method or use the fromArray or fromRemoteMessage methods
*
* @param string $messageName
* @param null $payload
Expand Down
3 changes: 2 additions & 1 deletion src/Messaging/MessageHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class MessageHeader
{
const TYPE_COMMAND = 'command';
const TYPE_EVENT = 'event';
const TYPE_QUERY = 'query';

/**
* @var Uuid
Expand Down Expand Up @@ -94,7 +95,7 @@ public function __construct(Uuid $uuid, \DateTimeImmutable $createdAt, $version,
Assertion::notEmpty($version, 'MessageHeader.version must not be empty');
Assertion::integer($version, 'MessageHeader.version must be an integer');

Assertion::inArray($type, [self::TYPE_COMMAND, self::TYPE_EVENT], 'MessageHeader.type must be command or event');
Assertion::inArray($type, [self::TYPE_COMMAND, self::TYPE_EVENT, self::TYPE_QUERY], 'MessageHeader.type must be command, query or event');

$this->uuid = $uuid;
$this->createdAt = $createdAt;
Expand Down
32 changes: 32 additions & 0 deletions src/Messaging/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the prooph/common.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 5/22/15 - 10:26 PM
*/
namespace Prooph\Common\Messaging;

/**
* Class Query
*
* This is the base class for queries used to fetch data from read model.
*
* @package Prooph\Common\Messaging
* @author Alexander Miertsch <[email protected]>
*/
class Query extends DomainMessage
{
/**
* Should be either MessageHeader::TYPE_COMMAND or MessageHeader::TYPE_EVENT or MessageHeader::TYPE_QUERY
*
* @return string
*/
protected function messageType()
{
return MessageHeader::TYPE_QUERY;
}
}
38 changes: 38 additions & 0 deletions tests/Messaging/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the prooph/common.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 5/22/15 - 10:28 PM
*/
namespace ProophTest\Common\Messaging;


use Prooph\Common\Messaging\MessageHeader;
use Prooph\Common\Messaging\Query;
use Rhumsaa\Uuid\Uuid;

final class QueryTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
function it_has_the_message_type_query()
{
$query = Query::fromArray([
'name' => 'TestQuery',
'uuid' => Uuid::uuid4()->toString(),
'version' => 1,
'created_at' => (new \DateTimeImmutable())->format(\DateTime::ISO8601),
'payload' => ['query' => 'payload'],
'metadata' => ['query' => 'metadata']
]);

$remoteMessage = $query->toRemoteMessage();

$this->assertEquals(MessageHeader::TYPE_QUERY, $remoteMessage->header()->type());
}
}

0 comments on commit 025246c

Please sign in to comment.