Skip to content

Commit ab85cc2

Browse files
committed
Adding Tests, Pint
1 parent edd1dea commit ab85cc2

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

src/Events/NewMicrosoft365SignInEvent.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@ class NewMicrosoft365SignInEvent
1212
use InteractsWithSockets;
1313
use SerializesModels;
1414

15-
public function __construct(public array $token)
16-
{
17-
18-
}
15+
public function __construct(public array $token) {}
1916
}

src/MsGraph.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ public static function setUserModel(string $model): static
8888
return new static();
8989
}
9090

91-
92-
public static function login($user) {
91+
public static function login($user)
92+
{
9393
self::$user = $user;
9494
}
9595

96-
public static function getUser() {
96+
public static function getUser()
97+
{
9798
return self::$user;
9899
}
99100

@@ -335,7 +336,7 @@ protected function isJson(string $string): bool
335336

336337
protected function getUserId(?string $id = null): ?string
337338
{
338-
if($this->getUser() !== null) {
339+
if ($this->getUser() !== null) {
339340
$id = $this->getUser()->id;
340341
}
341342

tests/MsGraphTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
use Dcblogdev\MsGraph\Facades\MsGraph as MsGraphFacade;
44
use Dcblogdev\MsGraph\Models\MsGraphToken;
55
use Dcblogdev\MsGraph\MsGraph;
6+
use Illuminate\Foundation\Auth\User as Authenticatable;
67
use Illuminate\Http\RedirectResponse;
78
use Illuminate\Routing\Redirector;
9+
use Illuminate\Support\Facades\Auth;
810
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
911
use League\OAuth2\Client\Provider\GenericProvider;
1012
use League\OAuth2\Client\Token\AccessToken;
@@ -171,3 +173,79 @@
171173
'refresh_token' => $refreshToken,
172174
]);
173175
});
176+
177+
class TestUser extends Authenticatable
178+
{
179+
protected $fillable = ['id', 'email'];
180+
}
181+
182+
test('can login and set user', function () {
183+
$user = new TestUser(['id' => 1, 'email' => '[email protected]']);
184+
185+
MsGraphFacade::login($user);
186+
187+
expect(MsGraphFacade::getUser())->toBe($user);
188+
});
189+
190+
test('getUser returns null when no user is logged in', function () {
191+
MsGraphFacade::login(null); // Reset the user
192+
expect(MsGraphFacade::getUser())->toBeNull();
193+
});
194+
195+
test('getAccessToken uses logged in user when available', function () {
196+
$user = new TestUser(['id' => 1, 'email' => '[email protected]']);
197+
198+
MsGraphFacade::login($user);
199+
200+
MsGraphToken::create([
201+
'user_id' => $user->id,
202+
'access_token' => 'test_token',
203+
'refresh_token' => 'refresh_token',
204+
'expires' => strtotime('+1 day'),
205+
]);
206+
207+
$token = MsGraphFacade::getAccessToken();
208+
209+
expect($token)->toBe('test_token');
210+
});
211+
212+
test('getUserId returns logged in user id when available', function () {
213+
$user = new TestUser(['id' => 1, 'email' => '[email protected]']);
214+
215+
MsGraphFacade::login($user);
216+
217+
$reflection = new ReflectionClass(MsGraphFacade::getFacadeRoot());
218+
$method = $reflection->getMethod('getUserId');
219+
$method->setAccessible(true);
220+
221+
$userId = $method->invoke(MsGraphFacade::getFacadeRoot());
222+
223+
expect($userId)->toBe('1');
224+
});
225+
226+
test('getUserId falls back to auth id when no user is logged in', function () {
227+
MsGraphFacade::login(null); // Reset the user
228+
229+
$user = new TestUser(['id' => 2, 'email' => '[email protected]']);
230+
Auth::shouldReceive('id')->andReturn(2);
231+
232+
$reflection = new ReflectionClass(MsGraphFacade::getFacadeRoot());
233+
$method = $reflection->getMethod('getUserId');
234+
$method->setAccessible(true);
235+
236+
$userId = $method->invoke(MsGraphFacade::getFacadeRoot());
237+
238+
expect($userId)->toBe('2');
239+
});
240+
241+
test('getAccessToken redirects when user is not connected and redirectWhenNotConnected is true', function () {
242+
config(['msgraph.redirectUri' => 'http://example.com/redirect']);
243+
244+
MsGraphFacade::login(null); // Reset the user
245+
Auth::shouldReceive('id')->andReturn(null);
246+
247+
$response = MsGraphFacade::getAccessToken(null, true);
248+
249+
expect($response)->toBeInstanceOf(RedirectResponse::class)
250+
->and($response->getTargetUrl())->toBe('http://example.com/redirect');
251+
});

0 commit comments

Comments
 (0)