From b635f1bab6bf346c0e427e71e9215c86f5e20141 Mon Sep 17 00:00:00 2001 From: Rapolas Gruzdys Date: Tue, 19 Sep 2023 18:16:43 +0300 Subject: [PATCH] Implement Misc endpoints (#29) - Standard implementation as other endpoints - Fix incorrect test case names - Remove ExampleTest.php --- src/ControlD.php | 6 ++ src/Entities/Network.php | 20 ++++++ src/Factories/DatacenterIpFactory.php | 22 ++++++ src/Factories/NetworkFactory.php | 23 ++++++ src/Resources/Misc.php | 40 +++++++++++ src/Responses/DatacenterIp.php | 17 +++++ src/Responses/Networks.php | 15 ++++ tests/ControlDTest.php | 6 ++ tests/ExampleTest.php | 5 -- tests/Factories/DatacenterIpFactoryTest.php | 27 +++++++ tests/Factories/LearnedIpFactoryTest.php | 2 +- tests/Factories/NetworkFactoryTest.php | 37 ++++++++++ tests/Mocks/Endpoints/misc-ip.json | 10 +++ tests/Mocks/Endpoints/misc-network.json | 79 +++++++++++++++++++++ tests/Resources/MiscTest.php | 49 +++++++++++++ 15 files changed, 352 insertions(+), 6 deletions(-) create mode 100644 src/Entities/Network.php create mode 100644 src/Factories/DatacenterIpFactory.php create mode 100644 src/Factories/NetworkFactory.php create mode 100644 src/Resources/Misc.php create mode 100644 src/Responses/DatacenterIp.php create mode 100644 src/Responses/Networks.php delete mode 100644 tests/ExampleTest.php create mode 100644 tests/Factories/DatacenterIpFactoryTest.php create mode 100644 tests/Factories/NetworkFactoryTest.php create mode 100644 tests/Mocks/Endpoints/misc-ip.json create mode 100644 tests/Mocks/Endpoints/misc-network.json create mode 100644 tests/Resources/MiscTest.php diff --git a/src/ControlD.php b/src/ControlD.php index 11baaf3..f4d40a8 100644 --- a/src/ControlD.php +++ b/src/ControlD.php @@ -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; @@ -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]); + } } diff --git a/src/Entities/Network.php b/src/Entities/Network.php new file mode 100644 index 0000000..968a54f --- /dev/null +++ b/src/Entities/Network.php @@ -0,0 +1,20 @@ +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; + } +} diff --git a/src/Responses/DatacenterIp.php b/src/Responses/DatacenterIp.php new file mode 100644 index 0000000..b98f91e --- /dev/null +++ b/src/Responses/DatacenterIp.php @@ -0,0 +1,17 @@ +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); +}); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 5d36321..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Factories/DatacenterIpFactoryTest.php b/tests/Factories/DatacenterIpFactoryTest.php new file mode 100644 index 0000000..a489e6e --- /dev/null +++ b/tests/Factories/DatacenterIpFactoryTest.php @@ -0,0 +1,27 @@ +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', + ), + ], +]); diff --git a/tests/Factories/LearnedIpFactoryTest.php b/tests/Factories/LearnedIpFactoryTest.php index 0e04165..5ae72ab 100644 --- a/tests/Factories/LearnedIpFactoryTest.php +++ b/tests/Factories/LearnedIpFactoryTest.php @@ -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([ [ diff --git a/tests/Factories/NetworkFactoryTest.php b/tests/Factories/NetworkFactoryTest.php new file mode 100644 index 0000000..c323e3e --- /dev/null +++ b/tests/Factories/NetworkFactoryTest.php @@ -0,0 +1,37 @@ +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, + ), + ], +]); diff --git a/tests/Mocks/Endpoints/misc-ip.json b/tests/Mocks/Endpoints/misc-ip.json new file mode 100644 index 0000000..21b9b46 --- /dev/null +++ b/tests/Mocks/Endpoints/misc-ip.json @@ -0,0 +1,10 @@ +{ + "body": { + "ip": "66.207.0.0", + "type": "v4", + "org": "Beanfield Technologies", + "country": "CA", + "handler": "dva-h01" + }, + "success": true +} diff --git a/tests/Mocks/Endpoints/misc-network.json b/tests/Mocks/Endpoints/misc-network.json new file mode 100644 index 0000000..d7d33b6 --- /dev/null +++ b/tests/Mocks/Endpoints/misc-network.json @@ -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 +} diff --git a/tests/Resources/MiscTest.php b/tests/Resources/MiscTest.php new file mode 100644 index 0000000..e51ab23 --- /dev/null +++ b/tests/Resources/MiscTest.php @@ -0,0 +1,49 @@ + 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'); +});