PoolsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. use Cloudflare\API\Adapter\Adapter;
  3. use Cloudflare\API\Endpoints\Pools;
  4. /**
  5. * @author Martijn Smidt <martijn@squeezely.tech>
  6. * User: HemeraOne
  7. * Date: 13/05/2019
  8. */
  9. class PoolsTest extends TestCase
  10. {
  11. public function testCreatePool()
  12. {
  13. $origins = [
  14. [
  15. 'name' => 'app-server-1',
  16. 'address' => '0.0.0.0',
  17. 'enabled' => true,
  18. 'weight' => 0.56
  19. ]
  20. ];
  21. $poolConfiguration = new \Cloudflare\API\Configurations\Pool('primary-dc-1', $origins);
  22. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createPool.json');
  23. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  24. $mock->method('post')->willReturn($response);
  25. $mock->expects($this->once())
  26. ->method('post')
  27. ->with(
  28. $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools'),
  29. $poolConfiguration->getArray()
  30. );
  31. $pools = new Pools($mock);
  32. $result = $pools->createPool('01a7362d577a6c3019a474fd6f485823', $poolConfiguration);
  33. $this->assertTrue($result);
  34. $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id);
  35. }
  36. public function testListPools()
  37. {
  38. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listPools.json');
  39. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  40. $mock->method('get')->willReturn($response);
  41. $mock->expects($this->once())
  42. ->method('get')
  43. ->with(
  44. $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools')
  45. );
  46. $pools = new Pools($mock);
  47. $pools->listPools('01a7362d577a6c3019a474fd6f485823');
  48. $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result[0]->id);
  49. }
  50. public function testGetPoolDetails()
  51. {
  52. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getPoolDetails.json');
  53. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  54. $mock->method('get')->willReturn($response);
  55. $mock->expects($this->once())
  56. ->method('get')
  57. ->with(
  58. $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools/17b5962d775c646f3f9725cbc7a53df4')
  59. );
  60. $pools = new Pools($mock);
  61. $pools->getPoolDetails('01a7362d577a6c3019a474fd6f485823', '17b5962d775c646f3f9725cbc7a53df4');
  62. $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id);
  63. }
  64. public function testUpdatePool()
  65. {
  66. $origins = [
  67. [
  68. 'name' => 'app-server-1',
  69. 'address' => '0.0.0.0',
  70. 'enabled' => true,
  71. 'weight' => 0.56
  72. ]
  73. ];
  74. $poolConfiguration = new \Cloudflare\API\Configurations\Pool('primary-dc-1', $origins);
  75. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updatePool.json');
  76. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  77. $mock->method('put')->willReturn($response);
  78. $mock->expects($this->once())
  79. ->method('put')
  80. ->with(
  81. $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools/17b5962d775c646f3f9725cbc7a53df4'),
  82. $this->equalTo($poolConfiguration->getArray())
  83. );
  84. $pools = new Pools($mock);
  85. $result = $pools->updatePool('01a7362d577a6c3019a474fd6f485823', '17b5962d775c646f3f9725cbc7a53df4', $poolConfiguration);
  86. $this->assertTrue($result);
  87. $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id);
  88. }
  89. public function testDeletePool()
  90. {
  91. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deletePool.json');
  92. $mock = $this->getMockBuilder(Adapter::class)->getMock();
  93. $mock->method('delete')->willReturn($response);
  94. $mock->expects($this->once())
  95. ->method('delete')
  96. ->with(
  97. $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools/17b5962d775c646f3f9725cbc7a53df4')
  98. );
  99. $pools = new Pools($mock);
  100. $result = $pools->deletePool('01a7362d577a6c3019a474fd6f485823', '17b5962d775c646f3f9725cbc7a53df4');
  101. $this->assertTrue($result);
  102. $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id);
  103. }
  104. }