| Conditions | 1 |
| Paths | 1 |
| Total Lines | 64 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 38 | public function testInvokeTerminatesTheScript(): void |
||
| 39 | { |
||
| 40 | $request = Request::createFromGlobals(); |
||
| 41 | |||
| 42 | //Create a mock response: |
||
| 43 | |||
| 44 | $httpResponseMock = $this |
||
| 45 | ->getMockBuilder(Response::class) |
||
| 46 | ->onlyMethods(['prepare', 'send']) |
||
| 47 | ->getMock() |
||
| 48 | ; |
||
| 49 | |||
| 50 | $httpResponseMock |
||
| 51 | ->expects($this->once()) |
||
| 52 | ->method('prepare') |
||
| 53 | ->with($request) |
||
| 54 | ->will($this->returnSelf()) |
||
| 55 | ; |
||
| 56 | |||
| 57 | //Make sure the response will be sent. |
||
| 58 | $httpResponseMock |
||
| 59 | ->expects($this->once()) |
||
| 60 | ->method('send') |
||
| 61 | ; |
||
| 62 | |||
| 63 | //Create a mock HTTP-response factory that will return our mock response: |
||
| 64 | |||
| 65 | $httpResponseFactoryMock = $this |
||
| 66 | ->getMockBuilder(HttpResponseFactory::class) |
||
| 67 | ->onlyMethods(['createForbiddenResponse']) |
||
| 68 | ->getMock() |
||
| 69 | ; |
||
| 70 | |||
| 71 | $httpResponseFactoryMock |
||
| 72 | ->expects($this->once()) |
||
| 73 | ->method('createForbiddenResponse') |
||
| 74 | ->with("We're not going to handle your request because it looks suspicious. Please contact us if we've made a mistake.") |
||
| 75 | ->willReturn($httpResponseMock) |
||
| 76 | ; |
||
| 77 | |||
| 78 | /** @var HttpResponseFactory $httpResponseFactoryMock */ |
||
| 79 | |||
| 80 | //Mock the PHP-functions wrapper so we can test if the script will be terminated. |
||
| 81 | |||
| 82 | $phpFunctionsMock = $this |
||
| 83 | ->getMockBuilder(PhpFunctionsWrapper::class) |
||
| 84 | ->onlyMethods(['exit']) |
||
| 85 | ->getMock() |
||
| 86 | ; |
||
| 87 | |||
| 88 | $phpFunctionsMock |
||
| 89 | ->expects($this->once()) |
||
| 90 | ->method('exit') |
||
| 91 | ->with(0) |
||
| 92 | ; |
||
| 93 | |||
| 94 | /** @var PhpFunctionsWrapper $phpFunctionsMock */ |
||
| 95 | |||
| 96 | //Run the handler: |
||
| 97 | |||
| 98 | $envelope = new Envelope($request, new NullLogger()); |
||
| 99 | |||
| 100 | $handler = new TerminateScriptHandler($phpFunctionsMock, $httpResponseFactoryMock); |
||
| 101 | $handler($envelope); |
||
| 102 | } |
||
| 104 |