From bf68a01177555dc3a5ad0c069c99c0a8b1d7523c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B0=E6=9E=97?= Date: Sun, 26 Sep 2021 19:03:50 +0800 Subject: [PATCH] calendar test --- src/Client.php | 26 +++--- src/Utilities/Abs.php | 33 +++----- tests/AbsCase.php | 2 +- tests/Calendars/AvailabilityTest.php | 122 +++++++++++++++++++++++++++ tests/Calendars/CalendarTest.php | 8 +- 5 files changed, 153 insertions(+), 38 deletions(-) create mode 100644 tests/Calendars/AvailabilityTest.php diff --git a/src/Client.php b/src/Client.php index 5b10e6b..a910d94 100644 --- a/src/Client.php +++ b/src/Client.php @@ -38,7 +38,12 @@ class Client /** * @var Options */ - private Options $options; + public Options $options; + + /** + * @var array + */ + private array $objects = []; // ------------------------------------------------------------------------------ @@ -76,18 +81,6 @@ public function __get(string $name): object // ------------------------------------------------------------------------------ - /** - * get options instance for setting options - * - * @return \Nylas\Utilities\Options - */ - public function Options(): Options - { - return $this->options; - } - - // ------------------------------------------------------------------------------ - /** * call sub class * @@ -97,6 +90,11 @@ public function Options(): Options */ private function callSubClass(string $name): object { + if (!empty($this->objects[$name])) + { + return $this->objects[$name]; + } + $apiClass = __NAMESPACE__.'\\'.\ucfirst($name).'\\Abs'; // check class exists @@ -105,7 +103,7 @@ private function callSubClass(string $name): object throw new NylasException(null, "class {$apiClass} not found!"); } - return new $apiClass($this->options); + return $this->objects[$name] = new $apiClass($this->options); } // ------------------------------------------------------------------------------ diff --git a/src/Utilities/Abs.php b/src/Utilities/Abs.php index e83150e..d6d328d 100644 --- a/src/Utilities/Abs.php +++ b/src/Utilities/Abs.php @@ -21,6 +21,11 @@ trait Abs */ private Options $options; + /** + * @var array + */ + private array $objects = []; + // ------------------------------------------------------------------------------ /** @@ -49,34 +54,22 @@ public function __get(string $name): object // ------------------------------------------------------------------------------ - /** - * call nylas apis with __call - * - * @param string $name - * @param array $arguments - * - * @return object - */ - public function __call(string $name, array $arguments): object - { - return $this->callSubClass($name, $arguments); - } - - // ------------------------------------------------------------------------------ - /** * call sub class * * @param string $name - * @param array $arguments * * @return object */ - private function callSubClass(string $name, array $arguments = []): object + private function callSubClass(string $name): object { + if (!empty($this->objects[$name])) + { + return $this->objects[$name]; + } + $nmSpace = \trim(\get_class($this), 'Abs'); - $nmSpace = \trim($nmSpace, '\\'); - $subClass = $nmSpace.'\\'.\ucfirst($name); + $subClass = \trim($nmSpace, '\\').'\\'.\ucfirst($name); // check class exists if (!\class_exists($subClass)) @@ -84,7 +77,7 @@ private function callSubClass(string $name, array $arguments = []): object throw new NylasException(null, "class {$subClass} not found!"); } - return new $subClass($this->options, ...$arguments); + return $this->objects[$name] = new $subClass($this->options); } // ------------------------------------------------------------------------------ diff --git a/tests/AbsCase.php b/tests/AbsCase.php index 42bfc0a..61f6db3 100644 --- a/tests/AbsCase.php +++ b/tests/AbsCase.php @@ -186,7 +186,7 @@ protected function mockResponse(array $data, array $header = [], int $code = 200 $mock = new MockHandler([new Response($code, $header, $body)]); - $this->client->Options()->setHandler($mock); + $this->client->options->setHandler($mock); } // ------------------------------------------------------------------------------ diff --git a/tests/Calendars/AvailabilityTest.php b/tests/Calendars/AvailabilityTest.php new file mode 100644 index 0000000..ffb52b4 --- /dev/null +++ b/tests/Calendars/AvailabilityTest.php @@ -0,0 +1,122 @@ +getSingleParams(); + + $this->mockResponse([ + 'object' => 'availability', + 'time_slots' => [ + [ + 'end' => 1605803400, + 'object' => 'time_slot', + 'start' => 1605801600, + 'status' => 'free', + ], + [ + 'end' => 1605804000, + 'object' => 'time_slot', + 'start' => 1605802200, + 'status' => 'free', + ], + [ + 'end' => 1605804600, + 'object' => 'time_slot', + 'start' => 1605802800, + 'status' => 'free', + ], + [ + 'end' => 1605805200, + 'object' => 'time_slot', + 'start' => 1605803400, + 'status' => 'free', + ], + [ + 'end' => 1605805800, + 'object' => 'time_slot', + 'start' => 1605804000, + 'status' => 'free', + ], + [ + 'end' => 1605806400, + 'object' => 'time_slot', + 'start' => 1605804600, + 'status' => 'free', + ], + [ + 'end' => 1605807000, + 'object' => 'time_slot', + 'start' => 1605805200, + 'status' => 'free', + ], + [ + 'end' => 1605816000, + 'object' => 'time_slot', + 'start' => 1605814200, + 'status' => 'free', + ], + ], + ]); + + $data = $this->client->Calendars->Availability->availabilityForASingleMeeting($params); + + $this->assertArrayHasKey('time_slots', $data); + } + + // ------------------------------------------------------------------------------ + + private function getSingleParams(): array + { + return [ + 'duration_minutes' => 30, + 'start_time' => 1605794400, + 'end_time' => 1605826800, + 'interval_minutes' => 10, + 'emails' => ['swag@nylas.com'], + 'free_busy' => [ + [ + 'email' => 'lamarr@player.com', + 'object' => 'free/busy', + 'time_slots' => [ + [ + 'start_time' => 1601042400, + 'end_time' => 1601044200, + 'object' => 'time_slot', + 'status' => 'busy', + ], + ], + ], + ], + 'open_hours' => [ + [ + 'days' => ['0'], + 'emails' => ['swag@nylas.com'], + 'timezone' => 'America/Chicago', + 'start' => '10:00', + 'end' => '14:00', + 'object_type' => 'open_hours', + ], + ], + ]; + } + + // ------------------------------------------------------------------------------ +} diff --git a/tests/Calendars/CalendarTest.php b/tests/Calendars/CalendarTest.php index 429630b..79a3211 100644 --- a/tests/Calendars/CalendarTest.php +++ b/tests/Calendars/CalendarTest.php @@ -25,7 +25,7 @@ public function testGetOAuthAuthorize(): void 'view' => 'count', ]; - $data = $this->client->Calendars->Calendar->getCalendarsList($params); + $data = $this->client->Calendars->Calendar->returnAllCalendars($params); $this->assertArrayHasKey('count', $data); } @@ -36,7 +36,7 @@ public function testGetCalendar(): void { $id = 'f0yci053ovp2tit18hwemup33'; - $data = $this->client->Calendars->Calendar->getCalendar($id); + $data = $this->client->Calendars->Calendar->createACalendar($id); $this->assertArrayHasKey($id, $data); } @@ -47,7 +47,7 @@ public function testAddCalendar(): void { $params = $this->getCalendarInfo(); - $data = $this->client->Calendars->Calendar->addCalendar($params); + $data = $this->client->Calendars->Calendar->createACalendar($params); $this->assertArrayHasKey('id', $data); } @@ -66,4 +66,6 @@ private function getCalendarInfo(): array 'location' => 'Front Conference Room', ]; } + + // ------------------------------------------------------------------------------ }