diff --git a/composer.json b/composer.json index e13d295..e8f9199 100755 --- a/composer.json +++ b/composer.json @@ -64,9 +64,14 @@ } }, "scripts": { - "pest": "vendor/bin/pest --parallel", - "pest-cov": "vendor/bin/pest --coverage", - "pest-type": "vendor/bin/pest --type-coverage", - "pint": "vendor/bin/pint" + "lint": "vendor/bin/pint", + "test": "vendor/bin/pest --parallel", + "type-coverage": "vendor/bin/pest --coverage", + "test-coverage": "vendor/bin/pest --type-coverage", + "check": [ + "@lint", + "@test-coverage", + "@type-coverage" + ] } } diff --git a/src/AdminResources/CalendarEvents.php b/src/AdminResources/CalendarEvents.php index 11b7017..5e79f44 100644 --- a/src/AdminResources/CalendarEvents.php +++ b/src/AdminResources/CalendarEvents.php @@ -9,9 +9,9 @@ class CalendarEvents extends MsGraphAdmin { private string $userId = ''; - private string $top = ''; + private string $top = '100'; - private string $skip = ''; + private string $skip = '0'; public function userid(string $userId): static { @@ -58,6 +58,11 @@ public function get(string $calendarId, array $params = []): array } $events = MsGraphAdmin::get("users/$this->userId/calendars/$calendarId/events?$params"); + + if (isset($events->error)) { + throw new Exception("Graph API Error, code: {$events->error->code}, Message: {$events->error->message}"); + } + $data = MsGraphAdmin::getPagination($events, $top, $skip); return [ diff --git a/src/AdminResources/Calendars.php b/src/AdminResources/Calendars.php index abc1cb2..4f183aa 100644 --- a/src/AdminResources/Calendars.php +++ b/src/AdminResources/Calendars.php @@ -9,9 +9,9 @@ class Calendars extends MsGraphAdmin { private string $userId = ''; - private string $top = ''; + private string $top = '100'; - private string $skip = ''; + private string $skip = '0'; public function userid(string $userId): static { @@ -59,6 +59,10 @@ public function get(array $params = []): array $calendars = MsGraphAdmin::get("users/$this->userId/calendars?$params"); + if (isset($calendars->error)) { + throw new Exception("Graph API Error, code: {$calendars->error->code}, Message: {$calendars->error->message}"); + } + $data = MsGraphAdmin::getPagination($calendars, $top, $skip); return [ diff --git a/src/AdminResources/Contacts.php b/src/AdminResources/Contacts.php index 696f69f..b918202 100644 --- a/src/AdminResources/Contacts.php +++ b/src/AdminResources/Contacts.php @@ -9,9 +9,9 @@ class Contacts extends MsGraphAdmin { private string $userId = ''; - private string $top = ''; + private string $top = '100'; - private string $skip = ''; + private string $skip = '0'; public function userid(string $userId): static { @@ -59,6 +59,10 @@ public function get(array $params = []): array $contacts = MsGraphAdmin::get('users/'.$this->userId.'/contacts?'.$params); + if (isset($contacts->error)) { + throw new Exception("Graph API Error, code: {$contacts->error->code}, Message: {$contacts->error->message}"); + } + $data = MsGraphAdmin::getPagination($contacts, $top, $skip); return [ diff --git a/src/AdminResources/Emails.php b/src/AdminResources/Emails.php index 52390e3..c984f97 100644 --- a/src/AdminResources/Emails.php +++ b/src/AdminResources/Emails.php @@ -9,9 +9,9 @@ class Emails extends MsGraphAdmin { private string $userId = ''; - private string $top = ''; + private string $top = '100'; - private string $skip = ''; + private string $skip = '0'; private string $subject = ''; @@ -129,9 +129,13 @@ public function get(array $params = []): array $params = http_build_query($params); } - //get messages from folderId + // get messages from folderId $emails = MsGraphAdmin::get('users/'.$this->userId.'/messages?'.$params); + if (isset($emails->error)) { + throw new Exception("Graph API Error, code: {$emails->error->code}, Message: {$emails->error->message}"); + } + $data = MsGraphAdmin::getPagination($emails, $top, $skip); return [ @@ -163,21 +167,21 @@ public function findInlineAttachments(array $email): array { $attachments = self::findAttachments($email['id']); - //replace every case of userId) === 0) { throw new Exception('userId is required.'); @@ -237,7 +241,7 @@ public function reply() /** * @throws Exception */ - public function forward() + public function forward(): MsGraphAdmin { if (strlen($this->userId) === 0) { throw new Exception('userId is required.'); @@ -257,7 +261,7 @@ public function forward() /** * @throws Exception */ - public function delete(string $id) + public function delete(string $id): MsGraphAdmin { if ($this->userId == null) { throw new Exception('userId is required.'); @@ -297,12 +301,12 @@ protected function prepareEmail(): array } } - $attachmentarray = []; + $attachmentArray = []; if ($attachments != null) { foreach ($attachments as $file) { $path = pathinfo($file); - $attachmentarray[] = [ + $attachmentArray[] = [ '@odata.type' => '#microsoft.graph.fileAttachment', 'name' => $path['basename'], 'contentType' => mime_content_type($file), @@ -330,8 +334,8 @@ protected function prepareEmail(): array if ($bccArray != null) { $envelope['message']['bccRecipients'] = $bccArray; } - if ($attachmentarray != null) { - $envelope['message']['attachments'] = $attachmentarray; + if ($attachmentArray != null) { + $envelope['message']['attachments'] = $attachmentArray; } if ($comment != null) { $envelope['comment'] = $comment; diff --git a/src/AdminResources/Events.php b/src/AdminResources/Events.php index 69e19b9..9c5de29 100644 --- a/src/AdminResources/Events.php +++ b/src/AdminResources/Events.php @@ -9,9 +9,9 @@ class Events extends MsGraphAdmin { private string $userId = ''; - private string $top = ''; + private string $top = '100'; - private string $skip = ''; + private string $skip = '0'; public function userid(string $userId): static { @@ -59,6 +59,10 @@ public function get(array $params = []): array $events = MsGraphAdmin::get("users/$this->userId/events?$params"); + if (isset($events->error)) { + throw new Exception("Graph API Error, code: {$events->error->code}, Message: {$events->error->message}"); + } + $data = MsGraphAdmin::getPagination($events, $top, $skip); return [ diff --git a/src/Events/NewMicrosoft365SignInEvent.php b/src/Events/NewMicrosoft365SignInEvent.php index b6bffab..270b910 100644 --- a/src/Events/NewMicrosoft365SignInEvent.php +++ b/src/Events/NewMicrosoft365SignInEvent.php @@ -12,7 +12,5 @@ class NewMicrosoft365SignInEvent use InteractsWithSockets; use SerializesModels; - public function __construct(public array $token) - { - } + public function __construct(public array $token) {} } diff --git a/src/Listeners/NewMicrosoft365SignInListener.php b/src/Listeners/NewMicrosoft365SignInListener.php index 5c478d0..0e751fc 100644 --- a/src/Listeners/NewMicrosoft365SignInListener.php +++ b/src/Listeners/NewMicrosoft365SignInListener.php @@ -18,7 +18,7 @@ public function handle(object $event): void 'password' => '', ]); - (new MsGraph())->storeToken( + (new MsGraph)->storeToken( $event->token['accessToken'], $event->token['refreshToken'], $event->token['expires'], diff --git a/src/MsGraph.php b/src/MsGraph.php index 2bf6f4f..e21e139 100755 --- a/src/MsGraph.php +++ b/src/MsGraph.php @@ -23,6 +23,8 @@ use Illuminate\Support\Facades\Http; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Provider\GenericProvider; +use Microsoft\Graph\Model\User; +use TestUser; class MsGraph { @@ -56,7 +58,7 @@ public function tasks(): Tasks return new Tasks; } - protected static $user; + protected static User|TestUser|null $user = null; protected static string $baseUrl = 'https://graph.microsoft.com/v1.0/'; @@ -88,12 +90,12 @@ public static function setUserModel(string $model): static return new static; } - public static function login($user) + public static function login(User|TestUser|null $user): void { self::$user = $user; } - public static function getUser() + public static function getUser(): User|TestUser|null { return self::$user; } @@ -281,7 +283,7 @@ public function __call(string $function, array $args) if (in_array($function, $options)) { return self::guzzle($function, $path, $data, $headers, $id); } else { - //request verb is not in the $options array + // request verb is not in the $options array throw new Exception($function.' is not a valid HTTP Verb'); } } diff --git a/src/MsGraphAdmin.php b/src/MsGraphAdmin.php index f13220c..ddb2395 100644 --- a/src/MsGraphAdmin.php +++ b/src/MsGraphAdmin.php @@ -22,32 +22,32 @@ class MsGraphAdmin { public function calendarEvents(): CalendarEvents { - return new CalendarEvents(); + return new CalendarEvents; } public function calendars(): Calendars { - return new Calendars(); + return new Calendars; } public function contacts(): Contacts { - return new Contacts(); + return new Contacts; } public function emails(): Emails { - return new Emails(); + return new Emails; } public function events(): Events { - return new Events(); + return new Events; } public function files(): Files { - return new Files(); + return new Files; } protected static string $baseUrl = 'https://graph.microsoft.com/v1.0/'; @@ -136,7 +136,7 @@ public function getTokenData(): ?MsGraphToken protected function storeToken(string $access_token, string $refresh_token, string $expires): MsGraphToken { - //Create or update a new record for admin token + // Create or update a new record for admin token return MsGraphToken::updateOrCreate(['user_id' => null], [ 'email' => 'application_token', // Placeholder name 'access_token' => $access_token, @@ -157,7 +157,7 @@ public function __call(string $function, array $args): mixed if (in_array($function, $options)) { return self::guzzle($function, $path, $data); } else { - //request verb is not in the $options array + // request verb is not in the $options array throw new Exception($function.' is not a valid HTTP Verb'); } } diff --git a/src/MsGraphServiceProvider.php b/src/MsGraphServiceProvider.php index 7a94fc6..477dfaa 100755 --- a/src/MsGraphServiceProvider.php +++ b/src/MsGraphServiceProvider.php @@ -75,7 +75,7 @@ public function registerFilesystem(): void $clientId = config('msgraph.clientId'); $clientSecret = config('msgraph.clientSecret'); - $guzzle = new Client(); + $guzzle = new Client; $response = $guzzle->post("https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token", [ 'headers' => [ diff --git a/src/Resources/Contacts.php b/src/Resources/Contacts.php index 09ca7dd..30dc852 100644 --- a/src/Resources/Contacts.php +++ b/src/Resources/Contacts.php @@ -61,7 +61,7 @@ protected function getParams(array $params, int $perPage): string '$count' => 'true', ]); } else { - //ensure $top, $skip and $count are part of params + // ensure $top, $skip and $count are part of params if (! in_array('$top', $params)) { $params['$top'] = $perPage; } diff --git a/src/Resources/Emails.php b/src/Resources/Emails.php index 9bcbe7b..b71e848 100644 --- a/src/Resources/Emails.php +++ b/src/Resources/Emails.php @@ -134,14 +134,14 @@ public function get(string $folderId = '', array $params = []): MsGraph $folder = $folderId == '' ? 'Inbox' : $folderId; - //get inbox from folders list + // get inbox from folders list $folder = MsGraph::get("me/mailFolders?\$filter=startswith(displayName,'$folder')"); if (isset($folder['value'][0])) { - //folder id + // folder id $folderId = $folder['value'][0]['id']; - //get messages from folderId + // get messages from folderId return MsGraph::get("me/mailFolders/$folderId/messages?".$params); } else { throw new Exception('email folder not found'); @@ -162,21 +162,21 @@ public function findInlineAttachments(array $email): array { $attachments = self::findAttachments($email['id']); - //replace every case of $name, - 'folder' => new \stdClass(), + 'folder' => new \stdClass, '@microsoft.graph.conflictBehavior' => $behavior, ]); } diff --git a/src/Resources/Tasks/Tasks.php b/src/Resources/Tasks/Tasks.php index 4ed2060..45beb04 100644 --- a/src/Resources/Tasks/Tasks.php +++ b/src/Resources/Tasks/Tasks.php @@ -64,7 +64,7 @@ protected function getParams(array $params, int $perPage, string $instance): arr '$skip' => $page, ]; } else { - //ensure $top, $skip and $count are part of params + // ensure $top, $skip and $count are part of params if (! in_array('$top', $params)) { $params['$top'] = $perPage; } diff --git a/tests/MsGraphAdminTest.php b/tests/MsGraphAdminTest.php index da8510a..dff6a29 100644 --- a/tests/MsGraphAdminTest.php +++ b/tests/MsGraphAdminTest.php @@ -9,7 +9,7 @@ }); test('can initalise', function () { - $this->assertInstanceOf(MsGraphAdmin::class, new MsGraphAdmin()); + $this->assertInstanceOf(MsGraphAdmin::class, new MsGraphAdmin); }); test('can refresh token', function () { diff --git a/tests/PaginatorTest.php b/tests/PaginatorTest.php new file mode 100644 index 0000000..7b666a1 --- /dev/null +++ b/tests/PaginatorTest.php @@ -0,0 +1,40 @@ +toBeInstanceOf(Paginator::class); +}); + +test('get_start calculates correct offset', function () { + $_GET['page'] = 3; + $paginator = new Paginator(10, 'page'); + + expect($paginator->get_start())->toBe(20); +}); + +test('setTotal updates totalRows correctly', function () { + $paginator = new Paginator(10, 'page'); + $paginator->setTotal(50); + + expect($paginator->page_links_array())->toHaveCount(5); +}); + +test('page_links_array returns correct pagination', function () { + $paginator = new Paginator(10, 'page'); + $paginator->setTotal(50); + + expect($paginator->page_links_array())->toBe([1, 2, 3, 4, 5]); +}); + +test('page_links generates correct HTML', function () { + $paginator = new Paginator(10, 'page'); + $paginator->setTotal(30); + + $html = $paginator->page_links(); + + expect($html)->toContain("