|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RenatoMarinho\LaravelPageSpeed\Test\Middleware; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use RenatoMarinho\LaravelPageSpeed\Test\TestCase; |
| 8 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
| 9 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 10 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 11 | +use RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed; |
| 12 | +use RenatoMarinho\LaravelPageSpeed\Middleware\CollapseWhitespace; |
| 13 | + |
| 14 | +class ShouldNotProcessResponseTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * PageSpeed middleware instance. |
| 18 | + * |
| 19 | + * @var \RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed |
| 20 | + */ |
| 21 | + protected $middleware; |
| 22 | + |
| 23 | + /** |
| 24 | + * Clean up the testing environment before the next test. |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + protected function tearDown() |
| 29 | + { |
| 30 | + parent::tearDown(); |
| 31 | + m::close(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Test that a BinaryFileResponse is ignored by any middleware. |
| 36 | + * |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + public function testSkipBinaryFileResponse() |
| 40 | + { |
| 41 | + $request = Request::create('/', 'GET', [], [], ['file' => new UploadedFile(__FILE__, 'foo.php')]); |
| 42 | + |
| 43 | + $response = $this->middleware->handle($request, $this->getNextBinaryFileResponse()); |
| 44 | + |
| 45 | + $this->assertInstanceOf(BinaryFileResponse::class, $response); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test that a StreamedResponse is ignored by any middleware. |
| 50 | + * |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function testSkipStreamedResponse() |
| 54 | + { |
| 55 | + $request = Request::create('/', 'GET'); |
| 56 | + |
| 57 | + $response = $this->middleware->handle($request, $this->getNextStreamedResponse()); |
| 58 | + |
| 59 | + $this->assertInstanceOf(StreamedResponse::class, $response); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test a LogicException is throw when trying to process a |
| 64 | + * BinaryFileResponse. |
| 65 | + * |
| 66 | + * @return void |
| 67 | + * |
| 68 | + * @expectedException \LogicException |
| 69 | + */ |
| 70 | + public function testExpectLogicExceptionInBinaryFileResponse() |
| 71 | + { |
| 72 | + $request = Request::create('/', 'GET', [], [], ['file' => new UploadedFile(__FILE__, 'foo.php')]); |
| 73 | + |
| 74 | + $middleware = $this->mockMiddlewareWhichAllowsPageSpeedProcess(); |
| 75 | + |
| 76 | + $middleware->handle($request, $this->getNextBinaryFileResponse()); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Test a LogicException is throw when trying to process a |
| 81 | + * StreamedResponse. |
| 82 | + * |
| 83 | + * @return void |
| 84 | + * |
| 85 | + * @expectedException \LogicException |
| 86 | + */ |
| 87 | + public function testExpectLogicExceptionInStreamedResponse() |
| 88 | + { |
| 89 | + $request = Request::create('/', 'GET'); |
| 90 | + |
| 91 | + $middleware = $this->mockMiddlewareWhichAllowsPageSpeedProcess(); |
| 92 | + |
| 93 | + $middleware->handle($request, $this->getNextStreamedResponse()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Mock a BinaryFileResponse. |
| 98 | + * |
| 99 | + * @return \Closure |
| 100 | + */ |
| 101 | + protected function getNextBinaryFileResponse() |
| 102 | + { |
| 103 | + return function ($request) { |
| 104 | + return response()->download($request->file); |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Mock a StreamedResponse. |
| 110 | + * |
| 111 | + * @return \Closure |
| 112 | + */ |
| 113 | + protected function getNextStreamedResponse() |
| 114 | + { |
| 115 | + return function ($request) { |
| 116 | + $response = new StreamedResponse(function () { |
| 117 | + echo "I am Streamed"; |
| 118 | + }); |
| 119 | + |
| 120 | + $response->headers->set('Content-Disposition', $response->headers->makeDisposition( |
| 121 | + 'attachment', |
| 122 | + 'foo.txt' |
| 123 | + )); |
| 124 | + |
| 125 | + return $response; |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Return an instance of the middleware which always |
| 131 | + * allows processing of response. |
| 132 | + * |
| 133 | + * @return m\Mock|PageSpeed |
| 134 | + */ |
| 135 | + protected function mockMiddlewareWhichAllowsPageSpeedProcess() |
| 136 | + { |
| 137 | + $mock = m::mock(CollapseWhitespace::class) |
| 138 | + ->shouldAllowMockingProtectedMethods() |
| 139 | + ->makePartial(); |
| 140 | + |
| 141 | + $mock->shouldReceive('shouldProcessPageSpeed') |
| 142 | + ->once() |
| 143 | + ->andReturn(true); |
| 144 | + |
| 145 | + return $mock; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Middleware used during this test. |
| 150 | + * |
| 151 | + * @return void |
| 152 | + */ |
| 153 | + protected function getMiddleware() |
| 154 | + { |
| 155 | + $this->middleware = new CollapseWhitespace(); |
| 156 | + } |
| 157 | +} |
0 commit comments