Added loadbalancers class and tests
This commit is contained in:
57
tests/Configurations/LoadBalancerTest.php
Normal file
57
tests/Configurations/LoadBalancerTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Martijn Smidt <martijn@squeezely.tech>
|
||||
* User: HemeraOne
|
||||
* Date: 13/05/2019
|
||||
*/
|
||||
|
||||
use Cloudflare\API\Configurations\ConfigurationsException;
|
||||
use Cloudflare\API\Configurations\LoadBalancer;
|
||||
|
||||
class LoadBalancerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider testArgumentsDataProvider
|
||||
*/
|
||||
public function testArguments($setFunction, $arguments, $getFunction, $invalid)
|
||||
{
|
||||
$lb = new LoadBalancer('bous', [], 'bogus');
|
||||
foreach ($arguments as $argument) {
|
||||
if ($invalid) {
|
||||
try {
|
||||
$lb->{$setFunction}($argument);
|
||||
} catch (ConfigurationsException $e) {
|
||||
$this->assertNotEquals($argument, $lb->{$getFunction}());
|
||||
}
|
||||
} else {
|
||||
$lb->{$setFunction}($argument);
|
||||
$this->assertEquals($argument, $lb->{$getFunction}());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function testArgumentsDataProvider()
|
||||
{
|
||||
return [
|
||||
'steeringPolicy arguments valid' => [
|
||||
'setSteeringPolicy', ['off', 'geo', 'random', 'dynamic_latency', ''], 'getSteeringPolicy', false
|
||||
],
|
||||
'sessionAffinity arguments valid' => [
|
||||
'setSessionAffinity', ['none', 'cookie', 'ip_cookie', ''], 'getSessionAffinity', false
|
||||
],
|
||||
'sessionAffinityTtl arguments valid' => [
|
||||
'setSessionAffinityTtl', [3600], 'getSessionAffinityTtl', false
|
||||
],
|
||||
'steeringPolicy arguments invalid' => [
|
||||
'setSteeringPolicy', ['invalid'], 'getSteeringPolicy', true
|
||||
],
|
||||
'sessionAffinity arguments invalid' => [
|
||||
'setSessionAffinity', ['invalid'], 'getSessionAffinity', true
|
||||
],
|
||||
'sessionAffinityTtl arguments invalid' => [
|
||||
'setSessionAffinityTtl', [1337], 'getSessionAffinityTtl', true
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
128
tests/Endpoints/LoadBalancersTest.php
Normal file
128
tests/Endpoints/LoadBalancersTest.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
use Cloudflare\API\Adapter\Adapter;
|
||||
use Cloudflare\API\Configurations\LoadBalancer;
|
||||
use Cloudflare\API\Endpoints\LoadBalancers;
|
||||
|
||||
/**
|
||||
* @author Martijn Smidt <martijn@squeezely.tech>
|
||||
* User: HemeraOne
|
||||
* Date: 13/05/2019
|
||||
*/
|
||||
|
||||
class LoadBalancersTest extends TestCase
|
||||
{
|
||||
public function testCreateLoadBalancer()
|
||||
{
|
||||
$pools = [
|
||||
'17b5962d775c646f3f9725cbc7a53df4',
|
||||
'9290f38c5d07c2e2f4df57b1f61d4196',
|
||||
'00920f38ce07c2e2f4df50b1f61d4194'
|
||||
];
|
||||
|
||||
$lbConfiguration = new LoadBalancer('www.example.com', $pools, '17b5962d775c646f3f9725cbc7a53df4');
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createLoadBalancer.json');
|
||||
|
||||
$mock = $this->getMockBuilder(Adapter::class)->getMock();
|
||||
$mock->method('post')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('post')
|
||||
->with(
|
||||
$this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers'),
|
||||
$lbConfiguration->getArray()
|
||||
);
|
||||
|
||||
$loadBalancers = new LoadBalancers($mock);
|
||||
$result = $loadBalancers->createLoadBalancer('699d98642c564d2e855e9661899b7252', $lbConfiguration);
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
|
||||
}
|
||||
|
||||
public function testListLoadBalancer()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listLoadBalancers.json');
|
||||
|
||||
$mock = $this->getMockBuilder(Adapter::class)->getMock();
|
||||
$mock->method('get')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers')
|
||||
);
|
||||
|
||||
$loadBalancers = new LoadBalancers($mock);
|
||||
$loadBalancers->listLoadBalancers('699d98642c564d2e855e9661899b7252');
|
||||
$this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result[0]->id);
|
||||
}
|
||||
|
||||
public function testGetLoadBalancerDetails()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getLoadBalancerDetails.json');
|
||||
|
||||
$mock = $this->getMockBuilder(Adapter::class)->getMock();
|
||||
$mock->method('get')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('get')
|
||||
->with(
|
||||
$this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252')
|
||||
);
|
||||
|
||||
$loadBalancers = new LoadBalancers($mock);
|
||||
$loadBalancers->getLoadBalancerDetails('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252');
|
||||
$this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
|
||||
}
|
||||
|
||||
public function testUpdateLoadBalancer()
|
||||
{
|
||||
$pools = [
|
||||
'17b5962d775c646f3f9725cbc7a53df4',
|
||||
'9290f38c5d07c2e2f4df57b1f61d4196',
|
||||
'00920f38ce07c2e2f4df50b1f61d4194'
|
||||
];
|
||||
|
||||
$lbConfiguration = new LoadBalancer('www.example.com', $pools, '17b5962d775c646f3f9725cbc7a53df4');
|
||||
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateLoadBalancer.json');
|
||||
|
||||
$mock = $this->getMockBuilder(Adapter::class)->getMock();
|
||||
$mock->method('patch')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('patch')
|
||||
->with(
|
||||
$this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252'),
|
||||
$this->equalTo($lbConfiguration->getArray())
|
||||
);
|
||||
|
||||
$loadBalancers = new LoadBalancers($mock);
|
||||
$result = $loadBalancers->updateLoadBalancer('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252', $lbConfiguration);
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
|
||||
}
|
||||
|
||||
public function testDeleteLoadBalancer()
|
||||
{
|
||||
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteLoadBalancer.json');
|
||||
|
||||
$mock = $this->getMockBuilder(Adapter::class)->getMock();
|
||||
$mock->method('delete')->willReturn($response);
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('delete')
|
||||
->with(
|
||||
$this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252')
|
||||
);
|
||||
|
||||
$loadBalancers = new LoadBalancers($mock);
|
||||
$result = $loadBalancers->deleteLoadBalancer('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252');
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
|
||||
}
|
||||
}
|
||||
46
tests/Fixtures/Endpoints/createLoadBalancer.json
Normal file
46
tests/Fixtures/Endpoints/createLoadBalancer.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [],
|
||||
"messages": [],
|
||||
"result": {
|
||||
"id": "699d98642c564d2e855e9661899b7252",
|
||||
"created_on": "2014-01-01T05:20:00.12345Z",
|
||||
"modified_on": "2014-01-01T05:20:00.12345Z",
|
||||
"description": "Load Balancer for www.example.com",
|
||||
"name": "www.example.com",
|
||||
"enabled": true,
|
||||
"ttl": 30,
|
||||
"fallback_pool": "17b5962d775c646f3f9725cbc7a53df4",
|
||||
"default_pools": [
|
||||
"17b5962d775c646f3f9725cbc7a53df4",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196",
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
],
|
||||
"region_pools": {
|
||||
"WNAM": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"ENAM": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"pop_pools": {
|
||||
"LAX": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"LHR": [
|
||||
"abd90f38ced07c2e2f4df50b1f61d4194",
|
||||
"f9138c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"SJC": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"proxied": true,
|
||||
"steering_policy": "dynamic_latency",
|
||||
"session_affinity": "cookie",
|
||||
"session_affinity_ttl": 5000
|
||||
}
|
||||
}
|
||||
8
tests/Fixtures/Endpoints/deleteLoadBalancer.json
Normal file
8
tests/Fixtures/Endpoints/deleteLoadBalancer.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [],
|
||||
"messages": [],
|
||||
"result": {
|
||||
"id": "699d98642c564d2e855e9661899b7252"
|
||||
}
|
||||
}
|
||||
46
tests/Fixtures/Endpoints/getLoadBalancerDetails.json
Normal file
46
tests/Fixtures/Endpoints/getLoadBalancerDetails.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [],
|
||||
"messages": [],
|
||||
"result": {
|
||||
"id": "699d98642c564d2e855e9661899b7252",
|
||||
"created_on": "2014-01-01T05:20:00.12345Z",
|
||||
"modified_on": "2014-01-01T05:20:00.12345Z",
|
||||
"description": "Load Balancer for www.example.com",
|
||||
"name": "www.example.com",
|
||||
"enabled": true,
|
||||
"ttl": 30,
|
||||
"fallback_pool": "17b5962d775c646f3f9725cbc7a53df4",
|
||||
"default_pools": [
|
||||
"17b5962d775c646f3f9725cbc7a53df4",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196",
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
],
|
||||
"region_pools": {
|
||||
"WNAM": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"ENAM": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"pop_pools": {
|
||||
"LAX": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"LHR": [
|
||||
"abd90f38ced07c2e2f4df50b1f61d4194",
|
||||
"f9138c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"SJC": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"proxied": true,
|
||||
"steering_policy": "dynamic_latency",
|
||||
"session_affinity": "cookie",
|
||||
"session_affinity_ttl": 5000
|
||||
}
|
||||
}
|
||||
54
tests/Fixtures/Endpoints/listLoadbalancers.json
Normal file
54
tests/Fixtures/Endpoints/listLoadbalancers.json
Normal file
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [],
|
||||
"messages": [],
|
||||
"result": [
|
||||
{
|
||||
"id": "699d98642c564d2e855e9661899b7252",
|
||||
"created_on": "2014-01-01T05:20:00.12345Z",
|
||||
"modified_on": "2014-01-01T05:20:00.12345Z",
|
||||
"description": "Load Balancer for www.example.com",
|
||||
"name": "www.example.com",
|
||||
"enabled": true,
|
||||
"ttl": 30,
|
||||
"fallback_pool": "17b5962d775c646f3f9725cbc7a53df4",
|
||||
"default_pools": [
|
||||
"17b5962d775c646f3f9725cbc7a53df4",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196",
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
],
|
||||
"region_pools": {
|
||||
"WNAM": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"ENAM": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"pop_pools": {
|
||||
"LAX": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"LHR": [
|
||||
"abd90f38ced07c2e2f4df50b1f61d4194",
|
||||
"f9138c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"SJC": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"proxied": true,
|
||||
"steering_policy": "dynamic_latency",
|
||||
"session_affinity": "cookie",
|
||||
"session_affinity_ttl": 5000
|
||||
}
|
||||
],
|
||||
"result_info": {
|
||||
"page": 1,
|
||||
"per_page": 20,
|
||||
"count": 1,
|
||||
"total_count": 2000
|
||||
}
|
||||
}
|
||||
46
tests/Fixtures/Endpoints/updateLoadBalancer.json
Normal file
46
tests/Fixtures/Endpoints/updateLoadBalancer.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"success": true,
|
||||
"errors": [],
|
||||
"messages": [],
|
||||
"result": {
|
||||
"id": "699d98642c564d2e855e9661899b7252",
|
||||
"created_on": "2014-01-01T05:20:00.12345Z",
|
||||
"modified_on": "2014-01-01T05:20:00.12345Z",
|
||||
"description": "Load Balancer for www.example.com",
|
||||
"name": "www.example.com",
|
||||
"enabled": true,
|
||||
"ttl": 30,
|
||||
"fallback_pool": "17b5962d775c646f3f9725cbc7a53df4",
|
||||
"default_pools": [
|
||||
"17b5962d775c646f3f9725cbc7a53df4",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196",
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
],
|
||||
"region_pools": {
|
||||
"WNAM": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"ENAM": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"pop_pools": {
|
||||
"LAX": [
|
||||
"de90f38ced07c2e2f4df50b1f61d4194",
|
||||
"9290f38c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"LHR": [
|
||||
"abd90f38ced07c2e2f4df50b1f61d4194",
|
||||
"f9138c5d07c2e2f4df57b1f61d4196"
|
||||
],
|
||||
"SJC": [
|
||||
"00920f38ce07c2e2f4df50b1f61d4194"
|
||||
]
|
||||
},
|
||||
"proxied": true,
|
||||
"steering_policy": "dynamic_latency",
|
||||
"session_affinity": "cookie",
|
||||
"session_affinity_ttl": 5000
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user