|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir PHP Runtime. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <support@ymirapp.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ymir\Runtime\Tests\Unit\Lambda\Handler\Sqs; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Ymir\Runtime\Lambda\Handler\Sqs\AbstractSqsHandler; |
| 18 | +use Ymir\Runtime\Lambda\InvocationEvent\SqsEvent; |
| 19 | +use Ymir\Runtime\Lambda\InvocationEvent\SqsRecord; |
| 20 | +use Ymir\Runtime\Lambda\Response\SqsResponse; |
| 21 | +use Ymir\Runtime\Tests\Mock\ContextMockTrait; |
| 22 | + |
| 23 | +class AbstractSqsHandlerTest extends TestCase |
| 24 | +{ |
| 25 | + use ContextMockTrait; |
| 26 | + |
| 27 | + public function testCanHandleReturnsFalse(): void |
| 28 | + { |
| 29 | + $handler = $this->getMockForAbstractClass(AbstractSqsHandler::class); |
| 30 | + |
| 31 | + $this->assertFalse($handler->canHandle($this->getMockBuilder(\Ymir\Runtime\Lambda\InvocationEvent\InvocationEventInterface::class)->getMock())); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCanHandleReturnsTrue(): void |
| 35 | + { |
| 36 | + $handler = $this->getMockForAbstractClass(AbstractSqsHandler::class); |
| 37 | + |
| 38 | + $this->assertTrue($handler->canHandle(new SqsEvent($this->getContextMock()))); |
| 39 | + } |
| 40 | + |
| 41 | + public function testHandleCollectsFailures(): void |
| 42 | + { |
| 43 | + $record1 = new SqsRecord(['messageId' => 'id1']); |
| 44 | + $record2 = new SqsRecord(['messageId' => 'id2']); |
| 45 | + $record3 = new SqsRecord(['messageId' => 'id3']); |
| 46 | + |
| 47 | + $event = new SqsEvent($this->getContextMock(), [ |
| 48 | + 'Records' => [ |
| 49 | + ['messageId' => 'id1'], |
| 50 | + ['messageId' => 'id2'], |
| 51 | + ['messageId' => 'id3'], |
| 52 | + ], |
| 53 | + ]); |
| 54 | + |
| 55 | + $handler = $this->getMockForAbstractClass(AbstractSqsHandler::class); |
| 56 | + $handler->expects($this->exactly(3)) |
| 57 | + ->method('processRecord') |
| 58 | + ->willReturnCallback(function (SqsRecord $record): void { |
| 59 | + if ('id2' === $record->getMessageId()) { |
| 60 | + throw new \Exception('Failed'); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + $response = $handler->handle($event); |
| 65 | + |
| 66 | + $this->assertInstanceOf(SqsResponse::class, $response); |
| 67 | + $this->assertSame([ |
| 68 | + 'batchItemFailures' => [ |
| 69 | + ['itemIdentifier' => 'id2'], |
| 70 | + ], |
| 71 | + ], $response->getResponseData()); |
| 72 | + } |
| 73 | +} |
0 commit comments