|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rakutentech\LaravelRequestDocs\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use Illuminate\Support\Facades\Log; |
| 7 | +use Illuminate\Support\Facades\Route; |
| 8 | +use Rakutentech\LaravelRequestDocs\LaravelRequestDocsMiddleware; |
| 9 | + |
| 10 | +class LaravelRequestDocsMiddlewareTest extends TestCase |
| 11 | +{ |
| 12 | + public function testMissingLRDHeader() |
| 13 | + { |
| 14 | + Route::get('middleware', function () { |
| 15 | + return ['test' => true]; |
| 16 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 17 | + |
| 18 | + $this->get('middleware') |
| 19 | + ->assertStatus(200) |
| 20 | + ->assertExactJson(['test' => true]); |
| 21 | + } |
| 22 | + |
| 23 | + public function testNotJsonResponse() |
| 24 | + { |
| 25 | + Route::get('middleware', function () { |
| 26 | + return 1; |
| 27 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 28 | + |
| 29 | + $response = $this->get('middleware', ['X-Request-LRD' => true]) |
| 30 | + ->assertStatus(200) |
| 31 | + ->assertExactJson([1]); |
| 32 | + } |
| 33 | + |
| 34 | + public function testJsonResponseIsObject() |
| 35 | + { |
| 36 | + Route::get('middleware', function () { |
| 37 | + return response()->json(['test' => true]); |
| 38 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 39 | + |
| 40 | + $response = $this->get('middleware', ['X-Request-LRD' => true]) |
| 41 | + ->assertStatus(200); |
| 42 | + |
| 43 | + $content = collect($response->json()); |
| 44 | + |
| 45 | + $this->assertSame(['test' => true], $content->get('data')); |
| 46 | + |
| 47 | + $lrd = $content->get('_lrd'); |
| 48 | + $this->assertCount(5, $lrd); |
| 49 | + $this->assertArrayHasKey('queries', $lrd); |
| 50 | + $this->assertArrayHasKey('logs', $lrd); |
| 51 | + $this->assertArrayHasKey('models', $lrd); |
| 52 | + $this->assertArrayHasKey('modelsTimeline', $lrd); |
| 53 | + $this->assertArrayHasKey('memory', $lrd); |
| 54 | + } |
| 55 | + |
| 56 | + public function testJsonResponseIsNotObject() |
| 57 | + { |
| 58 | + Route::get('middleware', function () { |
| 59 | + return response()->json('abc'); |
| 60 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 61 | + |
| 62 | + $response = $this->get('middleware', ['X-Request-LRD' => true]) |
| 63 | + ->assertStatus(200); |
| 64 | + |
| 65 | + $content = collect($response->json()); |
| 66 | + |
| 67 | + $this->assertSame('abc', $content->get('data')); |
| 68 | + |
| 69 | + $lrd = $content->get('_lrd'); |
| 70 | + $this->assertCount(5, $lrd); |
| 71 | + } |
| 72 | + |
| 73 | + public function testResponseIsGzipable() |
| 74 | + { |
| 75 | + Route::get('middleware', function () { |
| 76 | + return response()->json(['test' => true]); |
| 77 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 78 | + |
| 79 | + $this->get( |
| 80 | + 'middleware', |
| 81 | + [ |
| 82 | + 'X-Request-LRD' => true, |
| 83 | + 'Accept-Encoding' => 'gzip', |
| 84 | + ] |
| 85 | + ) |
| 86 | + ->assertStatus(200); |
| 87 | + } |
| 88 | + |
| 89 | + public function testLogListenerIsWorking() |
| 90 | + { |
| 91 | + Route::get('middleware', function () { |
| 92 | + Log::info('aaa'); |
| 93 | + return response()->json(['test' => true]); |
| 94 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 95 | + |
| 96 | + $response = $this->get('middleware', ['X-Request-LRD' => true]) |
| 97 | + ->assertStatus(200); |
| 98 | + |
| 99 | + $content = collect($response->json()); |
| 100 | + |
| 101 | + $lrd = $content->get('_lrd'); |
| 102 | + |
| 103 | + $this->assertSame([ |
| 104 | + [ |
| 105 | + 'level' => 'info', |
| 106 | + 'message' => 'aaa', |
| 107 | + 'context' => [], |
| 108 | + ], |
| 109 | + ], $lrd['logs']); |
| 110 | + } |
| 111 | + |
| 112 | + public function testDBListenerIsWorking() |
| 113 | + { |
| 114 | + Route::get('middleware', function () { |
| 115 | + DB::select('SELECT 1'); |
| 116 | + return response()->json(['test' => true]); |
| 117 | + })->middleware(LaravelRequestDocsMiddleware::class); |
| 118 | + |
| 119 | + $response = $this->get('middleware', ['X-Request-LRD' => true]) |
| 120 | + ->assertStatus(200); |
| 121 | + |
| 122 | + $content = collect($response->json()); |
| 123 | + |
| 124 | + $lrd = $content->get('_lrd'); |
| 125 | + |
| 126 | + $this->assertNotEmpty($lrd['queries']); |
| 127 | + $this->assertSame('SELECT 1', $lrd['queries'][0]['sql']); |
| 128 | + } |
| 129 | +} |
0 commit comments