LoadBalancersTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. use Cloudflare\API\Adapter\Adapter;
  3. use Cloudflare\API\Configurations\LoadBalancer;
  4. use Cloudflare\API\Endpoints\LoadBalancers;
  5. /**
  6. * @author Martijn Smidt <martijn@squeezely.tech>
  7. * User: HemeraOne
  8. * Date: 13/05/2019
  9. */
  10. class LoadBalancersTest extends TestCase
  11. {
  12. public function testCreateLoadBalancer()
  13. {
  14. $pools = [
  15. '17b5962d775c646f3f9725cbc7a53df4',
  16. '9290f38c5d07c2e2f4df57b1f61d4196',
  17. '00920f38ce07c2e2f4df50b1f61d4194'
  18. ];
  19. $lbConfiguration = new LoadBalancer('www.example.com', $pools, '17b5962d775c646f3f9725cbc7a53df4');
  20. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createLoadBalancer.json');
  21. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  22. $mock->method('post')->willReturn($response);
  23. $mock->expects($this->once())
  24. ->method('post')
  25. ->with(
  26. $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers'),
  27. $lbConfiguration->getArray()
  28. );
  29. $loadBalancers = new LoadBalancers($mock);
  30. $result = $loadBalancers->createLoadBalancer('699d98642c564d2e855e9661899b7252', $lbConfiguration);
  31. $this->assertTrue($result);
  32. $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
  33. }
  34. public function testListLoadBalancer()
  35. {
  36. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listLoadBalancers.json');
  37. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  38. $mock->method('get')->willReturn($response);
  39. $mock->expects($this->once())
  40. ->method('get')
  41. ->with(
  42. $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers')
  43. );
  44. $loadBalancers = new LoadBalancers($mock);
  45. $loadBalancers->listLoadBalancers('699d98642c564d2e855e9661899b7252');
  46. $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result[0]->id);
  47. }
  48. public function testGetLoadBalancerDetails()
  49. {
  50. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getLoadBalancerDetails.json');
  51. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  52. $mock->method('get')->willReturn($response);
  53. $mock->expects($this->once())
  54. ->method('get')
  55. ->with(
  56. $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252')
  57. );
  58. $loadBalancers = new LoadBalancers($mock);
  59. $loadBalancers->getLoadBalancerDetails('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252');
  60. $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
  61. }
  62. public function testUpdateLoadBalancer()
  63. {
  64. $pools = [
  65. '17b5962d775c646f3f9725cbc7a53df4',
  66. '9290f38c5d07c2e2f4df57b1f61d4196',
  67. '00920f38ce07c2e2f4df50b1f61d4194'
  68. ];
  69. $lbConfiguration = new LoadBalancer('www.example.com', $pools, '17b5962d775c646f3f9725cbc7a53df4');
  70. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateLoadBalancer.json');
  71. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  72. $mock->method('put')->willReturn($response);
  73. $mock->expects($this->once())
  74. ->method('put')
  75. ->with(
  76. $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252'),
  77. $this->equalTo($lbConfiguration->getArray())
  78. );
  79. $loadBalancers = new LoadBalancers($mock);
  80. $result = $loadBalancers->updateLoadBalancer('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252', $lbConfiguration);
  81. $this->assertTrue($result);
  82. $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
  83. }
  84. public function testDeleteLoadBalancer()
  85. {
  86. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteLoadBalancer.json');
  87. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  88. $mock->method('delete')->willReturn($response);
  89. $mock->expects($this->once())
  90. ->method('delete')
  91. ->with(
  92. $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252')
  93. );
  94. $loadBalancers = new LoadBalancers($mock);
  95. $result = $loadBalancers->deleteLoadBalancer('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252');
  96. $this->assertTrue($result);
  97. $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id);
  98. }
  99. }