| Conditions | 1 |
| Paths | 1 |
| Total Lines | 60 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 89 | public function testBearerToken() |
||
| 90 | { |
||
| 91 | // Register custom view resources |
||
| 92 | $noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR; |
||
| 93 | ServerFacade::setViewResources([ |
||
| 94 | TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR, |
||
| 95 | TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR, |
||
| 96 | TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR, |
||
| 97 | ]); |
||
| 98 | |||
| 99 | $bearerToken = md5(microtime(true)); |
||
| 100 | $bearerAuthenticator = new Bearer(function ($currentToken) use ($bearerToken) { |
||
| 101 | return $currentToken === $bearerToken; |
||
| 102 | }); |
||
| 103 | |||
| 104 | // Register a static route and add the bearer token authenticator |
||
| 105 | $bearerRoute = RouteFactory::createStaticRoute('/bearer', 'Test/Bearer'); |
||
| 106 | $bearerRoute->setAuth([$bearerAuthenticator]); |
||
| 107 | ServerFacade::registerRoute($bearerRoute); |
||
| 108 | |||
| 109 | // Test authorization header |
||
| 110 | $uri = new Uri('http://apparat/blog/bearer'); |
||
| 111 | $request = new ServerRequest(); |
||
| 112 | $request = $request->withUri($uri)->withAddedHeader('Authorization', 'Bearer '.$bearerToken); |
||
| 113 | $response = ServerFacade::dispatchRequest($request); |
||
| 114 | $this->assertInstanceOf(ResponseInterface::class, $response); |
||
| 115 | $this->assertEquals('[(bearer)]', trim($response->getBody())); |
||
| 116 | |||
| 117 | // Test "access_token" body parameter |
||
| 118 | $uri = new Uri('http://apparat/blog/bearer'); |
||
| 119 | $request = new ServerRequest(); |
||
| 120 | $request = $request->withUri($uri)->withParsedBody(['access_token' => $bearerToken]); |
||
| 121 | $response = ServerFacade::dispatchRequest($request); |
||
| 122 | $this->assertInstanceOf(ResponseInterface::class, $response); |
||
| 123 | $this->assertEquals('[(bearer)]', trim($response->getBody())); |
||
| 124 | |||
| 125 | // Test "access_token" query parameter |
||
| 126 | $uri = new Uri('http://apparat/blog/bearer'); |
||
| 127 | $request = new ServerRequest(); |
||
| 128 | $request = $request->withUri($uri)->withQueryParams(['access_token' => $bearerToken]); |
||
| 129 | $response = ServerFacade::dispatchRequest($request); |
||
| 130 | $this->assertInstanceOf(ResponseInterface::class, $response); |
||
| 131 | $this->assertEquals('[(bearer)]', trim($response->getBody())); |
||
| 132 | |||
| 133 | // Test invalid authorization header |
||
| 134 | $uri = new Uri('http://apparat/blog/bearer'); |
||
| 135 | $request = new ServerRequest(); |
||
| 136 | $request = $request->withUri($uri)->withAddedHeader('Authorization', 'Bearer'); |
||
| 137 | $response = ServerFacade::dispatchRequest($request); |
||
| 138 | $this->assertInstanceOf(ResponseInterface::class, $response); |
||
| 139 | $this->assertEquals(404, $response->getStatusCode()); |
||
| 140 | |||
| 141 | // Test with basic authorization header |
||
| 142 | $uri = new Uri('http://apparat/blog/bearer'); |
||
| 143 | $request = new ServerRequest(); |
||
| 144 | $request = $request->withUri($uri)->withAddedHeader('Authorization', 'Basic '.base64_encode("user:pass")); |
||
| 145 | $response = ServerFacade::dispatchRequest($request); |
||
| 146 | $this->assertInstanceOf(ResponseInterface::class, $response); |
||
| 147 | $this->assertEquals(404, $response->getStatusCode()); |
||
| 148 | } |
||
| 149 | } |
||
| 150 |