|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace HelloFresh\Tests\Engine\EventStore\Snapshot; |
| 4 | + |
| 5 | +use HelloFresh\Engine\Domain\AggregateId; |
| 6 | +use HelloFresh\Engine\Domain\AggregateIdInterface; |
| 7 | +use HelloFresh\Engine\Domain\AggregateRootInterface; |
| 8 | +use HelloFresh\Engine\EventStore\Snapshot\Snapshot; |
| 9 | +use HelloFresh\Tests\Engine\Mock\AggregateRoot; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class SnapshotTest extends TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @test |
| 16 | + * @dataProvider messageProvider |
| 17 | + * @param AggregateIdInterface $aggregateId |
| 18 | + * @param AggregateRootInterface $aggregate |
| 19 | + * @param $version |
| 20 | + * @param \DateTimeImmutable $date |
| 21 | + * @internal param $payload |
| 22 | + */ |
| 23 | + public function itShouldCreateASnapshotFromConstructor( |
| 24 | + AggregateIdInterface $aggregateId, |
| 25 | + AggregateRootInterface $aggregate, |
| 26 | + $version, |
| 27 | + \DateTimeImmutable $date |
| 28 | + ) { |
| 29 | + $message = new Snapshot($aggregateId, $aggregate, $version, $date); |
| 30 | + |
| 31 | + $this->assertInstanceOf(Snapshot::class, $message); |
| 32 | + $this->assertSame($aggregateId, $message->getAggregateId()); |
| 33 | + $this->assertSame($aggregate, $message->getAggregate()); |
| 34 | + $this->assertSame($version, $message->getVersion()); |
| 35 | + $this->assertSame(get_class($aggregate), $message->getType()); |
| 36 | + $this->assertEquals($date, $message->getCreatedAt()); |
| 37 | + $this->assertEquals(new \DateTimeZone('UTC'), $message->getCreatedAt()->getTimezone()); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @test |
| 42 | + * @dataProvider messageProvider |
| 43 | + * @param AggregateIdInterface $aggregateId |
| 44 | + * @param AggregateRootInterface $aggregate |
| 45 | + * @param string $version |
| 46 | + */ |
| 47 | + public function itShouldCreateASnapshotFromNamedConstructor( |
| 48 | + AggregateIdInterface $aggregateId, |
| 49 | + AggregateRootInterface $aggregate, |
| 50 | + $version |
| 51 | + ) { |
| 52 | + $snapshot = Snapshot::take($aggregateId, $aggregate, $version); |
| 53 | + |
| 54 | + $this->assertInstanceOf(Snapshot::class, $snapshot); |
| 55 | + $this->assertNotEmpty((int)$snapshot->getCreatedAt()->format('u'), 'Expected microseconds to be set'); |
| 56 | + $this->assertEquals(new \DateTimeZone('UTC'), $snapshot->getCreatedAt()->getTimezone()); |
| 57 | + } |
| 58 | + |
| 59 | + public function messageProvider() |
| 60 | + { |
| 61 | + return [ |
| 62 | + [ |
| 63 | + AggregateId::generate(), |
| 64 | + AggregateRoot::create(AggregateId::generate(), 'v1000'), |
| 65 | + '1000', |
| 66 | + new \DateTimeImmutable() |
| 67 | + ], |
| 68 | + [ |
| 69 | + AggregateId::generate(), |
| 70 | + AggregateRoot::create(AggregateId::generate(), 'v1'), |
| 71 | + '1', |
| 72 | + \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true))) |
| 73 | + ] |
| 74 | + ]; |
| 75 | + } |
| 76 | +} |
0 commit comments