diff --git a/src/Calendars/Availability.php b/src/Calendars/Availability.php index 0ed94cf..f5b3e16 100644 --- a/src/Calendars/Availability.php +++ b/src/Calendars/Availability.php @@ -50,7 +50,7 @@ public function __construct(Options $options) */ public function availabilityForASingleMeeting(array $params = []): array { - V::doValidate($this->getMeetingRules(), $params); + V::doValidate($this->getMeetingRules(true), $params); return $this->options ->getSync() @@ -74,7 +74,7 @@ public function availabilityForASingleMeeting(array $params = []): array */ public function availabilityForMultipleMeetings(array $params = []): array { - V::doValidate($this->getMeetingRules(), $params); + V::doValidate($this->getMeetingRules(false), $params); return $this->options ->getSync() @@ -88,8 +88,14 @@ public function availabilityForMultipleMeetings(array $params = []): array /** * @return \Nylas\Utilities\Validator */ - private function getMeetingRules(): V + private function getMeetingRules(bool $single): V { + $emailsRules = match ($single) + { + true => V::simpleArray(V::email()), + false => V::simpleArray(V::simpleArray(V::email())), + }; + $timeSlot = V::keySet( V::key('object', V::stringType()->notEmpty()), V::key('status', V::stringType()->notEmpty()), @@ -98,28 +104,28 @@ private function getMeetingRules(): V ); $freeBusy = V::keySet( - V::key('object', V::stringType()->notEmpty()), V::key('email', V::email()), - V::key('time_slot', V::simpleArray($timeSlot)), + V::key('object', V::stringType()->notEmpty()), + V::key('time_slots', V::simpleArray($timeSlot)), ); $openHours = V::keySet( - V::key('end', V::anyOf(V::equals(0), V::time('H:i'))), - V::key('start', V::anyOf(V::equals(0), V::time('H:i'))), + V::key('end', V::time('H:i')), + V::key('start', V::time('H:i')), V::key('days', V::simpleArray(V::in(['0', '1', '2', '3', '4', '5', '6']))), - V::key('emails', V::simpleArray(V::email())), + V::key('emails', $emailsRules), V::key('timezone', V::in(DateTimeZone::listIdentifiers())), V::key('object_type', V::equals('open_hours')), ); return V::keySet( - V::key('emails', V::simpleArray(V::email())), + V::key('emails', $emailsRules), V::key('end_time', V::timestampType()), V::key('start_time', V::timestampType()), V::key('free_busy', V::simpleArray($freeBusy)), - V::key('open_hours', V::simpleArray($openHours), false), V::key('interval_minutes', V::intType()), V::key('duration_minutes', V::intType()), + V::keyOptional('open_hours', V::simpleArray($openHours)), ); } diff --git a/src/Calendars/Calendar.php b/src/Calendars/Calendar.php index ea2e8ba..ac5071b 100644 --- a/src/Calendars/Calendar.php +++ b/src/Calendars/Calendar.php @@ -207,11 +207,15 @@ public function deleteACalendar(mixed $calendarId): array public function calendarFreeOrBusy(array $params = []): array { V::doValidate(V::keySet( - V::keyOptional('end_time', V::timestampType()), - V::keyOptional('start_time', V::timestampType()), - V::keyOptional('emails', V::simpleArray(V::email())) + V::key('end_time', V::timestampType()), + V::key('start_time', V::timestampType()), + V::key('emails', V::simpleArray(V::email())) ), $params); + // @todo the nylas docs require to pass Unix timestamp as a string, ball shit! + $params['end_time'] = (string) $params['end_time']; + $params['start_time'] = (string) $params['start_time']; + return $this->options ->getSync() ->setFormParams($params) diff --git a/src/Contacts/Contact.php b/src/Contacts/Contact.php index 3ade324..1d49e84 100644 --- a/src/Contacts/Contact.php +++ b/src/Contacts/Contact.php @@ -155,7 +155,7 @@ public function updateAContact(string $contactId, array $params): array * * @return array */ - public function deleteContact(mixed $contactId): array + public function deleteAContact(mixed $contactId): array { $contactId = Helper::fooToArray($contactId); @@ -213,7 +213,10 @@ public function returnsAContactsPicture(array $params): array }; } - return $this->options->getAsync()->pool($queues, true); + $picId = Helper::generateArray($downloadArr, 'id'); + $pools = $this->options->getAsync()->pool($queues, true); + + return Helper::concatPoolInfos($picId, $pools); } // ------------------------------------------------------------------------------ @@ -225,7 +228,7 @@ public function returnsAContactsPicture(array $params): array * * @return array */ - public function getContactGroups(): array + public function returnContactGroups(): array { return $this->options ->getSync() diff --git a/src/Drafts/Draft.php b/src/Drafts/Draft.php index 4483fff..311bfe5 100644 --- a/src/Drafts/Draft.php +++ b/src/Drafts/Draft.php @@ -219,7 +219,7 @@ private function arrayOfObject(): V return V::simpleArray( V::keySet( V::key('email', V::email()), - V::key('name', V::stringType(), false) + V::key('name', V::stringType()->notEmpty()) ) ); } diff --git a/src/Events/Validation.php b/src/Events/Validation.php index c9ace28..78d8f8c 100644 --- a/src/Events/Validation.php +++ b/src/Events/Validation.php @@ -27,11 +27,11 @@ class Validation public static function getEventRules(): V { return V::keySet( - V::key('when', self::timeRules()), + V::key('when', self::whenRules()), V::key('calendar_id', V::stringType()->notEmpty()), V::keyOptional('busy', V::boolType()), - V::keyOptional('read_only', V::boolType()), V::keyOptional('title', V::stringType()->notEmpty()), + V::keyOptional('read_only', V::boolType()), V::keyOptional('location', V::stringType()->notEmpty()), V::keyOptional('metadata', V::arrayType()), V::keyOptional('recurrence', self::recurrenceRules()), @@ -110,30 +110,32 @@ private static function participantsRules(): V // ------------------------------------------------------------------------------ /** - * get event time rules + * get event when rules * * @return \Nylas\Utilities\Validator */ - private static function timeRules(): V + private static function whenRules(): V { return V::anyOf( // time - V::keySet(V::key('time', V::timestampType())), + V::keySet(V::keyOptional('time', V::timestampType())), // date - V::keySet(V::key('date', V::date('Y-m-d'))), + V::keySet(V::keyOptional('date', V::date('Y-m-d'))), - // timespan + // date span V::keySet( - V::key('end_time', V::timestampType()), - V::key('start_time', V::timestampType()) + V::keyOptional('end_date', V::date('Y-m-d')), + V::keyOptional('start_date', V::date('Y-m-d')) ), - // date span + // timespan V::keySet( - V::key('end_date', V::date('Y-m-d')), - V::key('start_date', V::date('Y-m-d')) - ) + V::keyOptional('end_time', V::timestampType()), + V::keyOptional('start_time', V::timestampType()), + V::keyOptional('end_timezone', V::in(DateTimeZone::listIdentifiers())), + V::keyOptional('start_timezone', V::in(DateTimeZone::listIdentifiers())) + ), ); } @@ -146,24 +148,24 @@ private static function timeRules(): V */ private static function notificationRules(): V { - return V::each(V::oneOf( - V::keySet(V::key('sms', V::keySet( - V::key('type', V::equals('sms')), - V::key('message', V::stringType()->notEmpty()), - V::key('minutes_before_events', V::intType()), - ))), - V::keySet(V::key('email', V::keySet( - V::key('type', V::equals('email')), - V::key('body', V::stringType()->notEmpty()), - V::key('subject', V::stringType()->notEmpty()), - V::key('minutes_before_events', V::intType()), - ))), - V::keySet(V::key('webhooks', V::keySet( - V::key('url', V::url()), - V::key('type', V::equals('webhook')), - V::key('payload', V::stringType()->notEmpty()), - V::key('minutes_before_events', V::intType()), - ))), + return V::each(V::anyOf( + V::keySet( + V::keyOptional('type', V::equals('sms')), + V::keyOptional('message', V::stringType()->notEmpty()), + V::keyOptional('minutes_before_events', V::intType()), + ), + V::keySet( + V::keyOptional('type', V::equals('email')), + V::keyOptional('body', V::stringType()->notEmpty()), + V::keyOptional('subject', V::stringType()->notEmpty()), + V::keyOptional('minutes_before_events', V::intType()), + ), + V::keySet( + V::keyOptional('url', V::url()), + V::keyOptional('type', V::equals('webhook')), + V::keyOptional('payload', V::stringType()->notEmpty()), + V::keyOptional('minutes_before_events', V::intType()), + ), )); } @@ -176,49 +178,48 @@ private static function notificationRules(): V */ private static function conferenceRules(): V { + $autocreate = V::keySet( + V::key('provider', V::in(['Google Meet', 'Zoom Meeting'])), + V::key('autocreate', V::arrayType()), + ); + $webEx = V::keySet( V::key('provider', V::equals('WebEx')), V::key('details', V::keySet( - V::key('password', V::stringType()), - V::key('pin', V::stringType()), - V::key('url', V::stringType()) + V::keyOptional('password', V::stringType()), + V::keyOptional('pin', V::stringType()), + V::keyOptional('url', V::stringType()) )) ); $zoomMeeting = V::keySet( V::key('provider', V::equals('Zoom Meeting')), V::key('details', V::keySet( - V::key('meeting_code', V::stringType()), - V::key('password', V::stringType()), - V::key('url', V::stringType()), + V::keyOptional('meeting_code', V::stringType()), + V::keyOptional('password', V::stringType()), + V::keyOptional('url', V::stringType()), )) ); $goToMeeting = V::keySet( V::key('provider', V::equals('GoToMeeting')), V::key('details', V::keySet( - V::key('meeting_code', V::stringType()), - V::key('phone', V::simpleArray()), - V::key('url', V::stringType()), + V::keyOptional('meeting_code', V::stringType()), + V::keyOptional('phone', V::simpleArray()), + V::keyOptional('url', V::stringType()), )) ); $googleMeet = V::keySet( V::key('provider', V::equals('Google Meet')), V::key('details', V::keySet( - V::key('phone', V::simpleArray()), - V::key('pin', V::stringType()), - V::key('url', V::stringType()), + V::keyOptional('phone', V::simpleArray()), + V::keyOptional('pin', V::stringType()), + V::keyOptional('url', V::stringType()), )) ); - return V::oneOf( - V::keySet(V::key('details', V::oneOf($webEx, $zoomMeeting, $goToMeeting, $googleMeet))), - V::keySet(V::key('autocreate', V::keySet( - V::key('provider', V::in(['Google Meet', 'Zoom Meeting'])), - V::key('autocreate', V::arrayType()), - ))), - ); + return V::anyOf($autocreate, $webEx, $zoomMeeting, $goToMeeting, $googleMeet); } // ------------------------------------------------------------------------------ diff --git a/src/Request/AbsBase.php b/src/Request/AbsBase.php index 6a9d51e..7750320 100644 --- a/src/Request/AbsBase.php +++ b/src/Request/AbsBase.php @@ -154,7 +154,7 @@ public function setFormFiles(array $files) : self /** * set header params * - * @see https://docs.nylas.com/docs/using-access-tokens + * @see https://developer.nylas.com/docs/the-basics/authentication/authorizing-api-requests/ * * @param array $headers * diff --git a/tests/Calendars/AvailabilityTest.php b/tests/Calendars/AvailabilityTest.php index ffb52b4..e79bf28 100644 --- a/tests/Calendars/AvailabilityTest.php +++ b/tests/Calendars/AvailabilityTest.php @@ -49,36 +49,62 @@ public function testAvailabilityForASingleMeeting(): void 'start' => 1605803400, 'status' => 'free', ], + ], + ]); + + $data = $this->client->Calendars->Availability->availabilityForASingleMeeting($params); + + $this->assertArrayHasKey('time_slots', $data); + } + + // ------------------------------------------------------------------------------ + + public function testAvailabilityForMultipleMeetings(): void + { + $params = $this->getMultipleParams(); + + $this->mockResponse( + [ + [ [ - 'end' => 1605805800, - 'object' => 'time_slot', - 'start' => 1605804000, - 'status' => 'free', + "emails" => [ + "kat@spacetech.com", + "dorothy@spacetech.com" + ], + "end_time" => 1605794400, + "start_time" => 1605792600 ], [ - 'end' => 1605806400, - 'object' => 'time_slot', - 'start' => 1605804600, - 'status' => 'free', - ], + "emails" => [ + "dorothy@spacetech.com" + ], + "end_time" => 1605796200, + "start_time" => 1605794400 + ] + ], + [ [ - 'end' => 1605807000, - 'object' => 'time_slot', - 'start' => 1605805200, - 'status' => 'free', + "emails" => [ + "dorothy@spacetech.com" + ], + "end_time" => 1605801600, + "start_time" => 1605799800 ], [ - 'end' => 1605816000, - 'object' => 'time_slot', - 'start' => 1605814200, - 'status' => 'free', - ], + "emails" => [ + "kat@spacetech.com", + "dorothy@spacetech.com" + ], + "end_time" => 1605803400, + "start_time" => 1605801600 + ] ], ]); - $data = $this->client->Calendars->Availability->availabilityForASingleMeeting($params); - $this->assertArrayHasKey('time_slots', $data); + $data = $this->client->Calendars->Availability->availabilityForMultipleMeetings($params); + + $this->assertTrue(2 === \count($data)); } // ------------------------------------------------------------------------------ @@ -91,7 +117,7 @@ private function getSingleParams(): array 'end_time' => 1605826800, 'interval_minutes' => 10, 'emails' => ['swag@nylas.com'], - 'free_busy' => [ + 'free_busy' => [ [ 'email' => 'lamarr@player.com', 'object' => 'free/busy', @@ -107,8 +133,45 @@ private function getSingleParams(): array ], 'open_hours' => [ [ + 'days' => ['0'], + 'emails' => ['swag@nylas.com'], + 'timezone' => 'America/Chicago', + 'start' => '10:00', + 'end' => '14:00', + 'object_type' => 'open_hours', + ], + ], + ]; + } + + // ------------------------------------------------------------------------------ + + private function getMultipleParams(): array + { + return [ + 'duration_minutes' => 30, + 'start_time' => 1605794400, + 'end_time' => 1605826800, + 'interval_minutes' => 10, + 'emails' => [['swag@nylas.com']], + 'free_busy' => [ + [ + 'email' => 'swag@nylas.com', + 'object' => 'free_busy', + 'time_slots' => [ + [ + 'start_time' => 1605819600, + 'end_time' => 1605821400, + 'object' => 'time_slot', + 'status' => 'busy', + ], + ], + ], + ], + 'open_hours' => [ + [ + 'emails' => [['swag@nylas.com']], 'days' => ['0'], - 'emails' => ['swag@nylas.com'], 'timezone' => 'America/Chicago', 'start' => '10:00', 'end' => '14:00', diff --git a/tests/Calendars/CalendarTest.php b/tests/Calendars/CalendarTest.php index 79a3211..39d3e13 100644 --- a/tests/Calendars/CalendarTest.php +++ b/tests/Calendars/CalendarTest.php @@ -18,53 +18,172 @@ class CalendarTest extends AbsCase { // ------------------------------------------------------------------------------ - public function testGetOAuthAuthorize(): void + public function testReturnAllCalendars(): void { - $params = - [ - 'view' => 'count', + $params = [ + 'view' => 'expanded', + 'limit' => 10, + 'offset' => 0, ]; + $this->mockResponse([ + [ + 'account_id' => '5tgncdmczat03416u7d6uypyi', + 'description' => 'Emailed events', + 'id' => '6d4d54fd53c54a70a2b98e36038d', + 'is_primary' => null, + 'location' => null, + 'name' => 'Emailed events', + 'object' => 'calendar', + 'read_only' => true, + 'timezone' => null, + ], + [ + 'account_id' => '5tgncdmczat03416u7d6uypyi', + 'description' => null, + 'id' => 'fe51c4d8bedf45ec949bf1033c7', + 'is_primary' => true, + 'location' => null, + 'name' => 'tatiana.p@nylas.com', + 'object' => 'calendar', + 'read_only' => false, + 'timezone' => 'America/Chicago', + ], + ]); + $data = $this->client->Calendars->Calendar->returnAllCalendars($params); - $this->assertArrayHasKey('count', $data); + $this->assertArrayHasKey('account_id', $data[0]); } // ------------------------------------------------------------------------------ - public function testGetCalendar(): void + public function testCreateACalendar(): void { - $id = 'f0yci053ovp2tit18hwemup33'; + $params = [ + 'name' => 'My New Calendar', + 'description' => 'Description of my new calendar', + 'location' => 'Location description', + 'timezone' => 'America/Los_Angeles', + ]; + + $this->mockResponse([ + 'account_id' => 'eof2wrhqkl7kdwhy9hylpv9o9', + 'description' => 'Description of my new calendar', + 'id' => '8e570s302fdazx9zqwiuk9jqn', + 'is_primary' => true, + 'job_status_id' => '48pp6ijzrxpw9jors9ylnsxnf', + 'location' => 'Location description', + 'name' => 'My New Calendar', + 'object' => 'calendar', + 'read_only' => true, + 'timezone' => 'America/Los_Angeles', + ]); + + $data = $this->client->Calendars->Calendar->createACalendar($params); + + $this->assertArrayHasKey('account_id', $data); + } + + // ------------------------------------------------------------------------------ - $data = $this->client->Calendars->Calendar->createACalendar($id); + public function testReturnACalendar(): void + { + $id = '6d4d54fd53c54a70a2b98e36038d'; + + $this->mockResponse([ + 'account_id' => '5tgncdmczat03416u7d6uypyi', + 'description' => 'Emailed events', + 'id' => '6d4d54fd53c54a70a2b98e36038d', + 'is_primary' => null, + 'location' => null, + 'name' => 'Emailed events', + 'object' => 'calendar', + 'read_only' => true, + 'timezone' => null, + ]); + + $data = $this->client->Calendars->Calendar->returnACalendar($id); $this->assertArrayHasKey($id, $data); } // ------------------------------------------------------------------------------ - public function testAddCalendar(): void + public function testUpdateACalendar(): void { - $params = $this->getCalendarInfo(); + $id = '8e570s302fdazx9zqwiuk9jqn'; - $data = $this->client->Calendars->Calendar->createACalendar($params); + $params = [ + 'name' => 'My New Calendar', + 'description' => 'Description of my new calendar', + 'location' => 'Location description', + 'timezone' => 'America/Los_Angeles', + ]; + + $this->mockResponse([ + 'account_id' => 'eof2wrhqkl7kdwhy9hylpv9o9', + 'description' => 'Description of my new calendar', + 'id' => '8e570s302fdazx9zqwiuk9jqn', + 'is_primary' => true, + 'job_status_id' => '48pp6ijzrxpw9jors9ylnsxnf', + 'location' => 'Location description', + 'name' => 'My New Calendar', + 'object' => 'calendar', + 'read_only' => true, + 'timezone' => 'America/Los_Angeles', + ]); + + $data = $this->client->Calendars->Calendar->updateACalendar($id, $params); $this->assertArrayHasKey('id', $data); } // ------------------------------------------------------------------------------ - /** - * @return array - */ - private function getCalendarInfo(): array + public function testDeleteACalendar(): void { - return [ - 'name' => 'Test Calendar', - 'description' => 'This is a test calendar.', - 'timezone' => 'America/New_York', - 'location' => 'Front Conference Room', + $id = '8e570s302fdazx9zqwiuk9jqn'; + + $this->mockResponse(['job_status_id' => '48pp6ijzrxpw9jors9ylnsxnf']); + + $data = $this->client->Calendars->Calendar->deleteACalendar($id); + + $this->assertArrayHasKey($id, $data); + } + + // ------------------------------------------------------------------------------ + + public function testCalendarFreeOrBusy(): void + { + $params = [ + 'emails' => ['nyla@nylas.com'], + 'end_time' => 1409598000, + 'start_time' => 1409594400, ]; + + $this->mockResponse([[ + 'object' => 'free_busy', + 'email' => 'swag@nylas.com', + 'time_slots' => [ + [ + 'object' => 'time_slot', + 'status' => 'busy', + 'start_time' => 1409594400, + 'end_time' => 1409598000, + ], + [ + 'object' => 'time_slot', + 'status' => 'busy', + 'start_time' => 1409598000, + 'end_time' => 1409599000, + ], + ], + ]]); + + $data = $this->client->Calendars->Calendar->calendarFreeOrBusy($params); + + $this->assertArrayHasKey('email', $data[0]); } // ------------------------------------------------------------------------------ diff --git a/tests/Contacts/ContactTest.php b/tests/Contacts/ContactTest.php index 106b6b5..36cf388 100644 --- a/tests/Contacts/ContactTest.php +++ b/tests/Contacts/ContactTest.php @@ -2,7 +2,6 @@ namespace Tests\Contacts; -use Exception; use Tests\AbsCase; /** @@ -19,145 +18,253 @@ class ContactTest extends AbsCase { // ------------------------------------------------------------------------------ - public function testGetContactsList(): void + public function testReturnAllContacts(): void { - $data = $this->client->Contacts->Contact->getContactsList(); + $this->mockResponse([ + [ + 'account_id' => '5tgncdmczat02216u7d6uypyi', + 'birthday' => null, + 'company_name' => null, + 'emails' => [ + [ + 'email' => 'tom@brightideas.com', + 'type' => 'other', + ], + ], + 'given_name' => 'Thomas', + 'groups' => [], + 'id' => '7e1b9vqhzyjn05y22sdoxl9ij', + 'im_addresses' => [], + 'job_title' => null, + 'manager_name' => null, + 'middle_name' => null, + 'nickname' => null, + 'notes' => null, + 'object' => 'contact', + 'office_location' => null, + 'phone_numbers' => [], + 'physical_addresses' => [], + 'picture_url' => 'https://api.nylas.com/contacts/7vqhzyjn05y22sdoxl9ij/picture', + 'source' => 'address_book', + 'suffix' => null, + 'surname' => 'Edison', + 'web_pages' => [], + ], + ]); + + $data = $this->client->Contacts->Contact->returnAllContacts(); + + $this->assertArrayHasKey('id', $data[0]); + } + + // ------------------------------------------------------------------------------ + + public function testCreateAContact(): void + { + $params = $this->getContactBase(); + + $this->mockResponse($this->getContactData()); - $this->assertTrue(\count($data) > 0); + $data = $this->client->Contacts->Contact->createAContact($params); + + $this->assertArrayHasKey('groups', $data); } // ------------------------------------------------------------------------------ - public function testGetContact(): void + public function testReturnAContact(): void { - $id = 'p8yaokbz6oh8bd45jcs1vt74'; + $id = 'z3z3z3z3z3z3z3z3z3z3z3'; + + $this->mockResponse($this->getContactData()); - $data = $this->client->Contacts->Contact->getContact($id); + $data = $this->client->Contacts->Contact->returnAContact($id); $this->assertArrayHasKey($id, $data); } // ------------------------------------------------------------------------------ - public function testAddContact(): void + public function testUpdateAContact(): void { - $params = $this->getContactInfo(); + $id = 'z3z3z3z3z3z3z3z3z3z3z3'; + + $params = $this->getContactBase(); - $data = $this->client->Contacts->Contact->addContact($params); + $this->mockResponse($this->getContactData()); + + $data = $this->client->Contacts->Contact->updateAContact($id, $params); $this->assertArrayHasKey('id', $data); } // ------------------------------------------------------------------------------ - public function testUpdateContact(): void + public function testDeleteAContact(): void { - $params = - [ - 'id' => 'ojuzyfudlwkrwg476ip9sfw4', - 'company_name' => 'testing', - 'given_name' => 'Gege', - 'surname' => 'Chou', - ]; + $id = 'z3z3z3z3z3z3z3z3z3z3z3'; - $data = $this->client->Contacts->Contact->updateContact($params); + $this->mockResponse([]); - $this->assertArrayHasKey('id', $data); + $data = $this->client->Contacts->Contact->deleteAContact($id); + + $this->assertArrayHasKey($id, $data); } // ------------------------------------------------------------------------------ - public function testDeleteContact(): void + public function testReturnsAContactsPicture(): void { - $params = - [ - 'company_name' => null, - 'given_name' => 'Bown', - 'surname' => 'Chou', + $params = [ + 'id' => 'ojuzyfudlwkrwg476ip9sfw4', + 'path' => __DIR__.'/temp', ]; - $data = $this->client->Contacts->Contact->addContact($params); + $this->mockResponse([]); - try - { - $back = true; - $this->client->Contacts->Contact->deleteContact($data['id']); - } - catch (Exception $e) - { - $back = false; - } + $data = $this->client->Contacts->Contact->returnsAContactsPicture($params); - $this->assertTrue($back); + $this->assertArrayHasKey($params['id'], $data); } // ------------------------------------------------------------------------------ - public function testGetContactGroups(): void + public function testReturnContactGroups(): void { - $data = $this->client->Contacts->Contact->getContactGroups(); - - $this->assertTrue(\count($data) > 0); + $this->mockResponse([ + [ + 'id' => 'a0a0a0a0a0a0a0a0a0a0a0', + 'object' => 'contact_group', + 'account_id' => 'x2x2x2x2x2x2x2x2x2x2x2', + 'name' => 'Work', + 'path' => 'Contacts/Work', + ], + ]); + + $data = $this->client->Contacts->Contact->returnContactGroups(); + + $this->assertArrayHasKey('id', $data[0]); } // ------------------------------------------------------------------------------ - public function testGetContactPicture(): void + private function getContactBase(): array { - $params = - [ - 'id' => 'ojuzyfudlwkrwg476ip9sfw4', - 'path' => __DIR__.'/temp', + return [ + 'birthday' => '1960-12-31', + 'company_name' => 'Nylas', + 'emails' => [ + [ + 'email' => 'john@doe.com', + 'type' => 'work', + ], + ], + 'given_name' => 'John', + 'im_addresses' => [ + [ + 'type' => 'aim', + 'im_address' => 'myaimaddress', + ], + ], + 'job_title' => 'Software Engineer', + 'manager_name' => 'Bill the manager', + 'middle_name' => 'Jacob', + 'nickname' => 'JD', + 'notes' => 'Loves ramen', + 'office_location' => '123 Main Street', + 'phone_numbers' => [ + [ + 'number' => '1 800 123 4567', + 'type' => 'business', + ], + ], + 'physical_addresses' => [ + [ + 'format' => 'string', + 'type' => 'work', + 'street_address' => 'string', + 'city' => 'string', + 'postal_code' => 'string', + 'state' => 'string', + 'country' => 'string', + ], + ], + 'suffix' => 'string', + 'surname' => 'string', + 'web_pages' => [ + [ + 'type' => 'profile', + 'url' => 'string', + ], + ], + 'group' => 'string', ]; - - $data = $this->client->Contacts->Contact->getContactPicture($params); - - $this->assertTrue(\count($data) > 0); } // ------------------------------------------------------------------------------ - /** - * @return array - */ - private function getContactInfo(): array + private function getContactData(): array { return [ - 'given_name' => 'My', - 'middle_name' => 'Nylas', - 'surname' => 'Friend', - 'birthday' => '2014-06-01', - 'suffix' => 'API', - 'nickname' => 'Nylas', - 'company_name' => 'Nylas', - 'job_title' => 'Communications Platform', - 'manager_name' => 'Communications', - 'office_location'=> 'SF', - 'notes' => 'Check out the Nylas Email, Calendar, and Contacts APIs', - 'emails' => [[ - 'type' => 'personal', - 'email'=> 'swagg@nylas.com', - ]], - 'physical_addresses'=> [[ - 'type' => 'work', - 'street_address'=> '944 Market St, 8th Floor', - 'city' => 'San Francisco', - 'postal_code' => '94102', - 'state' => 'CA', - 'country' => 'USA', - ]], - 'phone_numbers'=> [[ - 'type' => 'home', - 'number'=> '123-123-1234', - ]], - 'web_pages'=> [[ - 'type'=> 'homepage', - 'url' => 'https=>//nylas.com', - ]], - 'im_addresses'=> [[ - 'type' => 'gtalk', - 'im_address'=> 'Nylas', - ]], + 'account_id' => 'x2x2x2x2x2x2x2x2x2x2x2', + 'birthday' => '1960-12-31', + 'company_name' => 'Nylas', + 'emails' => [ + [ + 'email' => 'john@doe.com', + 'type' => 'work', + ], + ], + 'given_name' => 'John', + 'id' => 'z3z3z3z3z3z3z3z3z3z3z3', + 'im_addresses' => [ + [ + 'type' => 'aim', + 'im_address' => 'myaimaddress', + ], + ], + 'job_title' => 'Software Engineer', + 'manager_name' => 'Bill the manager', + 'middle_name' => 'Jacob', + 'nickname' => 'JD', + 'notes' => 'Loves ramen', + 'object' => 'contact', + 'office_location' => '123 Main Street', + 'phone_numbers' => [ + [ + 'number' => '1 800 123 4567', + 'type' => 'business', + ], + ], + 'physical_addresses' => [ + [ + 'format' => 'string', + 'type' => 'work', + 'street_address' => 'string', + 'city' => 'string', + 'postal_code' => 'string', + 'state' => 'string', + 'country' => 'string', + ], + ], + 'picture_url' => 'https://api.nylas.com/contacts/427abc427abc427abc/picture', + 'suffix' => 'string', + 'surname' => 'string', + 'web_pages' => [ + [ + 'type' => 'profile', + 'url' => 'string', + ], + ], + 'groups' => [ + [ + 'id' => 'string', + 'object' => 'contact_group', + 'account_id' => 'string', + 'name' => 'string', + 'path' => 'string', + ], + ], ]; } diff --git a/tests/Contacts/temp b/tests/Contacts/temp new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/tests/Contacts/temp @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/Deltas/DeltaTest.php b/tests/Deltas/DeltaTest.php index bf3268e..2b296c4 100644 --- a/tests/Deltas/DeltaTest.php +++ b/tests/Deltas/DeltaTest.php @@ -18,58 +18,104 @@ class DeltaTest extends AbsCase { // ------------------------------------------------------------------------------ - public function testGetLatestCursor(): void + public function testGetADeltaCursor(): void { - $data = $this->client->Deltas->Delta->getLatestCursor(); + $this->mockResponse([ + 'cursor' => 'aqb0llc2ioo0***', + ]); + + $data = $this->client->Deltas->Delta->getADeltaCursor(); $this->assertArrayHasKey('cursor', $data); } // ------------------------------------------------------------------------------ - public function testGetSetOfDeltas(): void + public function testRequestDeltaCursors(): void { - $params = - [ - 'cursor' => '4whx9f0r544dwd07ipymrj7a1', - 'exclude_types' => 'message', - ]; + $params = ['cursor' => '4whx9f0r544dwd07ipymrj7a1']; - $data = $this->client->Deltas->Delta->getSetOfDeltas($params); + $this->mockResponse($this->getDeltasData()); + + $data = $this->client->Deltas->Delta->requestDeltaCursors($params); $this->assertArrayHasKey('deltas', $data); } // ------------------------------------------------------------------------------ - public function testLongPollingDelta(): void + public function testReturnLongPollingDeltas(): void { - $params = - [ - 'cursor' => '4whx9f0r544dwd07ipymrj7a1', - 'timeout' => 20, - 'exclude_types' => 'message', - ]; + $params = ['cursor' => '4whx9f0r544dwd07ipymrj7a1']; - $data = $this->client->Deltas->Delta->longPollingDelta($params); + $this->mockResponse($this->getDeltasData()); + + $data = $this->client->Deltas->Delta->returnLongPollingDeltas($params); $this->assertArrayHasKey('deltas', $data); } // ------------------------------------------------------------------------------ - public function testStreamingDelta(): void + public function testStreamingDeltas(): void { - $params = - [ - 'cursor' => '4whx9f0r544dwd07ipymrj7a1', - 'exclude_types' => 'message', - ]; + $params = ['cursor' => '4whx9f0r544dwd07ipymrj7a1']; + + $this->mockResponse($this->getDeltasData()); - $data = $this->client->Deltas->Delta->streamingDelta($params); + + $data = $this->client->Deltas->Delta->streamingDeltas($params); $this->assertArrayHasKey('deltas', $data); } // ------------------------------------------------------------------------------ + + private function getDeltasData(): array + { + return [ + 'cursor_end' => '4ekj8ktel67njbaw1c0nlvbdi', + 'cursor_start' => '9fboxh6t9b3ar4fwocxpwrcss', + 'deltas' => [ + [ + 'attributes' => [ + 'account_id' => 'aaz875kwuvxik6ku7pwkqp3ah', + 'bcc' => [], + 'body' => 'testing', + 'cc' => [], + 'date' => 1602001027, + 'events' => [], + 'files' => [], + 'from' => [[ + 'email' => 'email@nylas.com', + 'name' => 'Katherine Perry', + ]], + 'id' => '52m5un5v1m7rjigna5agc7y35', + 'labels' => [[ + 'display_name' => 'Sent Mail', + 'id' => 'ertg5obp5nvn43xtqe2e55en0', + 'name' => 'sent', + ]], + 'object' => 'message', + 'reply_to' => [], + 'snippet' => 'Hi Katherine PerryLead Technical Writer, Nylasemail@nylas.com', + 'starred' => false, + 'subject' => 'New Message', + 'thread_id' => 'chvd75bowkhg3gfpgeeygcxbb', + 'to' => [[ + 'email' => 'swag@nylas.com', + 'name' => 'Katherine Personal', + ]], + 'unread' => false, + ], + 'cursor' => '8hhvivgus0fbo4qengko8c38x', + 'event' => 'create', + 'id' => '52m5un5v1m7rjigna5agc7y35', + 'object' => 'message', + ], + ], + ]; + } + + // ------------------------------------------------------------------------------ } diff --git a/tests/Drafts/DraftTest.php b/tests/Drafts/DraftTest.php index f7d4112..e36e1b0 100644 --- a/tests/Drafts/DraftTest.php +++ b/tests/Drafts/DraftTest.php @@ -2,7 +2,6 @@ namespace Tests\Drafts; -use Exception; use Tests\AbsCase; /** @@ -19,100 +18,186 @@ class DraftTest extends AbsCase { // ------------------------------------------------------------------------------ - public function testGetDraftList(): void + public function testReturnAllDrafts(): void { - $data = $this->client->Drafts->Draft->getDraftsList(); + $this->mockResponse([$this->getDraftData()]); - $this->assertTrue(\count($data) > 0); + $data = $this->client->Drafts->Draft->returnAllDrafts(); + + $this->assertArrayHasKey('account_id', $data[0]); } // ------------------------------------------------------------------------------ - public function testGetDraft(): void + public function testCreateADraft(): void { - $id = 'c5m5em1s3jd2ggsttf2zayzre'; + $params = $this->getDraftBase(); - $data = $this->client->Drafts->Draft->getDraft($id); + $this->mockResponse($this->getDraftData()); - $this->assertArrayHasKey($id, $data); + $data = $this->client->Drafts->Draft->createADraft($params); + + $this->assertArrayHasKey('account_id', $data); } // ------------------------------------------------------------------------------ - public function testAddDraft(): void + public function testReturnADraft(): void { - $params = - [ - 'subject' => 'loving you', - 'to' => [['name' => '', 'email' => 'test@test.com']], - ]; + $id = '2vgnewhaclx4iog2140bva9y8'; + + $this->mockResponse($this->getDraftData()); - $data = $this->client->Drafts->Draft->addDraft($params); + $data = $this->client->Drafts->Draft->returnADraft($id); - $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey($id, $data); } // ------------------------------------------------------------------------------ - public function testUpdateDraft(): void + public function testUpdateADraft(): void { - $params = - [ - 'id' => '70dwlz4bfstk68pc4c0ae5rxw', - 'version' => 4, - 'subject' => 'loving - you!!!', - 'to' => [['name' => 'zhang san', 'email' => 'test@test.com']], - ]; + $id = '2vgnewhaclx4iog2140bva9y8'; + + $mock = $this->getDraftBase(); + + $mock['version'] = 2; - $data = $this->client->Drafts->Draft->updateDraft($params); + $this->mockResponse($this->getDraftData()); - $this->assertArrayHasKey('id', $data); + $data = $this->client->Drafts->Draft->updateADraft($id, $mock); + + $this->assertArrayHasKey('account_id', $data); } // ------------------------------------------------------------------------------ - public function testDeleteDraft(): void + public function testDeleteADraft(): void { - $params = - [ - 'id' => '70dwlz4bfstk68pc4c0ae5rxw', - 'version' => 5, - ]; + $params = ['id' => '2vgnewhaclx4iog2140bva9y8', 'version' => 2]; + + $this->mockResponse([]); - try - { - $back = true; - $this->client->Drafts->Draft->deleteDraft($params); - } - catch (Exception $e) - { - $back = false; - } - - $this->assertTrue($back); + $data = $this->client->Drafts->Draft->deleteADraft($params); + + $this->assertArrayHasKey($params['id'], $data); } // ------------------------------------------------------------------------------ - public function testSending(): void + private function getDraftBase(): array { - $params = - [ - 'subject' => 'loving you', - 'to' => [['name' => '', 'email' => 'test@test.com']], + return [ + 'subject' => 'From Nylas', + 'to' => [ + [ + 'name' => 'Dorothy Vaughan', + 'email' => 'dorothy@spacetech.com', + ], + ], + 'cc' => [ + [ + 'name' => 'George Washington Carver', + 'email' => 'carver@agritech.com', + ], + ], + 'bcc' => [ + [ + 'name' => 'Albert Einstein', + 'email' => 'al@particletech.com', + ], + ], + 'from' => [ + [ + 'name' => 'Marie Curie', + 'email' => 'marie@radioactivity.com', + ], + ], + 'reply_to' => [ + [ + 'email' => 'skwolek@fibers.com', + 'name' => 'Stephanie Kwolek', + ], + ], + 'reply_to_message_id' => '5ko445dyrr4s5pqc6n0klxhg0', + 'body' => 'This email was sent using the Nylas email API. Visit https://nylas.com for details.', + 'file_ids' => ['string'], ]; + } - $draft = $this->client->Drafts->Draft->addDraft($params); + // ------------------------------------------------------------------------------ - $params = - [ - 'version' => $draft['version'], - 'draft_id' => $draft['id'], + private function getDraftData(): array + { + return [ + 'account_id' => '{{account_id}}', + 'bcc' => [ + [ + 'email' => 'string', + 'name' => 'string', + ], + ], + 'body' => 'This email was sent using the Nylas email API. Visit https://nylas.com for details.', + 'cc' => [ + [ + 'email' => 'string', + 'name' => 'string', + ], + ], + 'date' => 1623080724, + 'events' => [ + ], + 'files' => [ + [ + 'content_disposition' => 'attachment', + 'content_type' => 'image/png', + 'filename' => 'Screen Shot 2021-06-01 at 5.06.47 PM.png', + 'id' => '{{file_id}}', + 'size' => 82186, + ], + ], + 'folder' => [ + 'display_name' => 'Drafts', + 'id' => '5i4pj87birnlrno7s9xu9f5cl', + 'name' => 'drafts', + ], + 'from' => [ + [ + 'email' => 'Your Name', + 'name' => 'you@example.com', + ], + ], + 'id' => '2vgnewhaclx4iog2140bva9y8', + 'job_status_id' => [ + 'account_id' => '88q8rglxj7jaeneiykhll23e2', + 'action' => 'save_draft', + 'created_at' => 1623080724, + 'id' => '2vgnewhaclx4iog2140bva9y8', + 'job_status_id' => 'ar22f9bwfboc4afyhnmfnuxc8', + 'object' => 'message', + 'status' => 'pending', + ], + 'object' => 'draft', + 'reply_to' => [ + [ + 'email' => 'swag@nylas.com', + 'name' => 'Nylas', + ], + ], + 'reply_to_message_id' => '5ko445dyrr4s5pqc6n0klxhg0', + 'snippet' => 'This email was sent using the Nylas email API. Visit https://nylas.com for details.', + 'starred' => false, + 'subject' => 'From Nylas', + 'thread_id' => '43va79g3vpq2z0ojwq09jg5el', + 'to' => [ + [ + 'email' => 'swag@nylas.com', + 'name' => 'Nylas', + ], + ], + 'unread' => false, + 'version' => 0, ]; - - $data = $this->client->Drafts->Sending->sendDraft($params); - - $this->assertArrayHasKey($draft['id'], $data); } // ------------------------------------------------------------------------------ diff --git a/tests/Events/EventTest.php b/tests/Events/EventTest.php index 346852c..bce67a3 100644 --- a/tests/Events/EventTest.php +++ b/tests/Events/EventTest.php @@ -2,7 +2,6 @@ namespace Tests\Events; -use Exception; use Tests\AbsCase; /** @@ -19,75 +18,287 @@ class EventTest extends AbsCase { // ------------------------------------------------------------------------------ - public function testGetEventList(): void + public function testReturnAllEvents(): void { - $data = $this->client->Events->Event->getEventsList(); + $this->mockResponse([$this->getEventData()]); - $this->assertTrue(\count($data) > 0); + $data = $this->client->Events->Event->returnAllEvents(); + + $this->assertArrayHasKey('account_id', $data[0]); } // ------------------------------------------------------------------------------ - public function testGetEvent(): void + /** + * @dataProvider getProvidData + * + * @param array $params + */ + public function testCreateAnEvents(array $params): void { - $params = ['id' => 'ejom4k3o5qor5ooyh8yx7hgbw']; + $this->mockResponse($this->getEventData()); - $data = $this->client->Events->Event->getEvent($params); + $data = $this->client->Events->Event->createAnEvent($params); - $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey('account_id', $data); } // ------------------------------------------------------------------------------ - public function testAddEvent(): void + public function testReturnAnEvent(): void { - $params = - [ - 'calendar_id' => '1fskeosmvaffwuffq774enx5p', - 'when' => ['time' => \time()], - 'title' => 'nothing...', - ]; + $data = $this->client->Events->Event->returnAnEvent(); + + $this->assertArrayHasKey($id, $data); + } + + // ------------------------------------------------------------------------------ + + public function testUpdateAnEvent(): void + { + $data = $this->client->Events->Event->updateAnEvent(); + + $this->assertArrayHasKey($id, $data); + } - $data = $this->client->Events->Event->addEvent($params); + // ------------------------------------------------------------------------------ - $this->assertArrayHasKey('id', $data); + public function testDeleteAnEvent(): void + { + $data = $this->client->Events->Event->deleteAnEvent(); + + $this->assertArrayHasKey($id, $data); } // ------------------------------------------------------------------------------ - public function testUpdateEvent(): void + public function testSendRSVP(): void { - $params = - [ - 'id' => '47137b6urkg0cf738o7is2aa3', - 'when' => ['time' => \time()], - ]; + $data = $this->client->Events->Event->sendRSVP(); - $data = $this->client->Events->Event->updateEvent($params); + $this->assertArrayHasKey($id, $data); + } + + // ------------------------------------------------------------------------------ - $this->assertArrayHasKey('id', $data); + private function getEventData(): array + { + return [ + 'account_id' => '{account_id}', + 'busy' => true, + 'calendar_id' => '{calendar_id}', + 'description' => 'Coffee meeting', + 'ical_uid' => '{ical_uid}', + 'id' => '{event_id}', + 'location' => 'string', + 'message_id' => 'string', + 'object' => 'event', + 'owner' => '', + 'participants' => [ + [ + 'name' => 'Dorothy Vaughan', + 'email' => 'dorothy@spacetech.com', + 'status' => 'noreply', + 'comment' => 'string', + ], + ], + 'read_only' => true, + 'title' => 'Remote Event: Group Yoga Class', + 'when' => [ + 'start_time' => 1409594400, + 'end_time' => 1409598000, + 'start_timezone' => 'America/New_York', + 'end_timezone' => 'America/New_York', + ], + 'status' => 'confirmed', + 'conferencing' => [ + 'provider' => 'WebEx', + 'details' => [ + 'password' => 'string', + 'pin' => 'string', + 'url' => 'string', + ], + ], + 'job_status_id' => 'string', + 'recurrence' => [ + 'rrule' => [ + 'RRULE:FREQ=WEEKLY;BYDAY=MO', + ], + 'timezone' => 'America/New_York', + ], + 'metadata' => [ + 'your-key' => 'string', + ], + ]; } // ------------------------------------------------------------------------------ - public function testDeleteDraft(): void + public function getProvidData(): array { - $params = - [ - 'id' => '47137b6urkg0cf738o7is2aa3', + $event = [ + 'title' => 'Birthday Party', + 'calendar_id' => '947kpa7ih22bfkeujpkfqn5bu', + 'status' => 'confirmed', + 'busy' => true, + 'read_only' => true, + 'participants' => [ + [ + 'name' => 'Aristotle', + 'email' => 'aristotle@nylas.com', + 'status' => 'yes', + ], + ], + 'description' => 'Come ready to skate', + 'when' => [ + 'object' => 'time', + 'time' => 1408875644, + ], + 'location' => 'Roller Rink', + 'recurrence' => [ + 'rrule' => [ + 'RRULE:FREQ=WEEKLY;BYDAY=MO', + ], + 'timezone' => 'America/New_York', + ], + ]; + + $conference = [ + 'title' => 'Birthday Party', + 'calendar_id' => '947kpa7ih22bfkeujpkfqn5bu', + 'busy' => true, + 'read_only' => true, + 'participants' => [ + [ + 'name' => 'Dorothy Vaughan', + 'email' => 'dorothy@spacetech.com', + 'status' => 'noreply', + ], + ], + 'description' => 'Come ready to skate', + 'when' => [ + 'time' => 1408875644, + 'timezone' => 'America/New_York', + ], + 'location' => 'Roller Rink', + 'recurrence' => [ + 'rrule' => [ + 'RRULE:FREQ=WEEKLY;BYDAY=MO', + ], + 'timezone' => 'America/New_York', + ], + 'conferencing' => [ + 'provider' => 'WebEx', + 'details' => [ + 'password' => 'string', + 'pin' => 'string', + 'url' => 'string', + ], + ], + 'reminder_minutes' => '[20]', + 'reminder_method' => 'popup', ]; - try - { - $back = true; - $this->client->Events->Event->deleteEvent($params); - } - catch (Exception $e) - { - $back = false; - } - - $this->assertTrue($back); + $metadata = [ + 'title' => 'Birthday Party', + 'location' => 'Roller Rink', + 'calendar_id' => '{calendar_id}', + 'status' => 'confirmed', + 'busy' => true, + 'read_only' => false, + 'participants' => [ + [ + 'name' => 'Thomas Edison', + 'email' => 'tom@brightideas.com', + ], + ], + 'description' => 'Lets Party!!!', + 'when' => [ + 'start_time' => '1615330800', + 'end_time' => '1615334400', + ], + 'metadata' => [ + 'number_of_guests' => '55', + 'event_type' => 'birthday', + 'internal_event_id' => 'b55469dk', + ], + ]; + + $zoom = [ + 'title' => 'Birthday Party', + 'location' => 'Roller Rink', + 'calendar_id' => 'egtdopqam5jxky7ifrkwcra55', + 'busy' => true, + 'read_only' => false, + 'conferencing' => [ + 'provider' => 'Zoom Meeting', + 'autocreate' => [ + 'settings' => [ + 'password' => '6789011', + 'settings' => [ + 'mute_upon_entry' => true, + ], + ], + ], + ], + 'participants' => [ + [ + 'name' => 'Katherine Johnson', + 'email' => 'kat@spacetech.com', + ], + ], + 'description' => 'Lets celebrate', + 'when' => [ + 'start_time' => '1627499520', + 'end_time' => '1630245600', + ], + ]; + + $notification = [ + 'title' => 'Lets celebrate', + 'location' => 'Roller Rink', + 'calendar_id' => 'egtdopqam5jxky7ifrkwcra55', + 'busy' => true, + 'read_only' => false, + 'conferencing' => [ + 'provider' => 'Zoom Meeting', + 'autocreate' => [ + 'settings' => ['settings' => []], + ], + ], + 'participants' => [ + [ + 'name' => 'Katherine Johnson', + 'email' => 'kat@spacetech.com', + 'phone_number' => '+12223456789', + ], + ], + 'when' => [ + 'start_time' => '1627499520', + 'end_time' => '1630245600', + ], + 'notifications' => [ + [ + 'type' => 'email', + 'minutes_before_events' => '600', + 'subject' => 'Test Event Notification', + 'body' => 'Reminding you about our meeting.', + ], + [ + 'type' => 'sms', + 'minutes_before_events' => '60', + 'message' => 'Test Event Notfication', + ], + ], + ]; + + return [ + 'zoom' => [$zoom], + 'event' => [$event], + 'metadata' => [$metadata], + 'conference' => [$conference], + 'notification' => [$notification], + ]; } // ------------------------------------------------------------------------------