Skip to content

Commit 81c4071

Browse files
authored
Merge pull request #97 from dcblogdev/increase-test-coverage
Increase test coverage
2 parents c1a1a71 + 01c2224 commit 81c4071

18 files changed

+127
-61
lines changed

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@
6464
}
6565
},
6666
"scripts": {
67-
"pest": "vendor/bin/pest --parallel",
68-
"pest-cov": "vendor/bin/pest --coverage",
69-
"pest-type": "vendor/bin/pest --type-coverage",
70-
"pint": "vendor/bin/pint"
67+
"lint": "vendor/bin/pint",
68+
"test": "vendor/bin/pest --parallel",
69+
"type-coverage": "vendor/bin/pest --coverage",
70+
"test-coverage": "vendor/bin/pest --type-coverage",
71+
"check": [
72+
"@lint",
73+
"@test-coverage",
74+
"@type-coverage"
75+
]
7176
}
7277
}

src/AdminResources/CalendarEvents.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class CalendarEvents extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -58,6 +58,11 @@ public function get(string $calendarId, array $params = []): array
5858
}
5959

6060
$events = MsGraphAdmin::get("users/$this->userId/calendars/$calendarId/events?$params");
61+
62+
if (isset($events->error)) {
63+
throw new Exception("Graph API Error, code: {$events->error->code}, Message: {$events->error->message}");
64+
}
65+
6166
$data = MsGraphAdmin::getPagination($events, $top, $skip);
6267

6368
return [

src/AdminResources/Calendars.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Calendars extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -59,6 +59,10 @@ public function get(array $params = []): array
5959

6060
$calendars = MsGraphAdmin::get("users/$this->userId/calendars?$params");
6161

62+
if (isset($calendars->error)) {
63+
throw new Exception("Graph API Error, code: {$calendars->error->code}, Message: {$calendars->error->message}");
64+
}
65+
6266
$data = MsGraphAdmin::getPagination($calendars, $top, $skip);
6367

6468
return [

src/AdminResources/Contacts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Contacts extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -59,6 +59,10 @@ public function get(array $params = []): array
5959

6060
$contacts = MsGraphAdmin::get('users/'.$this->userId.'/contacts?'.$params);
6161

62+
if (isset($contacts->error)) {
63+
throw new Exception("Graph API Error, code: {$contacts->error->code}, Message: {$contacts->error->message}");
64+
}
65+
6266
$data = MsGraphAdmin::getPagination($contacts, $top, $skip);
6367

6468
return [

src/AdminResources/Emails.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Emails extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
private string $subject = '';
1717

@@ -129,9 +129,13 @@ public function get(array $params = []): array
129129
$params = http_build_query($params);
130130
}
131131

132-
//get messages from folderId
132+
// get messages from folderId
133133
$emails = MsGraphAdmin::get('users/'.$this->userId.'/messages?'.$params);
134134

135+
if (isset($emails->error)) {
136+
throw new Exception("Graph API Error, code: {$emails->error->code}, Message: {$emails->error->message}");
137+
}
138+
135139
$data = MsGraphAdmin::getPagination($emails, $top, $skip);
136140

137141
return [
@@ -163,21 +167,21 @@ public function findInlineAttachments(array $email): array
163167
{
164168
$attachments = self::findAttachments($email['id']);
165169

166-
//replace every case of <img='cid:' with the base64 image
170+
// replace every case of <img='cid:' with the base64 image
167171
$email['body']['content'] = preg_replace_callback(
168172
'~cid.*?"~',
169173
function (array $m) use ($attachments) {
170-
//remove the last quote
174+
// remove the last quote
171175
$parts = explode('"', $m[0]);
172176

173-
//remove cid:
177+
// remove cid:
174178
$contentId = str_replace('cid:', '', $parts[0]);
175179

176-
//loop over the attachments
180+
// loop over the attachments
177181
foreach ($attachments['value'] as $file) {
178-
//if there is a match
182+
// if there is a match
179183
if ($file['contentId'] == $contentId) {
180-
//return a base64 image with a quote
184+
// return a base64 image with a quote
181185
return 'data:'.$file['contentType'].';base64,'.$file['contentBytes'].'"';
182186
}
183187
}
@@ -217,7 +221,7 @@ public function send(): void
217221
/**
218222
* @throws Exception
219223
*/
220-
public function reply()
224+
public function reply(): MsGraphAdmin
221225
{
222226
if (strlen($this->userId) === 0) {
223227
throw new Exception('userId is required.');
@@ -237,7 +241,7 @@ public function reply()
237241
/**
238242
* @throws Exception
239243
*/
240-
public function forward()
244+
public function forward(): MsGraphAdmin
241245
{
242246
if (strlen($this->userId) === 0) {
243247
throw new Exception('userId is required.');
@@ -257,7 +261,7 @@ public function forward()
257261
/**
258262
* @throws Exception
259263
*/
260-
public function delete(string $id)
264+
public function delete(string $id): MsGraphAdmin
261265
{
262266
if ($this->userId == null) {
263267
throw new Exception('userId is required.');
@@ -297,12 +301,12 @@ protected function prepareEmail(): array
297301
}
298302
}
299303

300-
$attachmentarray = [];
304+
$attachmentArray = [];
301305
if ($attachments != null) {
302306
foreach ($attachments as $file) {
303307
$path = pathinfo($file);
304308

305-
$attachmentarray[] = [
309+
$attachmentArray[] = [
306310
'@odata.type' => '#microsoft.graph.fileAttachment',
307311
'name' => $path['basename'],
308312
'contentType' => mime_content_type($file),
@@ -330,8 +334,8 @@ protected function prepareEmail(): array
330334
if ($bccArray != null) {
331335
$envelope['message']['bccRecipients'] = $bccArray;
332336
}
333-
if ($attachmentarray != null) {
334-
$envelope['message']['attachments'] = $attachmentarray;
337+
if ($attachmentArray != null) {
338+
$envelope['message']['attachments'] = $attachmentArray;
335339
}
336340
if ($comment != null) {
337341
$envelope['comment'] = $comment;

src/AdminResources/Events.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Events extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -59,6 +59,10 @@ public function get(array $params = []): array
5959

6060
$events = MsGraphAdmin::get("users/$this->userId/events?$params");
6161

62+
if (isset($events->error)) {
63+
throw new Exception("Graph API Error, code: {$events->error->code}, Message: {$events->error->message}");
64+
}
65+
6266
$data = MsGraphAdmin::getPagination($events, $top, $skip);
6367

6468
return [

src/Events/NewMicrosoft365SignInEvent.php

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

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

src/Listeners/NewMicrosoft365SignInListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function handle(object $event): void
1818
'password' => '',
1919
]);
2020

21-
(new MsGraph())->storeToken(
21+
(new MsGraph)->storeToken(
2222
$event->token['accessToken'],
2323
$event->token['refreshToken'],
2424
$event->token['expires'],

src/MsGraph.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Illuminate\Support\Facades\Http;
2424
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
2525
use League\OAuth2\Client\Provider\GenericProvider;
26+
use Microsoft\Graph\Model\User;
27+
use TestUser;
2628

2729
class MsGraph
2830
{
@@ -56,7 +58,7 @@ public function tasks(): Tasks
5658
return new Tasks;
5759
}
5860

59-
protected static $user;
61+
protected static User|TestUser|null $user = null;
6062

6163
protected static string $baseUrl = 'https://graph.microsoft.com/v1.0/';
6264

@@ -88,12 +90,12 @@ public static function setUserModel(string $model): static
8890
return new static;
8991
}
9092

91-
public static function login($user)
93+
public static function login(User|TestUser|null $user): void
9294
{
9395
self::$user = $user;
9496
}
9597

96-
public static function getUser()
98+
public static function getUser(): User|TestUser|null
9799
{
98100
return self::$user;
99101
}
@@ -281,7 +283,7 @@ public function __call(string $function, array $args)
281283
if (in_array($function, $options)) {
282284
return self::guzzle($function, $path, $data, $headers, $id);
283285
} else {
284-
//request verb is not in the $options array
286+
// request verb is not in the $options array
285287
throw new Exception($function.' is not a valid HTTP Verb');
286288
}
287289
}

src/MsGraphAdmin.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,32 @@ class MsGraphAdmin
2222
{
2323
public function calendarEvents(): CalendarEvents
2424
{
25-
return new CalendarEvents();
25+
return new CalendarEvents;
2626
}
2727

2828
public function calendars(): Calendars
2929
{
30-
return new Calendars();
30+
return new Calendars;
3131
}
3232

3333
public function contacts(): Contacts
3434
{
35-
return new Contacts();
35+
return new Contacts;
3636
}
3737

3838
public function emails(): Emails
3939
{
40-
return new Emails();
40+
return new Emails;
4141
}
4242

4343
public function events(): Events
4444
{
45-
return new Events();
45+
return new Events;
4646
}
4747

4848
public function files(): Files
4949
{
50-
return new Files();
50+
return new Files;
5151
}
5252

5353
protected static string $baseUrl = 'https://graph.microsoft.com/v1.0/';
@@ -136,7 +136,7 @@ public function getTokenData(): ?MsGraphToken
136136

137137
protected function storeToken(string $access_token, string $refresh_token, string $expires): MsGraphToken
138138
{
139-
//Create or update a new record for admin token
139+
// Create or update a new record for admin token
140140
return MsGraphToken::updateOrCreate(['user_id' => null], [
141141
'email' => 'application_token', // Placeholder name
142142
'access_token' => $access_token,
@@ -157,7 +157,7 @@ public function __call(string $function, array $args): mixed
157157
if (in_array($function, $options)) {
158158
return self::guzzle($function, $path, $data);
159159
} else {
160-
//request verb is not in the $options array
160+
// request verb is not in the $options array
161161
throw new Exception($function.' is not a valid HTTP Verb');
162162
}
163163
}

src/MsGraphServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function registerFilesystem(): void
7575
$clientId = config('msgraph.clientId');
7676
$clientSecret = config('msgraph.clientSecret');
7777

78-
$guzzle = new Client();
78+
$guzzle = new Client;
7979
$response = $guzzle->post("https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token",
8080
[
8181
'headers' => [

src/Resources/Contacts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function getParams(array $params, int $perPage): string
6161
'$count' => 'true',
6262
]);
6363
} else {
64-
//ensure $top, $skip and $count are part of params
64+
// ensure $top, $skip and $count are part of params
6565
if (! in_array('$top', $params)) {
6666
$params['$top'] = $perPage;
6767
}

0 commit comments

Comments
 (0)