|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Spec\Kreta\SharedKernel\Serialization; |
|
14
|
|
|
|
|
15
|
|
|
use Kreta\SharedKernel\Domain\Model\Exception; |
|
16
|
|
|
use Kreta\SharedKernel\Event\AsyncEvent; |
|
17
|
|
|
use Kreta\SharedKernel\Serialization\AsyncEventSerializer; |
|
18
|
|
|
use Kreta\SharedKernel\Serialization\InvalidSerializationObjectException; |
|
19
|
|
|
use Kreta\SharedKernel\Serialization\Resolver; |
|
20
|
|
|
use Kreta\SharedKernel\Serialization\Serializer; |
|
21
|
|
|
use Kreta\SharedKernel\Tests\Double\Domain\Model\DomainEventStub; |
|
22
|
|
|
use PhpSpec\ObjectBehavior; |
|
23
|
|
|
|
|
24
|
|
|
class AsyncEventSerializerSpec extends ObjectBehavior |
|
25
|
|
|
{ |
|
26
|
|
|
function let(Resolver $resolver) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->beConstructedWith($resolver); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
function it_is_initializable() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->shouldHaveType(AsyncEventSerializer::class); |
|
34
|
|
|
$this->shouldImplement(Serializer::class); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function it_serializes(Resolver $resolver) |
|
38
|
|
|
{ |
|
39
|
|
|
$event = new DomainEventStub('foo', 'bar', new \DateTimeImmutable('2016-11-06 23:02:21')); |
|
40
|
|
|
$resolver->resolve(DomainEventStub::class)->shouldBeCalled()->willReturn('event-name'); |
|
41
|
|
|
$this->serialize($event)->shouldReturn( |
|
42
|
|
|
'{"name":"event-name","occurredOn":"2016-11-06 23:02:21","values":{"bar":"bar","foo":"foo","occurredOn":"2016-11-06 23:02:21"}}' |
|
|
|
|
|
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
function it_does_not_serialize_when_event_is_not_instance_of_domain_event($notDomainEvent) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->shouldThrow(InvalidSerializationObjectException::class)->duringSerialize($notDomainEvent); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
function it_deserializes() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->deserialize('{"name":"event-name","occurredOn":"2016-11-06 23:02:21","values":{"bar":"bar","foo":"foo","occurredOn":"2016-11-06 23:02:21"}}') |
|
|
|
|
|
|
54
|
|
|
->shouldReturnAnInstanceOf(AsyncEvent::class); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
function it_does_not_deserialize_when_name_or_occurred_on_or_values_do_not_exist() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->shouldThrow(Exception::class) |
|
60
|
|
|
->duringDeserialize('{"name":"event-name","values":{"bar":"bar","foo":"foo","occurredOn":"2016-11-06 23:02:21"}}'); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.