Skip to content

Commit

Permalink
calendar test
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlin committed Sep 26, 2021
1 parent 156075a commit bf68a01
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 38 deletions.
26 changes: 12 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ class Client
/**
* @var Options
*/
private Options $options;
public Options $options;

/**
* @var array
*/
private array $objects = [];

// ------------------------------------------------------------------------------

Expand Down Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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);
}

// ------------------------------------------------------------------------------
Expand Down
33 changes: 13 additions & 20 deletions src/Utilities/Abs.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ trait Abs
*/
private Options $options;

/**
* @var array
*/
private array $objects = [];

// ------------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -49,42 +54,30 @@ 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))
{
throw new NylasException(null, "class {$subClass} not found!");
}

return new $subClass($this->options, ...$arguments);
return $this->objects[$name] = new $subClass($this->options);
}

// ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/AbsCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// ------------------------------------------------------------------------------
Expand Down
122 changes: 122 additions & 0 deletions tests/Calendars/AvailabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Tests\Calendars;

use Tests\AbsCase;

/**
* ----------------------------------------------------------------------------------
* Calendar Test
* ----------------------------------------------------------------------------------
*
* @author lanlin
* @change 2021/09/26
*
* @internal
*/
class AvailabilityTest extends AbsCase
{
// ------------------------------------------------------------------------------

public function testAvailabilityForASingleMeeting(): void
{
$params = $this->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' => ['[email protected]'],
'free_busy' => [
[
'email' => '[email protected]',
'object' => 'free/busy',
'time_slots' => [
[
'start_time' => 1601042400,
'end_time' => 1601044200,
'object' => 'time_slot',
'status' => 'busy',
],
],
],
],
'open_hours' => [
[
'days' => ['0'],
'emails' => ['[email protected]'],
'timezone' => 'America/Chicago',
'start' => '10:00',
'end' => '14:00',
'object_type' => 'open_hours',
],
],
];
}

// ------------------------------------------------------------------------------
}
8 changes: 5 additions & 3 deletions tests/Calendars/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -66,4 +66,6 @@ private function getCalendarInfo(): array
'location' => 'Front Conference Room',
];
}

// ------------------------------------------------------------------------------
}

0 comments on commit bf68a01

Please sign in to comment.