Skip to content

Commit

Permalink
feat(communities): Add endpoints for communities.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrorocker committed Jan 12, 2024
1 parent c4326bc commit 925ced3
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/CircleSo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AdroSoftware\CircleSoSdk\Contracts\InteractsWithEndpoints;
use AdroSoftware\CircleSoSdk\Endpoint\{
Community\Communities,
Me\Me,
Member\Members,
MemberTag\MemberTag,
Expand All @@ -26,6 +27,11 @@ public function me(): Me
return new Me($this);
}

public function communities(): Communities
{
return new Communities($this);
}

public function members(): Members
{
return new Members($this);
Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/InteractsWithEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AdroSoftware\CircleSoSdk\Contracts;

use AdroSoftware\CircleSoSdk\Endpoint\{
Community\Communities,
Me\Me,
Member\Members,
MemberTag\MemberTag,
Expand All @@ -15,6 +16,8 @@ interface InteractsWithEndpoints
{
public function me(): Me;

public function communities(): Communities;

public function members(): Members;

public function memberTags(): MemberTag;
Expand Down
28 changes: 28 additions & 0 deletions src/Endpoint/Community/Communities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace AdroSoftware\CircleSoSdk\Endpoint\Community;

use AdroSoftware\CircleSoSdk\Endpoint\AbstractEndpoint;
use AdroSoftware\CircleSoSdk\Endpoint\EndpointInterface;

final class Communities extends AbstractEndpoint implements EndpointInterface
{
public function all(): mixed
{
return $this->factorResponse(
$this->circleSo->getHttpClient()->get('/communities')
);
}

public function community(string $slug, ?int $communityId = null): mixed
{
$this->ensureCommunityIdIsPresent($communityId);

return $this->factorResponse(
response: $this->circleSo->getHttpClient()->get('/communities'),
throwNotFoundException: true,
);
}
}
63 changes: 63 additions & 0 deletions tests/Endpoint/Community/CommunitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace AdroSoftware\CircleSoSdk\Tests\Endpoint\Community;

use AdroSoftware\CircleSoSdk\Exception\{
ResourceNotFoundException,
UnsuccessfulResponseException,
};
use AdroSoftware\CircleSoSdk\Tests\TestCase;
use GuzzleHttp\Psr7\Response;

class CommunitiesTest extends TestCase
{
public function test_get_communities(): void
{
$circleSo = $this->getSdkWithMockedClient([
new Response(200, [], json_response('communities')),
]);

$communities = $circleSo->communities()->all();

$this->assertArrayHasKey('id', $communities[0]);

$this->assertSame(1, $communities[0]['id']);
$this->assertSame('Test community', $communities[0]['name']);
$this->assertSame('test-community', $communities[0]['slug']);
}

public function test_get_community_ok(): void
{
$circleSo = $this->getSdkWithMockedClient([
new Response(200, [], json_response('community')),
]);

$community = $circleSo->communities()
->communityId(1)
->community('test-community');

$this->assertArrayHasKey('id', $community);

$this->assertSame(1, $community['id']);
$this->assertSame('Test community', $community['name']);
$this->assertSame('test-community', $community['slug']);
}

public function test_get_community_empty(): void
{
$this->expectException(ResourceNotFoundException::class);
$this->expectExceptionMessage('The resource was not found.');
$this->expectExceptionCode(404);

$circleSo = $this->getSdkWithMockedClient([
new Response(200, [], null),
]);

$circleSo->communities()
->communityId(1)
->community('non-existent-community');

}
}
45 changes: 45 additions & 0 deletions tests/stubs/responses/communities.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"id": 1,
"name": "Test community",
"slug": "test-community",
"icon_url": "https://d2y5h3osumboay.cloudfront.net/qran10ilr2xb13ryvjhlkbm97db0",
"logo_url": null,
"dark_icon_url": null,
"dark_logo_url": null,
"owner_id": 1,
"is_private": true,
"space_ids": [
1,
2,
3,
4,
5
],
"last_visited_by_current_user": true,
"custom_tos": "https://test.com/terms",
"custom_tos_enabled": true,
"default_existing_member_space_id": 0,
"root_url": "circle.test.com",
"display_on_switcher": false,
"prefs": {

"has_posts": true,
"has_spaces": true,
"brand_color": {
"dark": "#FFFFFF",
"light": "#000000"
},
"has_seen_widget": true,
"brand_text_color": {
"dark": "#FFFFFF",
"light": "#000000"
},
"has_topics": true,
"has_completed_onboarding": true,
"has_invited_member": true,
"new_community_event_fired": true
},
"white_label": true
}
]
43 changes: 43 additions & 0 deletions tests/stubs/responses/community.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"id": 1,
"name": "Test community",
"slug": "test-community",
"icon_url": "https://d2y5h3osumboay.cloudfront.net/qran10ilr2xb13ryvjhlkbm97db0",
"logo_url": null,
"dark_icon_url": null,
"dark_logo_url": null,
"owner_id": 1,
"is_private": true,
"space_ids": [
1,
2,
3,
4,
5
],
"last_visited_by_current_user": true,
"custom_tos": "https://test.com/terms",
"custom_tos_enabled": true,
"default_existing_member_space_id": 0,
"root_url": "circle.test.com",
"display_on_switcher": false,
"prefs": {

"has_posts": true,
"has_spaces": true,
"brand_color": {
"dark": "#FFFFFF",
"light": "#000000"
},
"has_seen_widget": true,
"brand_text_color": {
"dark": "#FFFFFF",
"light": "#000000"
},
"has_topics": true,
"has_completed_onboarding": true,
"has_invited_member": true,
"new_community_event_fired": true
},
"white_label": true
}

0 comments on commit 925ced3

Please sign in to comment.