Skip to content

Commit

Permalink
[Calendar] Calendar Event Tag Relationship (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: onairmarc <[email protected]>
  • Loading branch information
onairmarc and onairmarc authored Nov 29, 2024
1 parent 813d493 commit 2099bd9
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 150 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ c98bbfd28bd7b34f22ade287046434127a6e200c
de929a45798d68147fec3ab9527979a5e5f0fce1
c90d7383de6e86260b6cc997b3d0af8e64826a11
baa146cd798ade580c6a6f4f2cbf98d5b72cc19f
cbe36f817a31ddbeb1ef59120b2406f6e9d4a689
28 changes: 28 additions & 0 deletions src/Objects/Calendar/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ public static function make(string $clientId, string $clientSecret): Event
{
$event = new self($clientId, $clientSecret);
$event->attributes = new EventAttributes;
$event->relationships = new EventRelationships;
$event->setApiVersion(PlanningCenterApiVersion::CALENDAR_DEFAULT);

return $event;
}

public function forEventId(string $eventId): static
{
$this->attributes->eventId = $eventId;

return $this;
}

public function all(array $query = []): ClientResponse
{
$http = $this->client()
Expand Down Expand Up @@ -72,6 +80,26 @@ public function instances(array $query = []): ClientResponse
return $eventInstance->all($query);
}

public function tags(array $query = []): ClientResponse
{
$http = $this->client()
->get($this->hostname() . self::EVENT_ENDPOINT . "/{$this->attributes->eventId}/tags", $query);

if ($this->isUsingSupportedApiVersion()) {
$tags = $http->json("data");
foreach ($tags as $tag) {
$tagRecord = Tag::make($this->clientId, $this->clientSecret);
$tagRecord->mapFromPco($tag);
$this->relationships->tags->add($tagRecord);
}
}

$clientResponse = new ClientResponse($http);
$clientResponse->data->add($this);

return $clientResponse;
}

private function mapFromPco(mixed $pco): void
{
$pco = pco_objectify($pco);
Expand Down
6 changes: 6 additions & 0 deletions src/Objects/Calendar/Relationships/EventRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

namespace EncoreDigitalGroup\PlanningCenter\Objects\Calendar\Relationships;

use EncoreDigitalGroup\PlanningCenter\Objects\Calendar\Tag;
use EncoreDigitalGroup\PlanningCenter\Objects\SdkObjects\Relationships\BasicRelationship;
use Illuminate\Support\Collection;

class EventRelationships
{
/** @var Collection<Tag> */
public Collection $tags;

public function __construct(public ?BasicRelationship $owner = null)
{
$this->owner = $owner ?? new BasicRelationship;
$this->tags = new Collection;
}
}
3 changes: 2 additions & 1 deletion src/Objects/Calendar/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function inTagGroup(string $tagGroupId): static
return $this;
}

private function mapFromPco(mixed $pco): void
/** @internal */
public function mapFromPco(mixed $pco): void
{
$pco = pco_objectify($pco);

Expand Down
13 changes: 8 additions & 5 deletions src/Traits/HasPlanningCenterClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ protected function processResponse(Response $http): ClientResponse
{
$clientResponse = new ClientResponse($http);

if (
$this->apiVersion == PlanningCenterApiVersion::PEOPLE_DEFAULT
|| $this->apiVersion == PlanningCenterApiVersion::GROUPS_DEFAULT
|| $this->apiVersion == PlanningCenterApiVersion::CALENDAR_DEFAULT
) {
if ($this->isUsingSupportedApiVersion()) {
$this->mapFromPco($http->json("data"));
if (!is_null($http->json("data"))) {
$clientResponse->data->add($this);
Expand All @@ -64,4 +60,11 @@ protected function processResponse(Response $http): ClientResponse

return $clientResponse;
}

protected function isUsingSupportedApiVersion(): bool
{
return $this->apiVersion == PlanningCenterApiVersion::PEOPLE_DEFAULT
|| $this->apiVersion == PlanningCenterApiVersion::GROUPS_DEFAULT
|| $this->apiVersion == PlanningCenterApiVersion::CALENDAR_DEFAULT;
}
}
6 changes: 3 additions & 3 deletions tests/Helpers/BaseMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected static function useCollectionResponse(ObjectType $type): array
$method = $type->value;

return [
'data' => [
"data" => [
static::$method(),
],
];
Expand All @@ -24,14 +24,14 @@ protected static function useSingleResponse(ObjectType $type): array
$method = $type->value;

return [
'data' => static::$method(),
"data" => static::$method(),
];
}

protected static function deleteResponse(): array
{
return [
'data' => null,
"data" => null,
];
}
}
4 changes: 2 additions & 2 deletions tests/Helpers/TestConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

class TestConstants
{
public const string CLIENT_ID = 'testClientId';
public const string CLIENT_SECRET = 'testClientSecret';
public const string CLIENT_ID = "testClientId";
public const string CLIENT_SECRET = "testClientSecret";
}
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

use Tests\TestCase;

uses(TestCase::class)->in('Unit');
uses(TestCase::class)->in("Unit");
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class TestCase extends OrchestraTestCase
{
protected const string HOSTNAME = 'https://api.planningcenteronline.com';
protected const string HOSTNAME = "https://api.planningcenteronline.com";

protected function setUp(): void
{
Expand All @@ -29,7 +29,7 @@ protected function setUp(): void

protected function getEnvironmentSetup($app): void
{
$app->singleton('http', function () {
$app->singleton("http", function (): \Illuminate\Http\Client\Factory {
return new HttpFactory();
});

Expand Down
135 changes: 74 additions & 61 deletions tests/Unit/Calendar/CalendarMocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class CalendarMocks extends BaseMock
{
use HasPlanningCenterClient;

public const string EVENT_ID = '1';
public const string EVENT_NAME = 'Sample Event';
public const string EVENT_ID = "1";
public const string EVENT_NAME = "Sample Event";
public const string EVENT_INSTANCE_ID = "1";
public const string TAG_GROUP_ID = "1";
public const string TAG_NAME = "Demo Tag";
Expand All @@ -33,15 +33,16 @@ public static function setup(): void
self::useSpecificEventInstance();
self::useTagGroupCollection();
self::useSpecificTagGroup();
self::useEventTagRelationshipCollection();
}

public static function useEventCollection(): void
{
HttpClient::fake([
self::HOSTNAME . Event::EVENT_ENDPOINT => function ($request) {
return match ($request->method()) {
'POST' => HttpClient::response(self::useSingleResponse(ObjectType::Event)),
'GET' => HttpClient::response(self::useCollectionResponse(ObjectType::Event)),
"POST" => HttpClient::response(self::useSingleResponse(ObjectType::Event)),
"GET" => HttpClient::response(self::useCollectionResponse(ObjectType::Event)),
default => HttpClient::response([], 405),
};
},
Expand All @@ -51,10 +52,10 @@ public static function useEventCollection(): void
public static function useSpecificEvent(): void
{
HttpClient::fake([
self::HOSTNAME . Event::EVENT_ENDPOINT . '/1' => function ($request) {
self::HOSTNAME . Event::EVENT_ENDPOINT . "/1" => function ($request) {
return match ($request->method()) {
'PUT', 'PATCH', 'GET' => HttpClient::response(self::useSingleResponse(ObjectType::Event)),
'DELETE' => HttpClient::response(self::deleteResponse()),
"PUT", "PATCH", "GET" => HttpClient::response(self::useSingleResponse(ObjectType::Event)),
"DELETE" => HttpClient::response(self::deleteResponse()),
default => HttpClient::response([], 405),
};
},
Expand All @@ -66,7 +67,7 @@ public static function useEventInstanceCollection(): void
HttpClient::fake([
self::HOSTNAME . Event::EVENT_ENDPOINT . "/1/event_instances" => function ($request) {
return match ($request->method()) {
'GET' => HttpClient::response(self::useCollectionResponse(ObjectType::EventInstance)),
"GET" => HttpClient::response(self::useCollectionResponse(ObjectType::EventInstance)),
default => HttpClient::response([], 405),
};
},
Expand All @@ -76,9 +77,9 @@ public static function useEventInstanceCollection(): void
public static function useSpecificEventInstance(): void
{
HttpClient::fake([
self::HOSTNAME . EventInstance::EVENT_INSTANCE_ENDPOINT . '/1' => function ($request) {
self::HOSTNAME . EventInstance::EVENT_INSTANCE_ENDPOINT . "/1" => function ($request) {
return match ($request->method()) {
'GET' => HttpClient::response(self::useSingleResponse(ObjectType::EventInstance)),
"GET" => HttpClient::response(self::useSingleResponse(ObjectType::EventInstance)),
default => HttpClient::response([], 405),
};
},
Expand All @@ -90,7 +91,7 @@ public static function useTagGroupCollection(): void
HttpClient::fake([
self::HOSTNAME . TagGroup::TAG_GROUP_ENDPOINT => function ($request) {
return match ($request->method()) {
'GET' => HttpClient::response(self::useCollectionResponse(ObjectType::TagGroup)),
"GET" => HttpClient::response(self::useCollectionResponse(ObjectType::TagGroup)),
default => HttpClient::response([], 405),
};
},
Expand All @@ -100,9 +101,21 @@ public static function useTagGroupCollection(): void
public static function useSpecificTagGroup(): void
{
HttpClient::fake([
self::HOSTNAME . TagGroup::TAG_GROUP_ENDPOINT . '/1/tags' => function ($request) {
self::HOSTNAME . TagGroup::TAG_GROUP_ENDPOINT . "/1/tags" => function ($request) {
return match ($request->method()) {
'GET' => HttpClient::response(self::useSingleResponse(ObjectType::Tag)),
"GET" => HttpClient::response(self::useSingleResponse(ObjectType::Tag)),
default => HttpClient::response([], 405),
};
},
]);
}

public static function useEventTagRelationshipCollection(): void
{
HttpClient::fake([
self::HOSTNAME . Event::EVENT_ENDPOINT . "/1/tags" => function ($request) {
return match ($request->method()) {
"GET" => HttpClient::response(self::useCollectionResponse(ObjectType::Tag)),
default => HttpClient::response([], 405),
};
},
Expand All @@ -112,27 +125,27 @@ public static function useSpecificTagGroup(): void
protected static function event(): array
{
return [
'type' => 'Event',
'id' => '1',
'attributes' => [
'approval_status' => 'string',
'created_at' => '2000-01-01T12:00:00Z',
'description' => 'string',
'featured' => true,
'image_url' => 'string',
'name' => self::EVENT_NAME,
'percent_approved' => 1,
'percent_rejected' => 1,
'registration_url' => 'string',
'summary' => 'string',
'updated_at' => '2000-01-01T12:00:00Z',
'visible_in_church_center' => true,
"type" => "Event",
"id" => "1",
"attributes" => [
"approval_status" => "string",
"created_at" => "2000-01-01T12:00:00Z",
"description" => "string",
"featured" => true,
"image_url" => "string",
"name" => self::EVENT_NAME,
"percent_approved" => 1,
"percent_rejected" => 1,
"registration_url" => "string",
"summary" => "string",
"updated_at" => "2000-01-01T12:00:00Z",
"visible_in_church_center" => true,
],
'relationships' => [
'owner' => [
'data' => [
'type' => 'Person',
'id' => '1',
"relationships" => [
"owner" => [
"data" => [
"type" => "Person",
"id" => "1",
],
],
],
Expand All @@ -142,27 +155,27 @@ protected static function event(): array
protected static function eventInstance(): array
{
return [
'type' => 'EventInstance',
'id' => '1',
'attributes' => [
'all_day_event' => true,
'compact_recurrence_description' => 'string',
'created_at' => '2000-01-01T12:00:00Z',
'ends_at' => '2000-01-01T12:00:00Z',
'location' => 'string',
'recurrence' => 'string',
'recurrence_description' => 'string',
'starts_at' => '2000-01-01T12:00:00Z',
'updated_at' => '2000-01-01T12:00:00Z',
'church_center_url' => 'string',
'published_starts_at' => 'string',
'published_ends_at' => 'string',
"type" => "EventInstance",
"id" => "1",
"attributes" => [
"all_day_event" => true,
"compact_recurrence_description" => "string",
"created_at" => "2000-01-01T12:00:00Z",
"ends_at" => "2000-01-01T12:00:00Z",
"location" => "string",
"recurrence" => "string",
"recurrence_description" => "string",
"starts_at" => "2000-01-01T12:00:00Z",
"updated_at" => "2000-01-01T12:00:00Z",
"church_center_url" => "string",
"published_starts_at" => "string",
"published_ends_at" => "string",
],
'relationships' => [
'event' => [
'data' => [
'type' => 'Event',
'id' => '1',
"relationships" => [
"event" => [
"data" => [
"type" => "Event",
"id" => "1",
],
],
],
Expand All @@ -172,15 +185,15 @@ protected static function eventInstance(): array
protected static function tagGroup(): array
{
return [
'type' => 'TagGroup',
'id' => '1',
'attributes' => [
'created_at' => '2000-01-01T12:00:00Z',
'name' => 'Demo Tag Group',
'updated_at' => '2000-01-01T12:00:00Z',
'required' => true,
"type" => "TagGroup",
"id" => "1",
"attributes" => [
"created_at" => "2000-01-01T12:00:00Z",
"name" => "Demo Tag Group",
"updated_at" => "2000-01-01T12:00:00Z",
"required" => true,
],
'relationships' => [],
"relationships" => [],
];
}

Expand Down
Loading

0 comments on commit 2099bd9

Please sign in to comment.