Skip to content

Commit 284051e

Browse files
committed
Un mock developer tests for curl calls, now use the client
1 parent 9ec1425 commit 284051e

1 file changed

Lines changed: 61 additions & 25 deletions

File tree

tests/TestCase/Controller/DevelopersControllerTest.php

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use App\Model\Table\DevelopersTable;
2222
use App\Test\Fixture\DevelopersFixture;
2323
use App\Test\Fixture\NotificationsFixture;
24+
use Cake\Http\TestSuite\HttpClientTrait;
2425
use Cake\ORM\TableRegistry;
2526
use Cake\TestSuite\IntegrationTestTrait;
2627
use Cake\TestSuite\TestCase;
@@ -38,6 +39,7 @@ class DevelopersControllerTest extends TestCase
3839
{
3940
use PHPMock;
4041
use IntegrationTestTrait;
42+
use HttpClientTrait;
4143

4244
protected DevelopersTable $Developers;
4345

@@ -73,45 +75,40 @@ public function testLogin(): void
7375
*/
7476
public function testCallback(): void
7577
{
76-
// Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent
77-
// so that we don't actually hit the Github Api
78-
$curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec');
79-
$curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo');
80-
8178
$accessTokenResponse = json_encode(['access_token' => 'abc']);
8279
$emptyAccessTokenResponse = json_encode(['access_token' => null]);
8380

8481
$nonSuccessUserResponse = json_encode(['message' => 'Unauthorized access']);
8582
$userResponse = file_get_contents(TESTS . 'Fixture' . DS . 'user_response.json');
8683

87-
// Github unsuccessful response followed by successful response
88-
$curlExecMock->expects($this->exactly(10))->willReturnOnConsecutiveCalls(
89-
$emptyAccessTokenResponse,
90-
$emptyAccessTokenResponse,
91-
$accessTokenResponse,
92-
$nonSuccessUserResponse,
93-
$accessTokenResponse,
94-
$userResponse,
95-
null,
96-
$accessTokenResponse,
97-
$userResponse,
98-
null
99-
);
100-
$curlGetInfoMock->expects($this->exactly(5))->willReturnOnConsecutiveCalls(
101-
401,
102-
200,
103-
404,
104-
200,
105-
204
84+
// Data for 1.1
85+
$this->cleanupMockResponses();
86+
$this->mockClientPost(
87+
'https://github.com/login/oauth/access_token',
88+
$this->newClientResponse(401, [], $emptyAccessTokenResponse),
10689
);
107-
10890
// Case 1.1 Test no access_token in Github response (with last_page not set in session)
10991
// So, empty the session
11092
$this->session([]);
11193

94+
$this->cleanupMockResponses();
95+
$this->mockClientPost(
96+
'https://github.com/login/oauth/access_token',
97+
$this->newClientResponse(200, [], $emptyAccessTokenResponse),
98+
);
11299
$this->get('developers/callback/?code=123123123');
113100
$this->assertRedirect(['controller' => '', 'action' => 'index']);
114101

102+
// Data for 1.2
103+
$this->cleanupMockResponses();
104+
$this->mockClientPost(
105+
'https://github.com/login/oauth/access_token',
106+
$this->newClientResponse(200, [], $accessTokenResponse),
107+
);
108+
$this->mockClientGet(
109+
'https://api.github.com/user',
110+
$this->newClientResponse(404, [], $nonSuccessUserResponse),
111+
);
115112
// Case 1.2 Test no access_token in Github response (with last_page set in session)
116113
$this->session(
117114
[
@@ -125,6 +122,16 @@ public function testCallback(): void
125122
$this->get('developers/callback/?code=123123123');
126123
$this->assertRedirect(['controller' => '', 'action' => 'index']);
127124

125+
// Data for 2.
126+
$this->cleanupMockResponses();
127+
$this->mockClientPost(
128+
'https://github.com/login/oauth/access_token',
129+
$this->newClientResponse(200, [], $accessTokenResponse),
130+
);
131+
$this->mockClientGet(
132+
'https://api.github.com/user',
133+
$this->newClientResponse(404, [], $nonSuccessUserResponse),
134+
);
128135
// Case 2. Non successful response code from Github
129136
$this->session(
130137
[
@@ -137,6 +144,21 @@ public function testCallback(): void
137144
$this->get('developers/callback/?code=123123123');
138145
$this->assertRedirect(['controller' => '', 'action' => 'index']);
139146

147+
// Data for 3.
148+
$this->cleanupMockResponses();
149+
$this->mockClientPost(
150+
'https://github.com/login/oauth/access_token',
151+
$this->newClientResponse(200, [], $accessTokenResponse),
152+
);
153+
$this->mockClientGet(
154+
'https://api.github.com/user',
155+
$this->newClientResponse(200, [], $userResponse),
156+
);
157+
$this->mockClientGet(
158+
'https://api.github.com/repos/phpmyadmin/phpmyadmin/collaborators/pma-bot',
159+
$this->newClientResponse(200, [], json_encode([])),
160+
);
161+
140162
// Case 3. Successful response code (new user), check whether session variables are init
141163
$this->get('developers/callback/?code=123123123');
142164
$this->assertSession(3, 'Developer.id');
@@ -147,6 +169,20 @@ public function testCallback(): void
147169
$this->assertEquals('abc', $developer->access_token);
148170
$this->assertEquals('pma-bot@phpmyadmin.net', $developer->email);
149171

172+
// Data for 4.
173+
$this->cleanupMockResponses();
174+
$this->mockClientPost(
175+
'https://github.com/login/oauth/access_token',
176+
$this->newClientResponse(200, [], $accessTokenResponse),
177+
);
178+
$this->mockClientGet(
179+
'https://api.github.com/user',
180+
$this->newClientResponse(200, [], $userResponse),
181+
);
182+
$this->mockClientGet(
183+
'https://api.github.com/repos/phpmyadmin/phpmyadmin/collaborators/pma-bot',
184+
$this->newClientResponse(204, [], json_encode([])),
185+
);
150186
// Case 4. Successful response code (returning user)
151187
// check whether session variables are init
152188
$this->session(['last_page' => null]);

0 commit comments

Comments
 (0)