|
| 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\Http; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Ymir\Runtime\FastCgi\FastCgiRequest; |
| 18 | +use Ymir\Runtime\Lambda\Handler\Http\PhpScriptHttpEventHandler; |
| 19 | +use Ymir\Runtime\Lambda\Response\Http\FastCgiHttpResponse; |
| 20 | +use Ymir\Runtime\Tests\Mock\FunctionMockTrait; |
| 21 | +use Ymir\Runtime\Tests\Mock\HttpRequestEventMockTrait; |
| 22 | +use Ymir\Runtime\Tests\Mock\InvocationEventInterfaceMockTrait; |
| 23 | +use Ymir\Runtime\Tests\Mock\LoggerMockTrait; |
| 24 | +use Ymir\Runtime\Tests\Mock\PhpFpmProcessMockTrait; |
| 25 | + |
| 26 | +class PhpScriptHttpEventHandlerTest extends TestCase |
| 27 | +{ |
| 28 | + use FunctionMockTrait; |
| 29 | + use HttpRequestEventMockTrait; |
| 30 | + use InvocationEventInterfaceMockTrait; |
| 31 | + use LoggerMockTrait; |
| 32 | + use PhpFpmProcessMockTrait; |
| 33 | + |
| 34 | + public function testCanHandleNonExistentScriptFile(): void |
| 35 | + { |
| 36 | + $file_exists = $this->getFunctionMock($this->getNamespace(PhpScriptHttpEventHandler::class), 'file_exists'); |
| 37 | + $process = $this->getPhpFpmProcessMock(); |
| 38 | + |
| 39 | + $file_exists->expects($this->any()) |
| 40 | + ->willReturn(false); |
| 41 | + |
| 42 | + $handler = new PhpScriptHttpEventHandler($this->getLoggerMock(), $process, '/', 'tmp.php'); |
| 43 | + |
| 44 | + $this->assertFalse($handler->canHandle($this->getHttpRequestEventMock())); |
| 45 | + } |
| 46 | + |
| 47 | + public function testCanHandleWithValidScriptFile(): void |
| 48 | + { |
| 49 | + $file_exists = $this->getFunctionMock($this->getNamespace(PhpScriptHttpEventHandler::class), 'file_exists'); |
| 50 | + $process = $this->getPhpFpmProcessMock(); |
| 51 | + |
| 52 | + $file_exists->expects($this->once()) |
| 53 | + ->with($this->identicalTo('/tmp.php')) |
| 54 | + ->willReturn(true); |
| 55 | + |
| 56 | + $handler = new PhpScriptHttpEventHandler($this->getLoggerMock(), $process, '', 'tmp.php'); |
| 57 | + |
| 58 | + $this->assertTrue($handler->canHandle($this->getHttpRequestEventMock())); |
| 59 | + } |
| 60 | + |
| 61 | + public function testCanHandleWrongEventType(): void |
| 62 | + { |
| 63 | + $file_exists = $this->getFunctionMock($this->getNamespace(PhpScriptHttpEventHandler::class), 'file_exists'); |
| 64 | + $process = $this->getPhpFpmProcessMock(); |
| 65 | + |
| 66 | + $file_exists->expects($this->any()) |
| 67 | + ->with($this->identicalTo('/tmp.php')) |
| 68 | + ->willReturn(true); |
| 69 | + |
| 70 | + $handler = new PhpScriptHttpEventHandler($this->getLoggerMock(), $process, '/', 'tmp.php'); |
| 71 | + |
| 72 | + $this->assertFalse($handler->canHandle($this->getInvocationEventInterfaceMock())); |
| 73 | + } |
| 74 | + |
| 75 | + public function testCanHandleWrongScriptFileType(): void |
| 76 | + { |
| 77 | + $process = $this->getPhpFpmProcessMock(); |
| 78 | + |
| 79 | + $handler = new PhpScriptHttpEventHandler($this->getLoggerMock(), $process, '/', 'tmp'); |
| 80 | + |
| 81 | + $this->assertFalse($handler->canHandle($this->getHttpRequestEventMock())); |
| 82 | + } |
| 83 | + |
| 84 | + public function testHandleCreatesFastCgiHttpResponseWithPayloadVersion1(): void |
| 85 | + { |
| 86 | + $event = $this->getHttpRequestEventMock(); |
| 87 | + $file_exists = $this->getFunctionMock($this->getNamespace(PhpScriptHttpEventHandler::class), 'file_exists'); |
| 88 | + $process = $this->getPhpFpmProcessMock(); |
| 89 | + |
| 90 | + $event->expects($this->exactly(2)) |
| 91 | + ->method('getPath') |
| 92 | + ->willReturn('tmp'); |
| 93 | + |
| 94 | + $event->expects($this->once()) |
| 95 | + ->method('getPayloadVersion') |
| 96 | + ->willReturn('1.0'); |
| 97 | + |
| 98 | + $process->expects($this->once()) |
| 99 | + ->method('handle') |
| 100 | + ->with($this->isInstanceOf(FastCgiRequest::class)); |
| 101 | + |
| 102 | + $file_exists->expects($this->any()) |
| 103 | + ->willReturn(true); |
| 104 | + |
| 105 | + $handler = new PhpScriptHttpEventHandler($this->getLoggerMock(), $process, '/', 'tmp.php'); |
| 106 | + |
| 107 | + $this->assertInstanceOf(FastCgiHttpResponse::class, $handler->handle($event)); |
| 108 | + } |
| 109 | + |
| 110 | + public function testHandleCreatesFastCgiHttpResponseWithPayloadVersion2(): void |
| 111 | + { |
| 112 | + $event = $this->getHttpRequestEventMock(); |
| 113 | + $file_exists = $this->getFunctionMock($this->getNamespace(PhpScriptHttpEventHandler::class), 'file_exists'); |
| 114 | + $process = $this->getPhpFpmProcessMock(); |
| 115 | + |
| 116 | + $event->expects($this->exactly(2)) |
| 117 | + ->method('getPath') |
| 118 | + ->willReturn('tmp'); |
| 119 | + |
| 120 | + $event->expects($this->once()) |
| 121 | + ->method('getPayloadVersion') |
| 122 | + ->willReturn('2.0'); |
| 123 | + |
| 124 | + $process->expects($this->once()) |
| 125 | + ->method('handle') |
| 126 | + ->with($this->isInstanceOf(FastCgiRequest::class)); |
| 127 | + |
| 128 | + $file_exists->expects($this->any()) |
| 129 | + ->willReturn(true); |
| 130 | + |
| 131 | + $handler = new PhpScriptHttpEventHandler($this->getLoggerMock(), $process, '/', 'tmp.php'); |
| 132 | + |
| 133 | + $this->assertInstanceOf(FastCgiHttpResponse::class, $handler->handle($event)); |
| 134 | + } |
| 135 | +} |
0 commit comments