PageRulesTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 19/09/2017
  6. * Time: 19:25
  7. */
  8. class PageRulesTest extends TestCase
  9. {
  10. public function testCreatePageRule()
  11. {
  12. $target = new \Cloudflare\API\Configurations\PageRulesTargets('*example.com/images/*');
  13. $action = new \Cloudflare\API\Configurations\PageRulesActions();
  14. $action->setAlwaysOnline(true);
  15. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createPageRule.json');
  16. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  17. $mock->method('post')->willReturn($response);
  18. $mock->expects($this->once())
  19. ->method('post')
  20. ->with(
  21. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/pagerules'),
  22. $this->equalTo([
  23. 'targets' => $target->getArray(),
  24. 'actions' => $action->getArray(),
  25. 'status' => 'active',
  26. 'priority' => 1
  27. ])
  28. );
  29. $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock);
  30. $result = $pageRules->createPageRule('023e105f4ecef8ad9ca31a8372d0c353', $target, $action, true, 1);
  31. $this->assertTrue($result);
  32. $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id);
  33. }
  34. public function testListPageRules()
  35. {
  36. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listPageRules.json');
  37. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  38. $mock->method('get')->willReturn($response);
  39. $mock->expects($this->once())
  40. ->method('get')
  41. ->with(
  42. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/pagerules'),
  43. $this->equalTo([
  44. 'status' => 'active',
  45. 'order' => 'status',
  46. 'direction' => 'desc',
  47. 'match' => 'all'
  48. ])
  49. );
  50. $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock);
  51. $pageRules->listPageRules('023e105f4ecef8ad9ca31a8372d0c353', 'active', 'status', 'desc', 'all');
  52. $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result[0]->id);
  53. }
  54. public function testGetPageRuleDetails()
  55. {
  56. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getPageRuleDetails.json');
  57. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  58. $mock->method('get')->willReturn($response);
  59. $mock->expects($this->once())
  60. ->method('get')
  61. ->with(
  62. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/pagerules/9a7806061c88ada191ed06f989cc3dac')
  63. );
  64. $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock);
  65. $pageRules->getPageRuleDetails('023e105f4ecef8ad9ca31a8372d0c353', '9a7806061c88ada191ed06f989cc3dac');
  66. $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id);
  67. }
  68. public function testUpdatePageRule()
  69. {
  70. $target = new \Cloudflare\API\Configurations\PageRulesTargets('*example.com/images/*');
  71. $action = new \Cloudflare\API\Configurations\PageRulesActions();
  72. $action->setAlwaysOnline(true);
  73. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updatePageRule.json');
  74. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  75. $mock->method('patch')->willReturn($response);
  76. $mock->expects($this->once())
  77. ->method('patch')
  78. ->with(
  79. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/pagerules/9a7806061c88ada191ed06f989cc3dac'),
  80. $this->equalTo([
  81. 'targets' => $target->getArray(),
  82. 'actions' => $action->getArray(),
  83. 'status' => 'active',
  84. 'priority' => 1
  85. ])
  86. );
  87. $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock);
  88. $result = $pageRules->updatePageRule('023e105f4ecef8ad9ca31a8372d0c353', '9a7806061c88ada191ed06f989cc3dac', $target, $action, true, 1);
  89. $this->assertTrue($result);
  90. $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id);
  91. }
  92. public function testDeletePageRule()
  93. {
  94. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deletePageRule.json');
  95. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  96. $mock->method('delete')->willReturn($response);
  97. $mock->expects($this->once())
  98. ->method('delete')
  99. ->with(
  100. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/pagerules/9a7806061c88ada191ed06f989cc3dac')
  101. );
  102. $pageRules = new \Cloudflare\API\Endpoints\PageRules($mock);
  103. $result = $pageRules->deletePageRule('023e105f4ecef8ad9ca31a8372d0c353', '9a7806061c88ada191ed06f989cc3dac');
  104. $this->assertTrue($result);
  105. $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $pageRules->getBody()->result->id);
  106. }
  107. }