Skip to content

Commit 8396b7c

Browse files
authored
Merge pull request #98 from dcblogdev/add-tests
Add tests - WIP
2 parents c1a3c54 + 87aed52 commit 8396b7c

9 files changed

+322
-6
lines changed

src/MsGraph.php

+4
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ public function getTokenData(?string $id = null): ?MsGraphToken
219219
{
220220
$id = $this->getUserId($id);
221221

222+
if ($id === null) {
223+
return null;
224+
}
225+
222226
return MsGraphToken::where('user_id', $id)->where('refresh_token', '<>', '')->first();
223227
}
224228

src/Resources/Contacts.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ protected function getParams(array $params, int $perPage): string
4949
{
5050
$skip = $params['skip'] ?? 0;
5151
$page = request('p', $skip);
52-
if ($page > 0) {
53-
$page--;
54-
}
5552

5653
if ($params == []) {
5754
$params = http_build_query([
@@ -62,15 +59,15 @@ protected function getParams(array $params, int $perPage): string
6259
]);
6360
} else {
6461
// ensure $top, $skip and $count are part of params
65-
if (! in_array('$top', $params)) {
62+
if (! array_key_exists('$top', $params)) {
6663
$params['$top'] = $perPage;
6764
}
6865

69-
if (! in_array('$skip', $params)) {
66+
if (! array_key_exists('$skip', $params)) {
7067
$params['$skip'] = $page;
7168
}
7269

73-
if (! in_array('$count', $params)) {
70+
if (! array_key_exists('$count', $params)) {
7471
$params['$count'] = 'true';
7572
}
7673

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraphAdmin;
4+
5+
test('refreshes admin token when MsGraphAdmin is connected', function () {
6+
// Mock MsGraphAdmin::isConnected() to return true
7+
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(true);
8+
MsGraphAdmin::shouldReceive('getAccessToken')->once()->with(true);
9+
10+
$this->artisan('msgraphadmin:keep-alive')
11+
->expectsOutput('connected')
12+
->assertExitCode(0);
13+
});
14+
15+
test('does nothing when MsGraphAdmin is not connected', function () {
16+
// Mock MsGraphAdmin::isConnected() to return false
17+
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(false);
18+
19+
$this->artisan('msgraphadmin:keep-alive')
20+
->doesntExpectOutput('connected')
21+
->assertExitCode(0);
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraph;
4+
5+
test('refreshes token when MsGraph is connected', function () {
6+
// Mock MsGraph::isConnected() to return true
7+
MsGraph::shouldReceive('isConnected')->once()->andReturn(true);
8+
MsGraph::shouldReceive('getAccessToken')->once()->with(null, false);
9+
10+
$this->artisan('msgraph:keep-alive')
11+
->expectsOutput('connected')
12+
->assertExitCode(0);
13+
});
14+
15+
test('does nothing when MsGraph is not connected', function () {
16+
// Mock MsGraph::isConnected() to return false
17+
MsGraph::shouldReceive('isConnected')->once()->andReturn(false);
18+
19+
$this->artisan('msgraph:keep-alive')
20+
->doesntExpectOutput('connected')
21+
->assertExitCode(0);
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Events\NewMicrosoft365SignInEvent;
4+
use Illuminate\Support\Facades\Event;
5+
6+
test('NewMicrosoft365SignInEvent is dispatched with token data', function () {
7+
Event::fake();
8+
9+
$tokenData = [
10+
'accessToken' => 'fake_access_token',
11+
'refreshToken' => 'fake_refresh_token',
12+
'expires' => now()->addHour()->timestamp,
13+
'info' => [
14+
'mail' => 'test@example.com',
15+
'displayName' => 'Test User',
16+
],
17+
];
18+
19+
// Dispatch the event
20+
event(new NewMicrosoft365SignInEvent($tokenData));
21+
22+
// Assert event was dispatched
23+
Event::assertDispatched(NewMicrosoft365SignInEvent::class, function ($event) use ($tokenData) {
24+
return $event->token === $tokenData;
25+
});
26+
});
File renamed without changes.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraphAdmin;
4+
use Dcblogdev\MsGraph\MsGraphAdminAuthenticated;
5+
use Illuminate\Support\Facades\Route;
6+
7+
beforeEach(function () {
8+
Route::middleware(MsGraphAdminAuthenticated::class)->get('/test-route', function () {
9+
return response()->json(['message' => 'Access granted']);
10+
});
11+
});
12+
13+
test('redirects to MsGraphAdmin::connect() when not connected', function () {
14+
// Mock MsGraphAdmin::isConnected() to return false
15+
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(false);
16+
MsGraphAdmin::shouldReceive('connect')->once()->andReturn(redirect('https://login.microsoftonline.com'));
17+
18+
$response = $this->get('/test-route');
19+
20+
$response->assertRedirect('https://login.microsoftonline.com');
21+
});
22+
23+
test('allows request when MsGraphAdmin is connected', function () {
24+
// Mock MsGraphAdmin::isConnected() to return true
25+
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(true);
26+
27+
$response = $this->get('/test-route');
28+
29+
$response->assertOk()
30+
->assertJson(['message' => 'Access granted']);
31+
});

tests/MsGraphAuthenticatedTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraph;
4+
use Dcblogdev\MsGraph\MsGraphAuthenticated;
5+
use Illuminate\Support\Facades\Route;
6+
7+
beforeEach(function () {
8+
Route::middleware(MsGraphAuthenticated::class)->get('/test-route', function () {
9+
return response()->json(['message' => 'Access granted']);
10+
});
11+
});
12+
13+
test('redirects to MsGraph::connect() when not connected', function () {
14+
// Mock MsGraph::isConnected() to return false
15+
MsGraph::shouldReceive('isConnected')->once()->andReturn(false);
16+
MsGraph::shouldReceive('connect')->once()->andReturn(redirect('https://login.microsoftonline.com'));
17+
18+
$response = $this->get('/test-route');
19+
20+
$response->assertRedirect('https://login.microsoftonline.com');
21+
});
22+
23+
test('allows request when MsGraph is connected', function () {
24+
// Mock MsGraph::isConnected() to return true
25+
MsGraph::shouldReceive('isConnected')->once()->andReturn(true);
26+
27+
$response = $this->get('/test-route');
28+
29+
$response->assertOk()
30+
->assertJson(['message' => 'Access granted']);
31+
});

tests/Resources/ContactsTest.php

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraph;
4+
use Dcblogdev\MsGraph\Resources\Contacts;
5+
6+
beforeEach(function () {
7+
MsGraph::shouldReceive('isConnected')->andReturn(true);
8+
$this->contacts = new Contacts;
9+
});
10+
11+
test('get contacts with params', function () {
12+
13+
$messageQueryParams = [
14+
'$orderby' => 'displayName',
15+
'$skip' => 0,
16+
'$top' => 5,
17+
'$count' => 'true',
18+
];
19+
20+
MsGraph::shouldReceive('get')
21+
->with('me/contacts?'.http_build_query($messageQueryParams))
22+
->andReturn([
23+
'@odata.count' => 10,
24+
'value' => [
25+
['id' => '1', 'displayName' => 'John Doe'],
26+
['id' => '2', 'displayName' => 'Jane Doe'],
27+
],
28+
]);
29+
30+
$response = (new Contacts)->get($messageQueryParams);
31+
32+
expect($response)->toHaveKeys(['contacts', 'total', 'links', 'links_array'])
33+
->and($response['contacts']['value'])->toBeArray()
34+
->and($response['total'])->toBe(10);
35+
});
36+
37+
test('getParams returns default values when no params provided', function () {
38+
39+
$contacts = new Contacts;
40+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
41+
$response = $reflection->invoke($contacts, [], 25);
42+
43+
parse_str($response, $parsedParams);
44+
45+
expect($parsedParams)->toMatchArray([
46+
'$orderby' => 'displayName',
47+
'$top' => '25',
48+
'$skip' => '0',
49+
'$count' => 'true',
50+
]);
51+
});
52+
53+
test('getParams includes custom top parameter', function () {
54+
$contacts = new Contacts;
55+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
56+
$response = $reflection->invoke($contacts, ['$top' => 10], 25);
57+
58+
parse_str($response, $parsedParams);
59+
60+
expect($parsedParams)->toMatchArray([
61+
'$top' => '10',
62+
'$skip' => '0',
63+
'$count' => 'true',
64+
]);
65+
});
66+
67+
test('getParams includes custom skip parameter', function () {
68+
$contacts = new Contacts;
69+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
70+
$response = $reflection->invoke($contacts, ['$skip' => 15], 25);
71+
72+
parse_str($response, $parsedParams);
73+
74+
expect($parsedParams)->toMatchArray([
75+
'$top' => '25',
76+
'$skip' => '15',
77+
'$count' => 'true',
78+
]);
79+
});
80+
81+
test('getParams forces count to be true when missing', function () {
82+
$contacts = new Contacts;
83+
$reflection = new ReflectionMethod(Contacts::class, 'getParams');
84+
$response = $reflection->invoke($contacts, ['$top' => 10, '$skip' => 5], 25);
85+
86+
parse_str($response, $parsedParams);
87+
88+
expect($parsedParams)->toMatchArray([
89+
'$top' => '10',
90+
'$skip' => '5',
91+
'$count' => 'true',
92+
]);
93+
});
94+
95+
test('find method retrieves a specific contact', function () {
96+
97+
MsGraph::shouldReceive('get')
98+
->with('me/contacts/1')
99+
->andReturn([
100+
'id' => '1',
101+
'displayName' => 'John Doe',
102+
'email' => 'johndoe@example.com',
103+
]);
104+
105+
$response = (new Contacts)->find('1');
106+
107+
expect($response)
108+
->toHaveKeys(['id', 'displayName', 'email'])
109+
->and($response['id'])->toBe('1')
110+
->and($response['displayName'])->toBe('John Doe')
111+
->and($response['email'])->toBe('johndoe@example.com');
112+
});
113+
114+
test('can create contact', function () {
115+
116+
$data = [
117+
'displayName' => 'John Doe',
118+
'givenName' => 'John Doe',
119+
'emailAddresses' => [
120+
[
121+
'address' => 'john@doe.com',
122+
'name' => 'John Doe',
123+
],
124+
],
125+
];
126+
127+
MsGraph::shouldReceive('post')
128+
->with('me/contacts', $data)
129+
->andReturn([
130+
'id' => '1',
131+
'displayName' => 'John Doe',
132+
'email' => 'johndoe@example.com',
133+
]);
134+
135+
$response = (new Contacts)->store($data);
136+
137+
expect($response)
138+
->toHaveKeys(['id', 'displayName', 'email'])
139+
->and($response['id'])->toBe('1')
140+
->and($response['displayName'])->toBe('John Doe')
141+
->and($response['email'])->toBe('johndoe@example.com');
142+
});
143+
144+
test('can update contact', function () {
145+
146+
$data = [
147+
'displayName' => 'John Doe',
148+
'givenName' => 'John Doe',
149+
'emailAddresses' => [
150+
[
151+
'address' => 'john@doe.com',
152+
'name' => 'John Doe',
153+
],
154+
],
155+
];
156+
157+
MsGraph::shouldReceive('patch')
158+
->with('me/contacts/1', $data)
159+
->andReturn([
160+
'id' => '1',
161+
'displayName' => 'John Doe',
162+
'email' => 'johndoe@example.com',
163+
]);
164+
165+
$response = (new Contacts)->update(1, $data);
166+
167+
expect($response)
168+
->toHaveKeys(['id', 'displayName', 'email'])
169+
->and($response['id'])->toBe('1')
170+
->and($response['displayName'])->toBe('John Doe')
171+
->and($response['email'])->toBe('johndoe@example.com');
172+
});
173+
174+
test('can delete contact', function () {
175+
176+
MsGraph::shouldReceive('delete')
177+
->with('me/contacts/1')
178+
->andReturn('');
179+
180+
$response = (new Contacts)->delete(1);
181+
182+
expect($response)->toBe('');
183+
});

0 commit comments

Comments
 (0)