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