|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Softonic\RestApiNestedResources\Http\Middleware; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Illuminate\Support\Facades\Response; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Softonic\RestApiNestedResources\PreProcessors\EnsureModelExists as EnsureModelExistsProcessor; |
| 9 | +use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
| 10 | + |
| 11 | +class EnsureModelExistsTest extends TestCase |
| 12 | +{ |
| 13 | + private EnsureModelExistsProcessor $ensureModelExistsProcessor; |
| 14 | + |
| 15 | + private EnsureModelExists $middleware; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $this->ensureModelExistsProcessor = \Mockery::mock(EnsureModelExistsProcessor::class); |
| 22 | + |
| 23 | + $this->middleware = new EnsureModelExists($this->ensureModelExistsProcessor); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @test |
| 28 | + */ |
| 29 | + public function whenModelExistsItShouldPassToTheNextMiddleware() |
| 30 | + { |
| 31 | + $pathParameters = [ |
| 32 | + 'program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', |
| 33 | + 'platform' => 'windows', |
| 34 | + ]; |
| 35 | + $requestParameters = [ |
| 36 | + 'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', |
| 37 | + 'id_platform' => 'mac', |
| 38 | + 'id_version' => '1.0', |
| 39 | + 'id_platformversion' => 'test', |
| 40 | + ]; |
| 41 | + $request = $this->getRequest($pathParameters, $requestParameters); |
| 42 | + |
| 43 | + $this->ensureModelExistsProcessor->shouldReceive('process') |
| 44 | + ->once() |
| 45 | + ->with( |
| 46 | + 'PlatformVersion', |
| 47 | + ['id_platform', 'id_version=id_platformversion'], |
| 48 | + [ |
| 49 | + 'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', |
| 50 | + 'id_platform' => 'windows', |
| 51 | + 'id_version' => '1.0', |
| 52 | + 'id_platformversion' => 'test', |
| 53 | + ] |
| 54 | + ); |
| 55 | + |
| 56 | + [$controllerResponse, $next] = $this->whenNextMiddlewareIsExecuted(); |
| 57 | + |
| 58 | + $response = $this->middleware->handle( |
| 59 | + $request, |
| 60 | + $next, |
| 61 | + 'PlatformVersion', |
| 62 | + 'id_platform', |
| 63 | + 'id_version=id_platformversion' |
| 64 | + ); |
| 65 | + |
| 66 | + self::assertSame($controllerResponse, $response, 'The response must not be modified'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @test |
| 71 | + */ |
| 72 | + public function whenModelDoesNotExistItShouldAbortTheExecution() |
| 73 | + { |
| 74 | + $pathParameters = [ |
| 75 | + 'program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', |
| 76 | + 'platform' => 'windows', |
| 77 | + ]; |
| 78 | + $requestParameters = [ |
| 79 | + 'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', |
| 80 | + 'id_platform' => 'mac', |
| 81 | + 'id_version' => '1.0', |
| 82 | + 'id_platformversion' => 'test', |
| 83 | + ]; |
| 84 | + $request = $this->getRequest($pathParameters, $requestParameters); |
| 85 | + |
| 86 | + $this->ensureModelExistsProcessor->shouldReceive('process') |
| 87 | + ->once() |
| 88 | + ->with( |
| 89 | + 'PlatformVersion', |
| 90 | + ['id_platform', 'id_version=id_platformversion'], |
| 91 | + [ |
| 92 | + 'id_program' => 'f0f1ae26-e44a-460a-9f59-f53c83ec4372', |
| 93 | + 'id_platform' => 'windows', |
| 94 | + 'id_version' => '1.0', |
| 95 | + 'id_platformversion' => 'test', |
| 96 | + ] |
| 97 | + ) |
| 98 | + ->andThrow(new ConflictHttpException('error message')); |
| 99 | + |
| 100 | + $next = $this->whenNextMiddlewareIsNotExecuted(); |
| 101 | + |
| 102 | + $this->expectException(ConflictHttpException::class); |
| 103 | + $this->expectExceptionMessage('error message'); |
| 104 | + |
| 105 | + $this->middleware->handle( |
| 106 | + $request, |
| 107 | + $next, |
| 108 | + 'PlatformVersion', |
| 109 | + 'id_platform', |
| 110 | + 'id_version=id_platformversion' |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private function getRequest(array $pathParameters, array $requestParameters): Request |
| 115 | + { |
| 116 | + $request = \Mockery::mock(Request::class); |
| 117 | + $request->shouldReceive('route->parameters') |
| 118 | + ->once() |
| 119 | + ->andReturn($pathParameters); |
| 120 | + $request->shouldReceive('all') |
| 121 | + ->once() |
| 122 | + ->andReturn($requestParameters); |
| 123 | + |
| 124 | + return $request; |
| 125 | + } |
| 126 | + |
| 127 | + private function whenNextMiddlewareIsExecuted(): array |
| 128 | + { |
| 129 | + $controllerResponse = Response::getFacadeRoot(); |
| 130 | + $next = function (Request $request) use ($controllerResponse) { |
| 131 | + return $controllerResponse; |
| 132 | + }; |
| 133 | + |
| 134 | + return [$controllerResponse, $next]; |
| 135 | + } |
| 136 | + |
| 137 | + private function whenNextMiddlewareIsNotExecuted(): \Closure |
| 138 | + { |
| 139 | + return function (Request $request) { |
| 140 | + self::assertTrue(false, 'The next handler must never be executed in this case.'); |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments