Skip to content

Commit

Permalink
Implement Service endpoints (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
rapkis authored Sep 18, 2023
1 parent 93c4a53 commit 8a80033
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ControlD.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rapkis\Controld\Resources\Access;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Resources\Profiles;
use Rapkis\Controld\Resources\Services;

class ControlD
{
Expand All @@ -29,4 +30,9 @@ public function access(): Access
{
return app(Access::class, ['client' => $this->request]);
}

public function services(): Services
{
return app(Services::class, ['client' => $this->request]);
}
}
16 changes: 16 additions & 0 deletions src/Entities/ServiceCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Entities;

class ServiceCategory
{
public function __construct(
public readonly string $pk,
public readonly string $name,
public readonly string $description,
public readonly int $count,
) {
}
}
21 changes: 21 additions & 0 deletions src/Factories/ServiceCategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Factories;

use Rapkis\Controld\Contracts\Factories\Factory;
use Rapkis\Controld\Entities\ServiceCategory;

class ServiceCategoryFactory implements Factory
{
public function make(array $data): ServiceCategory
{
return new ServiceCategory(
pk: $data['PK'],
name: $data['name'],
description: $data['description'],
count: (int) $data['count'],
);
}
}
48 changes: 48 additions & 0 deletions src/Resources/Services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Resources;

use Illuminate\Http\Client\PendingRequest;
use Rapkis\Controld\Factories\ServiceCategoryFactory;
use Rapkis\Controld\Factories\ServiceFactory;
use Rapkis\Controld\Responses\ServiceCategories;

class Services
{
public function __construct(
private readonly PendingRequest $client,
private readonly ServiceCategoryFactory $category,
private readonly ServiceFactory $service,
) {
}

public function categories(): ServiceCategories
{
$response = $this->client->get('services/categories')->json('body.categories');

$result = new ServiceCategories();

foreach ($response as $category) {
$category = $this->category->make($category);
$result->put($category->pk, $category);
}

return $result;
}

public function services(string $categoryPk): \Rapkis\Controld\Responses\Services
{
$response = $this->client->get("services/categories/{$categoryPk}")->json('body.services');

$result = new \Rapkis\Controld\Responses\Services();

foreach ($response as $service) {
$service = $this->service->make($service);
$result[$service->pk] = $service;
}

return $result;
}
}
9 changes: 9 additions & 0 deletions src/Responses/ServiceCategories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rapkis\Controld\Responses;

use Illuminate\Support\Collection;

class ServiceCategories extends Collection
{
}
6 changes: 6 additions & 0 deletions tests/ControlDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rapkis\Controld\Resources\Access;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Resources\Profiles;
use Rapkis\Controld\Resources\Services;

it('accesses profiles resource', function () {
$client = new ControlD($this->createStub(PendingRequest::class));
Expand All @@ -22,3 +23,8 @@
$client = new ControlD($this->createStub(PendingRequest::class));
expect($client->access())->toBeInstanceOf(Access::class);
});

it('accesses services resource', function () {
$client = new ControlD($this->createStub(PendingRequest::class));
expect($client->services())->toBeInstanceOf(Services::class);
});
25 changes: 25 additions & 0 deletions tests/Factories/ServiceCategoryFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Rapkis\Controld\Entities\ServiceCategory;
use Rapkis\Controld\Factories\ServiceCategoryFactory;

it('builds a service category', function (array $data, ServiceCategory $expected) {
expect((new ServiceCategoryFactory())->make($data))->toEqual($expected);
})->with([
[
[
'PK' => 'audio',
'name' => 'Audio',
'description' => 'Description',
'count' => 15,
],
new ServiceCategory(
pk: 'audio',
name: 'Audio',
description: 'Description',
count: 15,
),
],
]);
43 changes: 43 additions & 0 deletions tests/Mocks/Endpoints/services-categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"body": {
"categories": [
{
"PK": "audio",
"name": "Audio",
"description": "Audio streaming services and radio stations",
"count": 15
},
{
"PK": "gaming",
"name": "Gaming",
"description": "Gaming services and individual online games",
"count": 19
},
{
"PK": "shop",
"name": "Shop",
"description": "Shopping and auction websites",
"count": 16
},
{
"PK": "social",
"name": "Social",
"description": "Social networks and messaging tools",
"count": 29
},
{
"PK": "tools",
"name": "Tools",
"description": "Productivity tools and knowledge bases",
"count": 33
},
{
"PK": "video",
"name": "Video",
"description": "Video streaming services and live TV channels",
"count": 293
}
]
},
"success": true
}
106 changes: 106 additions & 0 deletions tests/Mocks/Endpoints/services-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"body": {
"services": [
{
"category": "audio",
"name": "Apple Music",
"warning": "This will also disable the App Store and iTunes. ",
"unlock_location": "DFW",
"PK": "applemusic"
},
{
"warning": "",
"category": "audio",
"name": "Audacy",
"unlock_location": "JFK",
"PK": "audacy"
},
{
"warning": "",
"category": "audio",
"name": "Audible",
"unlock_location": "JFK",
"PK": "audible"
},
{
"category": "audio",
"name": "Deezer",
"unlock_location": "DFW",
"PK": "deezer"
},
{
"category": "audio",
"name": "JOOX",
"unlock_location": "HKG",
"PK": "joox"
},
{
"warning": "",
"category": "audio",
"name": "Last.fm",
"unlock_location": "JFK",
"PK": "lastfm"
},
{
"warning": "",
"category": "audio",
"name": "Napster",
"unlock_location": "JFK",
"PK": "napster"
},
{
"category": "audio",
"name": "Pandora",
"unlock_location": "DFW",
"PK": "pandora"
},
{
"warning": "",
"category": "audio",
"name": "Shazam",
"unlock_location": "JFK",
"PK": "shazam"
},
{
"warning": "",
"category": "audio",
"name": "SiriusXM",
"unlock_location": "JFK",
"PK": "siriusxm"
},
{
"warning": "",
"category": "audio",
"name": "Sonos",
"unlock_location": "JFK",
"PK": "sonos"
},
{
"category": "audio",
"name": "Soundcloud",
"unlock_location": "DFW",
"PK": "soundcloud"
},
{
"name": "Spotify",
"unlock_location": "DFW",
"category": "audio",
"PK": "spotify"
},
{
"category": "audio",
"name": "Tidal",
"unlock_location": "DFW",
"PK": "tidal"
},
{
"warning": "",
"category": "audio",
"name": "TuneIn",
"unlock_location": "JFK",
"PK": "tunein"
}
]
},
"success": true
}
47 changes: 47 additions & 0 deletions tests/Resources/ServicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Http;
use Rapkis\Controld\Factories\ServiceCategoryFactory;
use Rapkis\Controld\Factories\ServiceFactory;
use Rapkis\Controld\Resources\Services;
use Rapkis\Controld\Responses\ServiceCategories;

beforeEach(function () {
Http::preventStrayRequests();
});

it('lists service categories', function () {
$request = Http::fake([
'services/categories' => Http::response(mockJsonEndpoint('services-categories')),
])->asJson();

$resource = new Services(
$request,
app(ServiceCategoryFactory::class),
$this->createStub(ServiceFactory::class),
);

$result = $resource->categories();

expect($result)->toBeInstanceOf(ServiceCategories::class)
->and($result)->toHaveCount(6);
});

it('lists services for category', function () {
$request = Http::fake([
'services/categories/category_pk' => Http::response(mockJsonEndpoint('services-services')),
])->asJson();

$resource = new Services(
$request,
$this->createStub(ServiceCategoryFactory::class),
app(ServiceFactory::class),
);

$result = $resource->services('category_pk');

expect($result)->toBeInstanceOf(\Rapkis\Controld\Responses\Services::class)
->and($result)->toHaveCount(15);
});

0 comments on commit 8a80033

Please sign in to comment.