Skip to content

Commit 03f2633

Browse files
authored
Merge pull request #24 from spinen/feature/supportLaravel7
Feature/support laravel7
2 parents 9eae0bd + e0b6000 commit 03f2633

File tree

5 files changed

+88
-22
lines changed

5 files changed

+88
-22
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ matrix:
1313
- php: 7.3
1414
- php: 7.3
1515
env: SETUP=lowest
16+
- php: 7.4
17+
- php: 7.4
18+
env: SETUP=lowest
1619

1720
sudo: false
1821

composer.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@
1414
{
1515
"name": "Jimmy Puckett",
1616
"email": "[email protected]"
17+
},
18+
{
19+
"name": "Stephen Finney",
20+
"email": "[email protected]"
1721
}
1822
],
1923
"require": {
2024
"php": ">=7.2",
2125
"ext-json": "*",
2226
"cviebrock/discourse-php": "^0.9.3",
2327
"guzzlehttp/guzzle": "^6.4",
24-
"illuminate/auth": "~5.5|~6",
25-
"illuminate/routing": "~5.5|~6",
26-
"illuminate/support": "~5.5|~6"
28+
"illuminate/auth": "~5.5|~6|~7",
29+
"illuminate/routing": "~5.5|~6|~7",
30+
"illuminate/support": "~5.5|~6|~7"
2731
},
2832
"require-dev": {
29-
"mockery/mockery": "^1",
30-
"phpunit/phpunit": "~7.0.1|~8.0",
31-
"psy/psysh": "^0.9.9",
33+
"mockery/mockery": "^1.3.1",
34+
"phpunit/phpunit": "^8.4|^9.0",
35+
"psy/psysh": "^0.10",
3236
"symfony/thanks": "^1.1",
3337
"symfony/var-dumper": "~3.0|^4.2"
3438
},

phpunit.xml.dist

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
convertErrorsToExceptions="true"
1515
convertNoticesToExceptions="true"
1616
convertWarningsToExceptions="true"
17-
processIsolation="true"
17+
processIsolation="false"
1818
stopOnFailure="false"
1919
verbose="true">
2020

@@ -48,7 +48,7 @@
4848
showOnlySummary="true"
4949
showUncoveredFiles="false"/>
5050
<log type="coverage-clover" target="build/phpunit/logs/clover.xml"/>
51-
<log type="json" target="./build/phpunit/logs/logfile.json"/>
51+
<log type="plain" target="./build/phpunit/logs/logfile.log"/>
5252
<log type="junit" target="./build/phpunit/logs/junit.xml"/>
5353
</logging>
5454
</phpunit>

src/Listeners/LogoutDiscourseUser.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,18 @@ public function handle($event)
7070
];
7171

7272
// Get Discourse user to match this one, and send a Logout request to Discourse and get the response
73-
$user = json_decode(
74-
$this->client->get("users/by-external/{$event->user->id}.json", $configs)
75-
->getBody()
76-
)->user;
73+
$response = $this->client->get("users/by-external/{$event->user->id}.json", $configs);
7774

78-
$response = $this->client->post("admin/users/{$user->id}/log_out");
75+
if ($response->getStatusCode() !== 200) {
76+
$this->logger->warning(
77+
"When getting user {$event->user->id} Discourse returned status code {$response->getStatusCode()}",
78+
['reason' => $response->getReasonPhrase()]
79+
);
80+
return;
81+
}
82+
83+
$user = json_decode($response->getBody())->user;
84+
$response = $this->client->post("admin/users/{$user->id}/log_out", $configs);
7985

8086
if ($response->getStatusCode() !== 200) {
8187
$this->logger->notice(

tests/Listeners/LogoutDiscourseUserTest.php

+62-9
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function it_logs_out_the_discourse_user_when_triggered()
129129
->andReturn(json_encode(['user' => $this->user_mock]));
130130

131131
$this->response_mock->shouldReceive('getStatusCode')
132-
->once()
132+
->twice()
133133
->andReturn(200);
134134

135135
$this->guzzle_mock->shouldReceive('get')
@@ -138,7 +138,7 @@ public function it_logs_out_the_discourse_user_when_triggered()
138138
->andReturn($this->response_mock);
139139

140140
$this->guzzle_mock->shouldReceive('post')
141-
->with('admin/users/1/log_out')
141+
->with('admin/users/1/log_out', $configs)
142142
->andReturn($this->response_mock);
143143

144144
$this->listener->handle($this->event_mock);
@@ -160,13 +160,12 @@ public function if_it_receives_no_user_it_does_nothing_and_returns()
160160
/**
161161
* @test
162162
*/
163-
public function if_discourse_response_code_is_not_200_log_a_notice_with_the_status_code()
163+
public function on_getting_user_if_discourse_response_code_is_not_200_log_a_warning_with_the_status_code()
164164
{
165165
$this->user_mock->id = 1;
166166
$this->event_mock->user = $this->user_mock;
167167

168-
$this->logger_mock->shouldReceive('notice')->once();
169-
168+
$this->logger_mock->shouldReceive('warning')->once();
170169

171170
$configs = [
172171
'base_uri' => 'http://discourse.example.com',
@@ -191,9 +190,53 @@ public function if_discourse_response_code_is_not_200_log_a_notice_with_the_stat
191190
->once()
192191
->andReturn($configs['headers']['Api-Username']);
193192

194-
$this->response_mock->shouldReceive('getBody')
193+
$this->response_mock->shouldReceive('getStatusCode')
194+
->andReturn(500);
195+
196+
$this->response_mock->shouldReceive('getReasonPhrase')
195197
->once()
196-
->andReturn(json_encode(['user' => $this->user_mock]));
198+
->andReturn('Server error');
199+
200+
$this->guzzle_mock->shouldReceive('get')
201+
->with('users/by-external/1.json', $configs)
202+
->once()
203+
->andReturn($this->response_mock);
204+
205+
$this->listener->handle($this->event_mock);
206+
}
207+
208+
/**
209+
* @test
210+
*/
211+
public function on_user_logout_if_discourse_response_code_is_not_200_log_a_notice_with_the_status_code()
212+
{
213+
$this->user_mock->id = 1;
214+
$this->event_mock->user = $this->user_mock;
215+
216+
$this->logger_mock->shouldReceive('notice')->once();
217+
218+
$configs = [
219+
'base_uri' => 'http://discourse.example.com',
220+
'headers' => [
221+
'Api-Key' => 'testkey',
222+
'Api-Username' => 'testuser',
223+
],
224+
];
225+
226+
$this->config_mock->shouldReceive('get')
227+
->with('services.discourse.url')
228+
->once()
229+
->andReturn($configs['base_uri']);
230+
231+
$this->config_mock->shouldReceive('get')
232+
->with('services.discourse.api.key')
233+
->once()
234+
->andReturn($configs['headers']['Api-Key']);
235+
236+
$this->config_mock->shouldReceive('get')
237+
->with('services.discourse.api.user')
238+
->once()
239+
->andReturn($configs['headers']['Api-Username']);
197240

198241
$this->response_mock->shouldReceive('getStatusCode')
199242
->andReturn(500);
@@ -202,13 +245,23 @@ public function if_discourse_response_code_is_not_200_log_a_notice_with_the_stat
202245
->once()
203246
->andReturn('Server error');
204247

248+
$good_response = Mockery::mock(Response::class);
249+
250+
$good_response->shouldReceive('getStatusCode')
251+
->once()
252+
->andReturn(200);
253+
254+
$good_response->shouldReceive('getBody')
255+
->once()
256+
->andReturn(json_encode(['user' => $this->user_mock]));
257+
205258
$this->guzzle_mock->shouldReceive('get')
206259
->with('users/by-external/1.json', $configs)
207260
->once()
208-
->andReturn($this->response_mock);
261+
->andReturn($good_response);
209262

210263
$this->guzzle_mock->shouldReceive('post')
211-
->with('admin/users/1/log_out')
264+
->with('admin/users/1/log_out', $configs)
212265
->andReturn($this->response_mock);
213266

214267
$this->listener->handle($this->event_mock);

0 commit comments

Comments
 (0)