|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Services\Application\Handlers\CheckInList\Public; |
| 4 | + |
| 5 | +use HiEvents\DomainObjects\AttendeeDomainObject; |
| 6 | +use HiEvents\DomainObjects\CheckInListDomainObject; |
| 7 | +use HiEvents\Exceptions\CannotCheckInException; |
| 8 | +use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface; |
| 9 | +use HiEvents\Repository\Interfaces\CheckInListRepositoryInterface; |
| 10 | +use HiEvents\Services\Application\Handlers\CheckInList\Public\GetCheckInListAttendeePublicHandler; |
| 11 | +use Mockery as m; |
| 12 | +use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class GetCheckInListAttendeePublicHandlerTest extends TestCase |
| 16 | +{ |
| 17 | + private CheckInListRepositoryInterface $checkInListRepository; |
| 18 | + private AttendeeRepositoryInterface $attendeeRepository; |
| 19 | + private GetCheckInListAttendeePublicHandler $handler; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->checkInListRepository = m::mock(CheckInListRepositoryInterface::class); |
| 26 | + $this->attendeeRepository = m::mock(AttendeeRepositoryInterface::class); |
| 27 | + |
| 28 | + $this->handler = new GetCheckInListAttendeePublicHandler( |
| 29 | + $this->attendeeRepository, |
| 30 | + $this->checkInListRepository |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testHandleThrowsNotFoundIfCheckInListMissing(): void |
| 35 | + { |
| 36 | + $this->checkInListRepository |
| 37 | + ->shouldReceive('loadRelation') |
| 38 | + ->andReturnSelf() |
| 39 | + ->times(2); |
| 40 | + |
| 41 | + $this->checkInListRepository |
| 42 | + ->shouldReceive('findFirstWhere') |
| 43 | + ->once() |
| 44 | + ->andReturnNull(); |
| 45 | + |
| 46 | + $this->expectException(ResourceNotFoundException::class); |
| 47 | + |
| 48 | + $this->handler->handle('short-id', 'attendee-public-id'); |
| 49 | + } |
| 50 | + |
| 51 | + public function testHandleThrowsCannotCheckInIfListExpired(): void |
| 52 | + { |
| 53 | + $checkInList = m::mock(CheckInListDomainObject::class); |
| 54 | + $checkInList->shouldReceive('getExpiresAt')->twice()->andReturn(now()->subMinute()); |
| 55 | + |
| 56 | + $this->checkInListRepository |
| 57 | + ->shouldReceive('loadRelation') |
| 58 | + ->andReturnSelf() |
| 59 | + ->times(2); |
| 60 | + |
| 61 | + $this->checkInListRepository |
| 62 | + ->shouldReceive('findFirstWhere') |
| 63 | + ->once() |
| 64 | + ->andReturn($checkInList); |
| 65 | + |
| 66 | + $this->expectException(CannotCheckInException::class); |
| 67 | + |
| 68 | + $this->handler->handle('short-id', 'attendee-public-id'); |
| 69 | + } |
| 70 | + |
| 71 | + public function testHandleThrowsCannotCheckInIfListNotActiveYet(): void |
| 72 | + { |
| 73 | + $checkInList = m::mock(CheckInListDomainObject::class); |
| 74 | + $checkInList->shouldReceive('getExpiresAt')->once()->andReturn(null); |
| 75 | + $checkInList->shouldReceive('getActivatesAt')->twice()->andReturn(now()->addMinute()); |
| 76 | + |
| 77 | + $this->checkInListRepository |
| 78 | + ->shouldReceive('loadRelation') |
| 79 | + ->andReturnSelf() |
| 80 | + ->times(2); |
| 81 | + |
| 82 | + $this->checkInListRepository |
| 83 | + ->shouldReceive('findFirstWhere') |
| 84 | + ->once() |
| 85 | + ->andReturn($checkInList); |
| 86 | + |
| 87 | + $this->expectException(CannotCheckInException::class); |
| 88 | + |
| 89 | + $this->handler->handle('short-id', 'attendee-public-id'); |
| 90 | + } |
| 91 | + |
| 92 | + public function testHandleReturnsAttendeeSuccessfully(): void |
| 93 | + { |
| 94 | + $checkInList = m::mock(CheckInListDomainObject::class); |
| 95 | + $checkInList->shouldReceive('getExpiresAt')->once()->andReturn(null); |
| 96 | + $checkInList->shouldReceive('getActivatesAt')->once()->andReturn(null); |
| 97 | + $checkInList->shouldReceive('getEventId')->once()->andReturn(123); |
| 98 | + |
| 99 | + $attendee = m::mock(AttendeeDomainObject::class); |
| 100 | + |
| 101 | + $this->checkInListRepository |
| 102 | + ->shouldReceive('loadRelation') |
| 103 | + ->andReturnSelf() |
| 104 | + ->times(2); |
| 105 | + |
| 106 | + $this->checkInListRepository |
| 107 | + ->shouldReceive('findFirstWhere') |
| 108 | + ->once() |
| 109 | + ->andReturn($checkInList); |
| 110 | + |
| 111 | + $this->attendeeRepository |
| 112 | + ->shouldReceive('findFirstWhere') |
| 113 | + ->once() |
| 114 | + ->with([ |
| 115 | + 'public_id' => 'attendee-public-id', |
| 116 | + 'event_id' => 123, |
| 117 | + ]) |
| 118 | + ->andReturn($attendee); |
| 119 | + |
| 120 | + $result = $this->handler->handle('short-id', 'attendee-public-id'); |
| 121 | + |
| 122 | + $this->assertSame($attendee, $result); |
| 123 | + } |
| 124 | +} |
0 commit comments