|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\Controllers; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Restify; |
| 6 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostAbortMiddleware; |
| 7 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostWithCustomMiddlewareRepository; |
| 8 | +use Binaryk\LaravelRestify\Tests\IntegrationTest; |
| 9 | +use Mockery as m; |
| 10 | + |
| 11 | +class RepositoryMiddlewaresTest extends IntegrationTest |
| 12 | +{ |
| 13 | + protected function tearDown(): void |
| 14 | + { |
| 15 | + m::close(); |
| 16 | + } |
| 17 | + |
| 18 | + public function test_repository_can_have_custom_middleware() |
| 19 | + { |
| 20 | + $middleware = m::mock(PostAbortMiddleware::class); |
| 21 | + |
| 22 | + $nextParam = null; |
| 23 | + |
| 24 | + $middleware |
| 25 | + ->expects('handle') |
| 26 | + ->once(); |
| 27 | + |
| 28 | + PostWithCustomMiddlewareRepository::$middlewares = [ |
| 29 | + $middleware, |
| 30 | + ]; |
| 31 | + |
| 32 | + Restify::repositories([ |
| 33 | + PostWithCustomMiddlewareRepository::class, |
| 34 | + ]); |
| 35 | + |
| 36 | + $this->getJson('restify-api/post-with-middleware') |
| 37 | + ->assertStatus(200); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_request_fails_if_middleware_abort() |
| 41 | + { |
| 42 | + PostWithCustomMiddlewareRepository::$middlewares = [ |
| 43 | + PostAbortMiddleware::class, |
| 44 | + ]; |
| 45 | + |
| 46 | + Restify::repositories([ |
| 47 | + PostWithCustomMiddlewareRepository::class, |
| 48 | + ]); |
| 49 | + |
| 50 | + $this->getJson('restify-api/post-with-middleware') |
| 51 | + ->assertStatus(404); |
| 52 | + } |
| 53 | + |
| 54 | + public function test_foreign_repository_middleware_should_not_be_invoked() |
| 55 | + { |
| 56 | + $middleware = m::mock(PostAbortMiddleware::class); |
| 57 | + |
| 58 | + $nextParam = null; |
| 59 | + |
| 60 | + $middleware |
| 61 | + ->expects('handle') |
| 62 | + ->never(); |
| 63 | + |
| 64 | + PostWithCustomMiddlewareRepository::$middlewares = [ |
| 65 | + $middleware, |
| 66 | + ]; |
| 67 | + |
| 68 | + Restify::repositories([ |
| 69 | + PostWithCustomMiddlewareRepository::class, |
| 70 | + ]); |
| 71 | + |
| 72 | + $this->getJson('restify-api/posts') |
| 73 | + ->assertStatus(200); |
| 74 | + } |
| 75 | +} |
0 commit comments