Skip to content

Commit

Permalink
Merge pull request #6 from tienvx/test-service
Browse files Browse the repository at this point in the history
test: Test EnvelopeCollector
  • Loading branch information
tienvx authored Mar 28, 2024
2 parents 1ff6a32 + ec04237 commit 10c2141
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/Unit/Service/EnvelopeCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Tienvx\Bundle\PactProviderBundle\Tests\Unit\Service;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Tienvx\Bundle\PactMessengerBundle\Exception\LogicException;
use Tienvx\Bundle\PactMessengerBundle\Service\EnvelopeCollector;
use Tienvx\Bundle\PactMessengerBundle\Service\EnvelopeCollectorInterface;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserCreated;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserDeleted;
use Tienvx\Bundle\PactMessengerBundle\Tests\TestApplication\Message\UserUpdated;

class EnvelopeCollectorTest extends TestCase
{
private EnvelopeCollectorInterface $collector;

protected function setUp(): void
{
$this->collector = new EnvelopeCollector();
}

public function testGetAll(): void
{
$this->collector->collect($create = new Envelope(new UserCreated(123)));
$this->collector->collect($update = new Envelope(new UserUpdated(123)));
$this->collector->collect($delete = new Envelope(new UserDeleted(123)));
$this->assertSame([$create, $update, $delete], $this->collector->getAll());
}

public function testSingleWithoutTransport(): void
{
$this->collector->collect($create = new Envelope(new UserCreated(123)));
$this->collector->collect($update = new Envelope(new UserUpdated(123)));
$this->collector->collect($delete = new Envelope(new UserDeleted(123)));
$this->assertSame($create, $this->collector->getSingle(UserCreated::class));
}

public function testSingleWithTransportThrowException(): void
{
$this->collector->collect($create = new Envelope(new UserCreated(123)));
$this->collector->collect($update = new Envelope(new UserUpdated(123)));
$this->collector->collect($delete = new Envelope(new UserDeleted(123)));
$this->expectException(LogicException::class);
$this->expectExceptionMessage('You need to upgrade to Symfony >=6.2 to be able to filter envelope by transport.');
$this->collector->getSingle(UserCreated::class, 'async');
}

public function testSingleWithTransportFound(): void
{
$this->collector->collect($create = new Envelope(new UserCreated(123)), ['async']);
$this->collector->collect($update = new Envelope(new UserUpdated(123)), ['async']);
$this->collector->collect($delete = new Envelope(new UserDeleted(123)), ['async', 'audit']);
$this->assertSame($delete, $this->collector->getSingle(UserDeleted::class, 'audit'));
}

public function testSingleWithTransportNotFound(): void
{
$this->collector->collect($create = new Envelope(new UserCreated(123)), ['async']);
$this->collector->collect($update = new Envelope(new UserUpdated(123)), ['async']);
$this->collector->collect($delete = new Envelope(new UserDeleted(123)), ['async', 'audit']);
$this->assertNull($this->collector->getSingle(UserUpdated::class, 'audit'));
}
}

0 comments on commit 10c2141

Please sign in to comment.