1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Patchlevel \EventSourcingBundle \Tests \Unit \CommandBus ;
6+
7+ use Patchlevel \EventSourcing \Aggregate \CustomId ;
8+ use Patchlevel \EventSourcingBundle \CommandBus \SymfonyCommandBus ;
9+ use Patchlevel \EventSourcingBundle \Tests \Fixtures \CreateProfile ;
10+ use PHPUnit \Framework \TestCase ;
11+ use Prophecy \PhpUnit \ProphecyTrait ;
12+ use RuntimeException ;
13+ use Symfony \Component \Messenger \Envelope ;
14+ use Symfony \Component \Messenger \Exception \HandlerFailedException ;
15+ use Symfony \Component \Messenger \MessageBusInterface ;
16+
17+ /** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */
18+ final class SymfonyCommandtBusTest extends TestCase
19+ {
20+ use ProphecyTrait;
21+
22+ public function testDispatch (): void
23+ {
24+ $ command = new CreateProfile (
25+ CustomId::fromString ('1 ' ),
26+ );
27+
28+ $ symfony = $ this ->prophesize (MessageBusInterface::class);
29+
30+ $ envelope = new Envelope ($ command );
31+
32+ $ symfony ->dispatch ($ command )->willReturn ($ envelope )->shouldBeCalled ();
33+
34+ $ commandBus = new SymfonyCommandBus ($ symfony ->reveal ());
35+ $ commandBus ->dispatch ($ command );
36+ }
37+
38+ public function testException (): void
39+ {
40+ $ command = new CreateProfile (
41+ CustomId::fromString ('1 ' ),
42+ );
43+
44+ $ symfony = $ this ->prophesize (MessageBusInterface::class);
45+
46+ $ internalException = new class extends RuntimeException {
47+ };
48+
49+ $ envelope = new Envelope ($ command );
50+
51+ $ symfony
52+ ->dispatch ($ command )
53+ ->willThrow (new HandlerFailedException ($ envelope , [$ internalException ]))
54+ ->shouldBeCalled ();
55+
56+ $ commandBus = new SymfonyCommandBus ($ symfony ->reveal ());
57+
58+ $ this ->expectException ($ internalException ::class);
59+
60+ $ commandBus ->dispatch ($ command );
61+ }
62+
63+
64+ public function testRecursiveException (): void
65+ {
66+ $ command = new CreateProfile (
67+ CustomId::fromString ('1 ' ),
68+ );
69+
70+ $ symfony = $ this ->prophesize (MessageBusInterface::class);
71+
72+ $ internalException = new class extends RuntimeException {
73+ };
74+
75+ $ envelope = new Envelope ($ command );
76+
77+ $ symfony
78+ ->dispatch ($ command )
79+ ->willThrow (new HandlerFailedException ($ envelope , [new HandlerFailedException ($ envelope , [$ internalException ])]))
80+ ->shouldBeCalled ();
81+
82+ $ commandBus = new SymfonyCommandBus ($ symfony ->reveal ());
83+
84+ $ this ->expectException ($ internalException ::class);
85+
86+ $ commandBus ->dispatch ($ command );
87+ }
88+ }
0 commit comments