diff --git a/src/Endpoints/AccessRules.php b/src/Endpoints/AccessRules.php index b2d7584d..07bc31c2 100644 --- a/src/Endpoints/AccessRules.php +++ b/src/Endpoints/AccessRules.php @@ -4,9 +4,12 @@ use Cloudflare\API\Adapter\Adapter; use Cloudflare\API\Configurations\Configurations; +use Cloudflare\API\Traits\BodyAccessorTrait; class AccessRules implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -78,9 +81,9 @@ public function listRules( } $data = $this->adapter->get('zones/' . $zoneID . '/firewall/access_rules/rules', $query); - $body = json_decode($data->getBody()); + $this->body = json_decode($data->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function createRule( @@ -100,9 +103,9 @@ public function createRule( $query = $this->adapter->post('zones/' . $zoneID . '/firewall/access_rules/rules', $options); - $body = json_decode($query->getBody()); + $this->body = json_decode($query->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -125,9 +128,9 @@ public function updateRule( $query = $this->adapter->patch('zones/' . $zoneID . '/firewall/access_rules/rules/' . $ruleID, $options); - $body = json_decode($query->getBody()); + $this->body = json_decode($query->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -142,9 +145,9 @@ public function deleteRule(string $zoneID, string $ruleID, string $cascade = 'no $data = $this->adapter->delete('zones/' . $zoneID . '/firewall/access_rules/rules/' . $ruleID, $options); - $body = json_decode($data->getBody()); + $this->body = json_decode($data->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 4b37d1ef..4af5c9ee 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class CustomHostnames implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -40,8 +43,8 @@ public function addHostname(string $zoneID, string $hostname, string $sslMethod ]; $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); - $body = json_decode($zone->getBody()); - return $body->result; + $this->body = json_decode($zone->getBody()); + return $this->body->result; } /** @@ -88,9 +91,9 @@ public function listHostnames( } $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query); - $body = json_decode($zone->getBody()); + $this->body = json_decode($zone->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } /** @@ -101,9 +104,9 @@ public function listHostnames( public function getHostname(string $zoneID, string $hostnameID) { $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID); - $body = json_decode($zone->getBody()); + $this->body = json_decode($zone->getBody()); - return $body->result; + return $this->body->result; } /** @@ -132,8 +135,8 @@ public function updateHostname(string $zoneID, string $hostnameID, string $sslMe ]; $zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, $options); - $body = json_decode($zone->getBody()); - return $body->result; + $this->body = json_decode($zone->getBody()); + return $this->body->result; } /** @@ -144,7 +147,7 @@ public function updateHostname(string $zoneID, string $hostnameID, string $sslMe public function deleteHostname(string $zoneID, string $hostnameID): \stdClass { $zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID); - $body = json_decode($zone->getBody()); - return $body; + $this->body = json_decode($zone->getBody()); + return $this->body; } } diff --git a/src/Endpoints/DNS.php b/src/Endpoints/DNS.php index 6bc6ad65..d06116f5 100644 --- a/src/Endpoints/DNS.php +++ b/src/Endpoints/DNS.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class DNS implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -63,9 +66,9 @@ public function addRecord( $user = $this->adapter->post('zones/' . $zoneID . '/dns_records', $options); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -110,31 +113,32 @@ public function listRecords( } $user = $this->adapter->get('zones/' . $zoneID . '/dns_records', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function getRecordDetails(string $zoneID, string $recordID): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/dns_records/' . $recordID); - $body = json_decode($user->getBody()); - return $body->result; + $this->body = json_decode($user->getBody()); + return $this->body->result; } public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass { $response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, $details); - return json_decode($response->getBody()); + $this->body = json_decode($response->getBody()); + return $this->body; } public function deleteRecord(string $zoneID, string $recordID): bool { $user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Endpoints/IPs.php b/src/Endpoints/IPs.php index 46b47651..16ad2fef 100644 --- a/src/Endpoints/IPs.php +++ b/src/Endpoints/IPs.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class IPs implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -22,8 +25,8 @@ public function __construct(Adapter $adapter) public function listIPs(): \stdClass { $ips = $this->adapter->get('ips'); - $body = json_decode($ips->getBody()); + $this->body = json_decode($ips->getBody()); - return $body->result; + return $this->body->result; } } diff --git a/src/Endpoints/PageRules.php b/src/Endpoints/PageRules.php index 43f2b3ae..3e56007a 100644 --- a/src/Endpoints/PageRules.php +++ b/src/Endpoints/PageRules.php @@ -11,9 +11,12 @@ use Cloudflare\API\Adapter\Adapter; use Cloudflare\API\Configurations\PageRulesActions; use Cloudflare\API\Configurations\PageRulesTargets; +use Cloudflare\API\Traits\BodyAccessorTrait; class PageRules implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -54,9 +57,9 @@ public function createPageRule( $query = $this->adapter->post('zones/' . $zoneID . '/pagerules', $options); - $body = json_decode($query->getBody()); + $this->body = json_decode($query->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -94,16 +97,16 @@ public function listPageRules( ]; $user = $this->adapter->get('zones/' . $zoneID . '/pagerules', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function getPageRuleDetails(string $zoneID, string $ruleID): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/pagerules/' . $ruleID); - $body = json_decode($user->getBody()); - return $body->result; + $this->body = json_decode($user->getBody()); + return $this->body->result; } public function updatePageRule( @@ -134,9 +137,9 @@ public function updatePageRule( $query = $this->adapter->patch('zones/' . $zoneID . '/pagerules', $options); - $body = json_decode($query->getBody()); + $this->body = json_decode($query->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -147,9 +150,9 @@ public function deletePageRule(string $zoneID, string $ruleID): bool { $user = $this->adapter->delete('zones/' . $zoneID . '/pagerules/' . $ruleID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Endpoints/Railgun.php b/src/Endpoints/Railgun.php index 7272d691..8ba31820 100644 --- a/src/Endpoints/Railgun.php +++ b/src/Endpoints/Railgun.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class Railgun implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -27,9 +30,9 @@ public function create( ]; $user = $this->adapter->post('railguns', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body; + return $this->body; } public function list( @@ -47,27 +50,27 @@ public function list( } $user = $this->adapter->get('railguns', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function get( string $railgunID ): \stdClass { $user = $this->adapter->get('railguns/' . $railgunID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function getZones( string $railgunID ): \stdClass { $user = $this->adapter->get('railguns/' . $railgunID . '/zones'); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function update( @@ -79,18 +82,18 @@ public function update( ]; $user = $this->adapter->patch('railguns/' . $railgunID, $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function delete( string $railgunID ): bool { $user = $this->adapter->delete('railguns/' . $railgunID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Endpoints/UARules.php b/src/Endpoints/UARules.php index 2284c0ab..8c7bfd9c 100644 --- a/src/Endpoints/UARules.php +++ b/src/Endpoints/UARules.php @@ -10,9 +10,12 @@ use Cloudflare\API\Configurations\Configurations; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class UARules implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -31,9 +34,9 @@ public function listRules( ]; $user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function createRule( @@ -58,9 +61,9 @@ public function createRule( $user = $this->adapter->post('zones/' . $zoneID . '/firewall/ua_rules', $options); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -70,8 +73,8 @@ public function createRule( public function getRuleDetails(string $zoneID, string $blockID): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/firewall/ua_rules/' . $blockID); - $body = json_decode($user->getBody()); - return $body->result; + $this->body = json_decode($user->getBody()); + return $this->body->result; } public function updateRule( @@ -93,9 +96,9 @@ public function updateRule( $user = $this->adapter->put('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID, $options); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -106,9 +109,9 @@ public function deleteRule(string $zoneID, string $ruleID): bool { $user = $this->adapter->delete('zones/' . $zoneID . '/firewall/ua_rules/' . $ruleID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Endpoints/User.php b/src/Endpoints/User.php index 02b9a8a1..93d38267 100644 --- a/src/Endpoints/User.php +++ b/src/Endpoints/User.php @@ -8,9 +8,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class User implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -21,8 +24,8 @@ public function __construct(Adapter $adapter) public function getUserDetails(): \stdClass { $user = $this->adapter->get('user'); - $body = json_decode($user->getBody()); - return $body->result; + $this->body = json_decode($user->getBody()); + return $this->body->result; } public function getUserID(): string @@ -38,6 +41,7 @@ public function getUserEmail(): string public function updateUserDetails(array $details): \stdClass { $response = $this->adapter->patch('user', $details); - return json_decode($response->getBody()); + $this->body = json_decode($response->getBody()); + return $this->body; } } diff --git a/src/Endpoints/WAF.php b/src/Endpoints/WAF.php index 2088940c..b1e3611f 100644 --- a/src/Endpoints/WAF.php +++ b/src/Endpoints/WAF.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class WAF implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -42,9 +45,9 @@ public function getPackages( } $user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } @@ -53,9 +56,9 @@ public function getPackageInfo( string $packageID ): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function getRules( @@ -81,9 +84,9 @@ public function getRules( $query['direction'] = $direction; } $user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function getRuleInfo( @@ -92,9 +95,9 @@ public function getRuleInfo( string $ruleID ): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function updateRule( @@ -111,9 +114,9 @@ public function updateRule( 'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/rules/' . $ruleID, $query ); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function getGroups( @@ -143,9 +146,9 @@ public function getGroups( 'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups', $query ); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function getGroupInfo( @@ -154,9 +157,9 @@ public function getGroupInfo( string $groupID ): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } public function updateGroup( @@ -173,8 +176,8 @@ public function updateGroup( 'zones/' . $zoneID . '/firewall/waf/packages/' . $packageID . '/groups/' . $groupID, $query ); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return $body->result; + return $this->body->result; } } diff --git a/src/Endpoints/ZoneLockdown.php b/src/Endpoints/ZoneLockdown.php index e42cde2d..aeccb254 100644 --- a/src/Endpoints/ZoneLockdown.php +++ b/src/Endpoints/ZoneLockdown.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class ZoneLockdown implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -30,9 +33,9 @@ public function listLockdowns( ]; $user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function createLockdown( @@ -57,9 +60,9 @@ public function createLockdown( $user = $this->adapter->post('zones/' . $zoneID . '/firewall/lockdowns', $options); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -69,8 +72,8 @@ public function createLockdown( public function getLockdownDetails(string $zoneID, string $lockdownID): \stdClass { $user = $this->adapter->get('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID); - $body = json_decode($user->getBody()); - return $body->result; + $this->body = json_decode($user->getBody()); + return $this->body->result; } public function updateLockdown( @@ -92,9 +95,9 @@ public function updateLockdown( $user = $this->adapter->put('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID, $options); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -105,9 +108,9 @@ public function deleteLockdown(string $zoneID, string $lockdownID): bool { $user = $this->adapter->delete('zones/' . $zoneID . '/firewall/lockdowns/' . $lockdownID); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index f0e77779..8595cb6f 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -9,9 +9,12 @@ namespace Cloudflare\API\Endpoints; use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; class Zones implements API { + use BodyAccessorTrait; + private $adapter; public function __construct(Adapter $adapter) @@ -39,16 +42,16 @@ public function addZone(string $name, bool $jumpStart = false, string $organizat } $user = $this->adapter->post('zones', $options); - $body = json_decode($user->getBody()); - return $body->result; + $this->body = json_decode($user->getBody()); + return $this->body->result; } public function activationCheck(string $zoneID): bool { $user = $this->adapter->put('zones/' . $zoneID . '/activation_check'); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -87,9 +90,9 @@ public function listZones( } $user = $this->adapter->get('zones', $query); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - return (object)['result' => $body->result, 'result_info' => $body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } public function getZoneID(string $name = ''): string @@ -116,7 +119,9 @@ public function getAnalyticsDashboard(string $zoneID, string $since = '-10080', { $response = $this->adapter->get('zones/' . $zoneID . '/analytics/dashboard', ['since' => $since, 'until' => $until, 'continuous' => var_export($continuous, true)]); - return json_decode($response->getBody())->result; + $this->body = $response->getBody(); + + return json_decode($this->body)->result; } /** @@ -130,9 +135,9 @@ public function changeDevelopmentMode(string $zoneID, bool $enable = false): boo { $response = $this->adapter->patch('zones/' . $zoneID . '/settings/development_mode', ['value' => $enable ? 'on' : 'off']); - $body = json_decode($response->getBody()); + $this->body = json_decode($response->getBody()); - if ($body->success) { + if ($this->body->success) { return true; } @@ -149,9 +154,9 @@ public function cachePurgeEverything(string $zoneID): bool { $user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } @@ -172,16 +177,16 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu if (!is_null($tags)) { $options['tags'] = $tags; } - + if (!is_null($hosts)) { $options['hosts'] = $hosts; } $user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', $options); - $body = json_decode($user->getBody()); + $this->body = json_decode($user->getBody()); - if (isset($body->result->id)) { + if (isset($this->body->result->id)) { return true; } diff --git a/src/Traits/BodyAccessorTrait.php b/src/Traits/BodyAccessorTrait.php new file mode 100644 index 00000000..add13ad3 --- /dev/null +++ b/src/Traits/BodyAccessorTrait.php @@ -0,0 +1,13 @@ +body; + } +} diff --git a/tests/Endpoints/AccessRulesTest.php b/tests/Endpoints/AccessRulesTest.php index a7fd24d9..fbe4bd48 100644 --- a/tests/Endpoints/AccessRulesTest.php +++ b/tests/Endpoints/AccessRulesTest.php @@ -28,6 +28,7 @@ public function testListRules() $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $zones->getBody()->result[0]->id); } public function testCreateRule() @@ -58,6 +59,7 @@ public function testCreateRule() $config, 'This rule is on because of an event that occured on date X' ); + $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $rules->getBody()->result->id); } public function testUpdateRule() @@ -84,6 +86,7 @@ public function testUpdateRule() 'challenge', 'This rule is on because of an event that occured on date X' ); + $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $rules->getBody()->result->id); } public function testDeleteRule() @@ -104,5 +107,6 @@ public function testDeleteRule() $rules = new \Cloudflare\API\Endpoints\AccessRules($mock); $rules->deleteRule('023e105f4ecef8ad9ca31a8372d0c353', '92f17202ed8bd63d69a66b86a49a8f6b'); + $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $rules->getBody()->result->id); } } diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index a6d9f9b6..faa247fb 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -32,6 +32,7 @@ public function testAddHostname() $hostname = new CustomHostnames($mock); $hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv'); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); } public function testListHostnames() @@ -64,6 +65,7 @@ public function testListHostnames() $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result[0]->id); } public function testGetHostname() @@ -84,6 +86,7 @@ public function testGetHostname() $this->assertObjectHasAttribute('id', $result); $this->assertObjectHasAttribute('hostname', $result); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id); } public function testUpdateHostname() @@ -110,6 +113,7 @@ public function testUpdateHostname() $this->assertObjectHasAttribute('id', $result); $this->assertObjectHasAttribute('hostname', $result); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id); } public function testDeleteHostname() @@ -129,5 +133,6 @@ public function testDeleteHostname() $result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9'); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->id); } } diff --git a/tests/Endpoints/DNSTest.php b/tests/Endpoints/DNSTest.php index d82553a5..11c21fa8 100644 --- a/tests/Endpoints/DNSTest.php +++ b/tests/Endpoints/DNSTest.php @@ -63,6 +63,7 @@ public function testListRecords() $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id); } public function testGetDNSRecordDetails() @@ -82,6 +83,7 @@ public function testGetDNSRecordDetails() $result = $dns->getRecordDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59'); $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $dns->getBody()->result->id); } public function testUpdateDNSRecord() @@ -110,6 +112,7 @@ public function testUpdateDNSRecord() $result = $dns->updateRecordDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59', $details); $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result->id); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $dns->getBody()->result->id); foreach ($details as $property => $value) { $this->assertEquals($result->result->{ $property }, $value); diff --git a/tests/Endpoints/IPsTest.php b/tests/Endpoints/IPsTest.php index 27b85b5b..1f92804b 100644 --- a/tests/Endpoints/IPsTest.php +++ b/tests/Endpoints/IPsTest.php @@ -21,9 +21,11 @@ public function testListIPs() $this->equalTo('ips') ); - $ips = new \Cloudflare\API\Endpoints\IPs($mock); - $ips = $ips->listIPs(); + $ipsMock = new \Cloudflare\API\Endpoints\IPs($mock); + $ips = $ipsMock->listIPs(); $this->assertObjectHasAttribute('ipv4_cidrs', $ips); $this->assertObjectHasAttribute('ipv6_cidrs', $ips); + $this->assertObjectHasAttribute('ipv4_cidrs', $ipsMock->getBody()->result); + $this->assertObjectHasAttribute('ipv6_cidrs', $ipsMock->getBody()->result); } } diff --git a/tests/Endpoints/PageRulesTest.php b/tests/Endpoints/PageRulesTest.php index b082f60b..f2813fd0 100644 --- a/tests/Endpoints/PageRulesTest.php +++ b/tests/Endpoints/PageRulesTest.php @@ -35,6 +35,7 @@ public function testCreatePageRule() $result = $pageRules->createPageRule('023e105f4ecef8ad9ca31a8372d0c353', $target, $action, true, 1); $this->assertTrue($result); + $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id); } public function testListPageRules() @@ -58,6 +59,7 @@ public function testListPageRules() $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock); $pageRules->listPageRules('023e105f4ecef8ad9ca31a8372d0c353', 'active', 'status', 'desc', 'all'); + $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result[0]->id); } public function testGetPageRuleDetails() @@ -75,6 +77,7 @@ public function testGetPageRuleDetails() $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock); $pageRules->getPageRuleDetails('023e105f4ecef8ad9ca31a8372d0c353', '9a7806061c88ada191ed06f989cc3dac'); + $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id); } public function testUpdatePageRule() @@ -104,6 +107,7 @@ public function testUpdatePageRule() $result = $pageRules->updatePageRule('023e105f4ecef8ad9ca31a8372d0c353', $target, $action, true, 1); $this->assertTrue($result); + $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id); } public function testDeletePageRule() @@ -123,5 +127,6 @@ public function testDeletePageRule() $result = $pageRules->deletePageRule('023e105f4ecef8ad9ca31a8372d0c353', '9a7806061c88ada191ed06f989cc3dac'); $this->assertTrue($result); + $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id); } } diff --git a/tests/Endpoints/RailgunTest.php b/tests/Endpoints/RailgunTest.php index 7d56e1cf..15e210b1 100644 --- a/tests/Endpoints/RailgunTest.php +++ b/tests/Endpoints/RailgunTest.php @@ -34,6 +34,7 @@ public function testcreate() foreach ($details as $property => $value) { $this->assertEquals($result->result->{ $property }, $value); } + $this->assertEquals('e928d310693a83094309acf9ead50448', $railgun->getBody()->result->id); } public function testlist() @@ -59,6 +60,7 @@ public function testlist() $this->assertObjectHasAttribute('result', $result); $this->assertObjectHasAttribute('result_info', $result); + $this->assertEquals('e928d310693a83094309acf9ead50448', $railgun->getBody()->result[0]->id); } public function testget() @@ -78,6 +80,7 @@ public function testget() $result = $railgun->get('e928d310693a83094309acf9ead50448'); $this->assertEquals('e928d310693a83094309acf9ead50448', $result->id); + $this->assertEquals('e928d310693a83094309acf9ead50448', $railgun->getBody()->result->id); } public function testgetZones() @@ -98,6 +101,7 @@ public function testgetZones() $this->assertObjectHasAttribute('result', $result); $this->assertObjectHasAttribute('result_info', $result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $railgun->getBody()->result[0]->id); } public function testupdate() @@ -122,6 +126,7 @@ public function testupdate() $result = $waf->update('e928d310693a83094309acf9ead50448', true); $this->assertEquals('e928d310693a83094309acf9ead50448', $result->id); + $this->assertEquals('e928d310693a83094309acf9ead50448', $waf->getBody()->result->id); } public function testdelete() @@ -139,5 +144,6 @@ public function testdelete() $waf = new \Cloudflare\API\Endpoints\Railgun($mock); $waf->delete('e928d310693a83094309acf9ead50448'); + $this->assertEquals('e928d310693a83094309acf9ead50448', $waf->getBody()->result->id); } } diff --git a/tests/Endpoints/UARulesTest.php b/tests/Endpoints/UARulesTest.php index 666f17d7..fec94b51 100644 --- a/tests/Endpoints/UARulesTest.php +++ b/tests/Endpoints/UARulesTest.php @@ -33,6 +33,7 @@ public function testListRules() $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id); } public function testCreateRule() @@ -65,6 +66,7 @@ public function testCreateRule() '372e67954025e0ba6aaa6d586b9e0b59', 'Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack' ); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id); } public function getRuleDetails() @@ -84,6 +86,7 @@ public function getRuleDetails() $result = $lockdown->getRuleDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59'); $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $lockdown->getBody()->result->id); } public function testUpdateRule() @@ -116,6 +119,7 @@ public function testUpdateRule() $config, 'Restrict access to these endpoints to requests from a known IP address' ); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id); } public function testDeleteRule() @@ -133,5 +137,6 @@ public function testDeleteRule() $rules = new \Cloudflare\API\Endpoints\UARules($mock); $rules->deleteRule('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59'); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id); } } diff --git a/tests/Endpoints/UserTest.php b/tests/Endpoints/UserTest.php index e87343e7..07dc4248 100644 --- a/tests/Endpoints/UserTest.php +++ b/tests/Endpoints/UserTest.php @@ -21,6 +21,7 @@ public function testGetUserDetails() $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $details->id); $this->assertObjectHasAttribute('email', $details); $this->assertEquals('user@example.com', $details->email); + $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id); } public function testGetUserID() @@ -32,6 +33,7 @@ public function testGetUserID() $user = new \Cloudflare\API\Endpoints\User($mock); $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getUserID()); + $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id); } public function testGetUserEmail() @@ -45,6 +47,7 @@ public function testGetUserEmail() $user = new \Cloudflare\API\Endpoints\User($mock); $this->assertEquals('user@example.com', $user->getUserEmail()); + $this->assertEquals('user@example.com', $user->getBody()->result->email); } public function testUpdateUserDetails() @@ -60,5 +63,6 @@ public function testUpdateUserDetails() $user = new \Cloudflare\API\Endpoints\User($mock); $user->updateUserDetails(['email' => 'user2@example.com']); + $this->assertEquals('7c5dae5552338874e5053f2534d2767a', $user->getBody()->result->id); } } diff --git a/tests/Endpoints/WAFTest.php b/tests/Endpoints/WAFTest.php index 18057f39..f0c97aa9 100644 --- a/tests/Endpoints/WAFTest.php +++ b/tests/Endpoints/WAFTest.php @@ -36,6 +36,7 @@ public function testgetPackages() $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $waf->getBody()->result[0]->id); } public function testgetPackageInfo() @@ -55,6 +56,7 @@ public function testgetPackageInfo() $result = $waf->getPackageInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b'); $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $result->id); + $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $waf->getBody()->result->id); } public function testgetRules() @@ -85,6 +87,7 @@ public function testgetRules() $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $waf->getBody()->result[0]->id); } public function testgetRuleInfo() @@ -104,6 +107,7 @@ public function testgetRuleInfo() $result = $waf->getRuleInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'f939de3be84e66e757adcdcb87908023'); $this->assertEquals('f939de3be84e66e757adcdcb87908023', $result->id); + $this->assertEquals('f939de3be84e66e757adcdcb87908023', $waf->getBody()->result->id); } public function testupdateRule() @@ -132,6 +136,7 @@ public function testupdateRule() foreach ($details as $property => $value) { $this->assertEquals($result->{ $property }, $value); } + $this->assertEquals('f939de3be84e66e757adcdcb87908023', $waf->getBody()->result->id); } public function getGroups() @@ -162,6 +167,7 @@ public function getGroups() $this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result[0]->id); } public function testgetGroupInfo() @@ -181,6 +187,7 @@ public function testgetGroupInfo() $result = $waf->getGroupInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'de677e5818985db1285d0e80225f06e5'); $this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->id); + $this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result->id); } public function testupdateGroup() @@ -209,5 +216,6 @@ public function testupdateGroup() foreach ($details as $property => $value) { $this->assertEquals($result->{ $property }, $value); } + $this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result->id); } } diff --git a/tests/Endpoints/ZoneLockdownTest.php b/tests/Endpoints/ZoneLockdownTest.php index 599b4d6e..2af55ac6 100644 --- a/tests/Endpoints/ZoneLockdownTest.php +++ b/tests/Endpoints/ZoneLockdownTest.php @@ -33,6 +33,7 @@ public function testListLockdowns() $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id); } public function testAddLockdown() @@ -65,6 +66,7 @@ public function testAddLockdown() '372e67954025e0ba6aaa6d586b9e0b59', 'Restrict access to these endpoints to requests from a known IP address' ); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id); } public function testGetRecordDetails() @@ -84,6 +86,7 @@ public function testGetRecordDetails() $result = $lockdown->getLockdownDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59'); $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $lockdown->getBody()->result->id); } public function testUpdateLockdown() @@ -116,6 +119,7 @@ public function testUpdateLockdown() $config, 'Restrict access to these endpoints to requests from a known IP address' ); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id); } public function testDeleteLockdown() @@ -136,5 +140,6 @@ public function testDeleteLockdown() $zoneLockdown = new \Cloudflare\API\Endpoints\ZoneLockdown($mock); $zoneLockdown->deleteLockdown('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59'); + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id); } } diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 5221eb78..0bda6514 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -46,6 +46,7 @@ public function testAddZone() $zones = new \Cloudflare\API\Endpoints\Zones($mock); $zones->addZone('example.com', true, '01a7362d577a6c3019a474fd6f485823'); + $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id); } public function testActivationTest() @@ -65,6 +66,7 @@ public function testActivationTest() $result = $zones->activationCheck('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); } public function testListZones() @@ -97,6 +99,7 @@ public function testListZones() $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->result[0]->id); $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result[0]->id); } public function testGetZoneID() @@ -122,6 +125,7 @@ public function testGetZoneID() $result = $zones->getZoneID('example.com'); $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result[0]->id); } public function testGetAnalyticsDashboard() @@ -163,6 +167,7 @@ public function testChangeDevelopmentMode() $result = $zones->changeDevelopmentMode('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); $this->assertTrue($result); + $this->assertEquals('development_mode', $zones->getBody()->result->id); } public function testCachePurgeEverything() @@ -183,6 +188,7 @@ public function testCachePurgeEverything() $result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); } public function testCachePurgeHost() @@ -209,5 +215,32 @@ public function testCachePurgeHost() $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], ['dash.cloudflare.com']); $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); + } + + public function testCachePurge() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), + $this->equalTo(['files' => [ + 'https://example.com/file.jpg', + ] + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [ + 'https://example.com/file.jpg', + ]); + + $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); } } diff --git a/tests/Fixtures/Endpoints/cachePurge.json b/tests/Fixtures/Endpoints/cachePurge.json new file mode 100644 index 00000000..bedd2f5c --- /dev/null +++ b/tests/Fixtures/Endpoints/cachePurge.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "023e105f4ecef8ad9ca31a8372d0c353" + } +}