| Conditions | 1 |
| Paths | 1 |
| Total Lines | 87 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 70 | public function testCallback(): void |
||
| 71 | { |
||
| 72 | // Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent |
||
| 73 | // so that we don't actually hit the Github Api |
||
| 74 | $curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec'); |
||
| 75 | $curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo'); |
||
| 76 | |||
| 77 | $accessTokenResponse = json_encode(['access_token' => 'abc']); |
||
| 78 | $emptyAccessTokenResponse = json_encode(['access_token' => null]); |
||
| 79 | |||
| 80 | $nonSuccessUserResponse = json_encode(['message' => 'Unauthorized access']); |
||
| 81 | $userResponse = file_get_contents(TESTS . 'Fixture' . DS . 'user_response.json'); |
||
| 82 | |||
| 83 | // Github unsuccessful response followed by successful response |
||
| 84 | $curlExecMock->expects($this->exactly(10))->willReturnOnConsecutiveCalls( |
||
| 85 | $emptyAccessTokenResponse, |
||
| 86 | $emptyAccessTokenResponse, |
||
| 87 | $accessTokenResponse, |
||
| 88 | $nonSuccessUserResponse, |
||
| 89 | $accessTokenResponse, |
||
| 90 | $userResponse, |
||
| 91 | null, |
||
| 92 | $accessTokenResponse, |
||
| 93 | $userResponse, |
||
| 94 | null |
||
| 95 | ); |
||
| 96 | $curlGetInfoMock->expects($this->exactly(5))->willReturnOnConsecutiveCalls( |
||
| 97 | 401, |
||
| 98 | 200, |
||
| 99 | 404, |
||
| 100 | 200, |
||
| 101 | 204 |
||
| 102 | ); |
||
| 103 | |||
| 104 | // Case 1.1 Test no access_token in Github response (with last_page not set in session) |
||
| 105 | // So, empty the session |
||
| 106 | $this->session([]); |
||
| 107 | |||
| 108 | $this->get('developers/callback/?code=123123123'); |
||
| 109 | $this->assertRedirect(['controller' => '', 'action' => 'index']); |
||
| 110 | |||
| 111 | // Case 1.2 Test no access_token in Github response (with last_page set in session) |
||
| 112 | $this->session( |
||
| 113 | [ |
||
| 114 | 'last_page' => [ |
||
| 115 | 'controller' => 'notifications', |
||
| 116 | 'action' => 'index', |
||
| 117 | ], |
||
| 118 | ] |
||
| 119 | ); |
||
| 120 | |||
| 121 | $this->get('developers/callback/?code=123123123'); |
||
| 122 | $this->assertRedirect(['controller' => '', 'action' => 'index']); |
||
| 123 | |||
| 124 | // Case 2. Non successful response code from Github |
||
| 125 | $this->session( |
||
| 126 | [ |
||
| 127 | 'last_page' => [ |
||
| 128 | 'controller' => 'reports', |
||
| 129 | 'action' => 'index', |
||
| 130 | ], |
||
| 131 | ] |
||
| 132 | ); |
||
| 133 | $this->get('developers/callback/?code=123123123'); |
||
| 134 | $this->assertRedirect(['controller' => '', 'action' => 'index']); |
||
| 135 | |||
| 136 | // Case 3. Successful response code (new user), check whether session variables are init |
||
| 137 | $this->get('developers/callback/?code=123123123'); |
||
| 138 | $this->assertSession(3, 'Developer.id'); |
||
| 139 | $this->assertSession(true, 'read_only'); |
||
| 140 | $this->assertSession('abc', 'access_token'); |
||
| 141 | |||
| 142 | $developer = $this->Developers->get(3); |
||
| 143 | $this->assertEquals('abc', $developer->access_token); |
||
| 144 | $this->assertEquals('[email protected]', $developer->email); |
||
| 145 | |||
| 146 | // Case 4. Successful response code (returning user) |
||
| 147 | // check whether session variables are init |
||
| 148 | $this->session(['last_page' => null]); |
||
| 149 | |||
| 150 | $this->get('developers/callback/?code=123123123'); |
||
| 151 | $this->assertSession(3, 'Developer.id'); |
||
| 152 | $this->assertSession(false, 'read_only'); |
||
| 153 | $this->assertSession('abc', 'access_token'); |
||
| 154 | |||
| 155 | $developer = $this->Developers->get(3); |
||
| 156 | $this->assertEquals(1, $developer->has_commit_access); |
||
| 157 | } |
||
| 171 |