Skip to content

Commit

Permalink
Implement Misc endpoints (#29)
Browse files Browse the repository at this point in the history
- Standard implementation as other endpoints
- Fix incorrect test case names
- Remove ExampleTest.php
  • Loading branch information
rapkis authored Sep 19, 2023
1 parent 630d9bd commit b635f1b
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/ControlD.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rapkis\Controld\Resources\Account;
use Rapkis\Controld\Resources\Analytics;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Resources\Misc;
use Rapkis\Controld\Resources\MobileConfig;
use Rapkis\Controld\Resources\Organizations;
use Rapkis\Controld\Resources\Profiles;
Expand Down Expand Up @@ -59,4 +60,9 @@ public function mobileConfig(): MobileConfig
{
return app(MobileConfig::class, ['client' => $this->request]);
}

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

declare(strict_types=1);

namespace Rapkis\Controld\Entities;

class Network
{
public function __construct(
public readonly string $iataCode,
public readonly string $city,
public readonly string $country,
public readonly float $gpsLat,
public readonly float $gpsLong,
public readonly int $apiStatus,
public readonly int $dnsStatus,
public readonly int $proxyStatus,
) {
}
}
22 changes: 22 additions & 0 deletions src/Factories/DatacenterIpFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Factories;

use Rapkis\Controld\Contracts\Factories\Factory;
use Rapkis\Controld\Responses\DatacenterIp;

class DatacenterIpFactory implements Factory
{
public function make(array $data): DatacenterIp
{
return new DatacenterIp(
ip: $data['ip'],
type: $data['type'],
organization: $data['org'],
country: $data['country'],
handler: $data['handler'],
);
}
}
23 changes: 23 additions & 0 deletions src/Factories/NetworkFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rapkis\Controld\Factories;

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

class NetworkFactory implements Factory
{
public function make(array $data): Network
{
return new Network(
iataCode: $data['iata_code'],
city: $data['city_name'],
country: $data['country_name'],
gpsLat: (float) $data['location']['lat'],
gpsLong: (float) $data['location']['long'],
apiStatus: (int) $data['status']['api'],
dnsStatus: (int) $data['status']['dns'],
proxyStatus: (int) $data['status']['pxy'],
);
}
}
40 changes: 40 additions & 0 deletions src/Resources/Misc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Resources;

use Illuminate\Http\Client\PendingRequest;
use Rapkis\Controld\Factories\DatacenterIpFactory;
use Rapkis\Controld\Factories\NetworkFactory;
use Rapkis\Controld\Responses\DatacenterIp;
use Rapkis\Controld\Responses\Networks;

class Misc
{
public function __construct(
private readonly PendingRequest $client,
private readonly DatacenterIpFactory $ip,
private readonly NetworkFactory $network,
) {
}

public function ip(): DatacenterIp
{
return $this->ip->make($this->client->get('ip')->json('body'));
}

public function networkStats()
{
$response = $this->client->get('network')->json('body');

$result = new Networks(time: $response['time'], currentPop: $response['current_pop']);

foreach ($response['network'] as $network) {
$network = $this->network->make($network);
$result->put($network->iataCode, $network);
}

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

declare(strict_types=1);

namespace Rapkis\Controld\Responses;

class DatacenterIp
{
public function __construct(
public readonly string $ip,
public readonly string $type,
public readonly string $organization,
public readonly string $country,
public readonly string $handler,
) {
}
}
15 changes: 15 additions & 0 deletions src/Responses/Networks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Responses;

use Illuminate\Support\Collection;

class Networks extends Collection
{
public function __construct(public readonly int $time, public readonly string $currentPop, $items = [])
{
parent::__construct($items);
}
}
6 changes: 6 additions & 0 deletions tests/ControlDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rapkis\Controld\Resources\Account;
use Rapkis\Controld\Resources\Analytics;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Resources\Misc;
use Rapkis\Controld\Resources\MobileConfig;
use Rapkis\Controld\Resources\Organizations;
use Rapkis\Controld\Resources\Profiles;
Expand Down Expand Up @@ -52,3 +53,8 @@
$client = new ControlD($this->createStub(PendingRequest::class));
expect($client->mobileConfig())->toBeInstanceOf(MobileConfig::class);
});

it('accesses misc resource', function () {
$client = new ControlD($this->createStub(PendingRequest::class));
expect($client->misc())->toBeInstanceOf(Misc::class);
});
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

27 changes: 27 additions & 0 deletions tests/Factories/DatacenterIpFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Rapkis\Controld\Factories\DatacenterIpFactory;
use Rapkis\Controld\Responses\DatacenterIp;

it('builds a data center ip', function (array $data, DatacenterIp $expected) {
expect((new DatacenterIpFactory())->make($data))->toEqual($expected);
})->with([
[
[
'ip' => '66.207.0.0',
'type' => 'v4',
'org' => 'Beanfield Technologies',
'country' => 'CA',
'handler' => 'dva-h01',
],
new DatacenterIp(
ip: '66.207.0.0',
type: 'v4',
organization: 'Beanfield Technologies',
country: 'CA',
handler: 'dva-h01',
),
],
]);
2 changes: 1 addition & 1 deletion tests/Factories/LearnedIpFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Rapkis\Controld\Entities\LearnedIp;
use Rapkis\Controld\Factories\LearnedIpFactory;

it('builds an action', function (array $data, LearnedIp $expected) {
it('builds a learned ip', function (array $data, LearnedIp $expected) {
expect((new LearnedIpFactory())->make($data))->toEqual($expected);
})->with([
[
Expand Down
37 changes: 37 additions & 0 deletions tests/Factories/NetworkFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Rapkis\Controld\Entities\Network;
use Rapkis\Controld\Factories\NetworkFactory;

it('builds a network', function (array $data, Network $expected) {
expect((new NetworkFactory())->make($data))->toEqual($expected);
})->with([
[
[
'iata_code' => 'SYD',
'city_name' => 'Sydney',
'country_name' => 'AU',
'location' => [
'lat' => -111.1111,
'long' => 222.2222,
],
'status' => [
'api' => 1,
'dns' => 0,
'pxy' => -1,
],
],
new Network(
iataCode: 'SYD',
city: 'Sydney',
country: 'AU',
gpsLat: -111.1111,
gpsLong: 222.2222,
apiStatus: 1,
dnsStatus: 0,
proxyStatus: -1,
),
],
]);
10 changes: 10 additions & 0 deletions tests/Mocks/Endpoints/misc-ip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"body": {
"ip": "66.207.0.0",
"type": "v4",
"org": "Beanfield Technologies",
"country": "CA",
"handler": "dva-h01"
},
"success": true
}
79 changes: 79 additions & 0 deletions tests/Mocks/Endpoints/misc-network.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"body": {
"network": [
{
"iata_code": "AMS",
"city_name": "Amsterdam",
"country_name": "NL",
"location": {
"lat": 52.377956,
"long": 4.89707
},
"status": {
"api": 1,
"dns": 1,
"pxy": -1
}
},
{
"iata_code": "CDG",
"city_name": "Paris",
"country_name": "FR",
"location": {
"lat": 48.81340913389218,
"long": 2.5556617457046547
},
"status": {
"api": 1,
"dns": 1,
"pxy": -1
}
},
{
"iata_code": "CGH",
"city_name": "São Paulo",
"country_name": "BR",
"location": {
"lat": -23.72876040612837,
"long": -46.48397842634525
},
"status": {
"api": 1,
"dns": 1,
"pxy": 1
}
},
{
"iata_code": "CHI",
"city_name": "Chicago",
"country_name": "US",
"location": {
"lat": 41.855159216729724,
"long": -87.67313302781018
},
"status": {
"api": 1,
"dns": 1,
"pxy": -1
}
},
{
"iata_code": "DAL",
"city_name": "Dallas",
"country_name": "US",
"location": {
"lat": 32.83519150465354,
"long": -96.66913588349176
},
"status": {
"api": 1,
"dns": 1,
"pxy": -1
}
}
],
"time": 1695135064,
"current_pop": "IAD"
},
"success": true
}
49 changes: 49 additions & 0 deletions tests/Resources/MiscTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Http;
use Rapkis\Controld\Factories\DatacenterIpFactory;
use Rapkis\Controld\Factories\NetworkFactory;
use Rapkis\Controld\Resources\Misc;
use Rapkis\Controld\Responses\DatacenterIp;
use Rapkis\Controld\Responses\Networks;

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

it('shows datacenter ip', function () {
$request = Http::fake([
'ip' => Http::response(mockJsonEndpoint('misc-ip')),
])->asJson();

$resource = new Misc(
$request,
app(DatacenterIpFactory::class),
$this->createStub(NetworkFactory::class),
);

$result = $resource->ip();

expect($result)->toBeInstanceOf(DatacenterIp::class);
});

it('lists network statuses', function () {
$request = Http::fake([
'network' => Http::response(mockJsonEndpoint('misc-network')),
])->asJson();

$resource = new Misc(
$request,
$this->createStub(DatacenterIpFactory::class),
app(NetworkFactory::class),
);

$result = $resource->networkStats();

expect($result)->toBeInstanceOf(Networks::class)
->and($result)->toHaveCount(5)
->and($result->time)->toBe(1695135064)
->and($result->currentPop)->toBe('IAD');
});

0 comments on commit b635f1b

Please sign in to comment.