CertificatesTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. use Cloudflare\API\Endpoints\Certificates;
  3. class CertificatesTest extends TestCase
  4. {
  5. public function testListCertificates()
  6. {
  7. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listCertificates.json');
  8. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  9. $mock->method('get')->willReturn($response);
  10. $mock->expects($this->once())
  11. ->method('get')
  12. ->with(
  13. $this->equalTo('certificates'),
  14. $this->equalTo(
  15. [
  16. 'zone_id' => '023e105f4ecef8ad9ca31a8372d0c353',
  17. ]
  18. )
  19. );
  20. $certEndpoint = new Certificates($mock);
  21. $result = $certEndpoint->listCertificates('023e105f4ecef8ad9ca31a8372d0c353');
  22. $this->assertObjectHasAttribute('result', $result);
  23. $cert = $result->result[0];
  24. $this->assertEquals('328578533902268680212849205732770752308931942346', $cert->id);
  25. $this->assertEquals('origin-rsa', $cert->request_type);
  26. $this->assertEquals(5475, $cert->requested_validity);
  27. $this->assertEquals(['example.com', '*.example.com'], $cert->hostnames);
  28. $this->assertEquals('some-cert-data', $cert->certificate);
  29. $this->assertEquals('some-csr-data', $cert->csr);
  30. }
  31. public function testGetCertificate()
  32. {
  33. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getCertificate.json');
  34. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  35. $mock->method('get')->willReturn($response);
  36. $mock->expects($this->once())
  37. ->method('get')
  38. ->with(
  39. $this->equalTo('certificates/6666699999996666699999999966666666'),
  40. $this->equalTo(['zone_id' => '023e105f4ecef8ad9ca31a8372d0c353']),
  41. $this->equalTo([])
  42. );
  43. $certEndpoint = new Certificates($mock);
  44. $response = $certEndpoint->getCertificate(
  45. '6666699999996666699999999966666666',
  46. '023e105f4ecef8ad9ca31a8372d0c353'
  47. );
  48. $this->assertObjectHasAttribute('result', $response);
  49. $cert = $response->result;
  50. $this->assertEquals('6666699999996666699999999966666666', $cert->id);
  51. $this->assertEquals('origin-ecc', $cert->request_type);
  52. $this->assertEquals(5475, $cert->requested_validity);
  53. $this->assertEquals(['foo.example.com', 'bar.example.com'], $cert->hostnames);
  54. $this->assertEquals('some-cert-data-foobar', $cert->certificate);
  55. $this->assertEquals('some-csr-data-foobar', $cert->csr);
  56. }
  57. public function testRevokeCertificate()
  58. {
  59. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getCertificate.json');
  60. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  61. $mock->method('delete')->willReturn($response);
  62. $mock->expects($this->once())
  63. ->method('delete')
  64. ->with(
  65. $this->equalTo('certificates/11112222233333444455555'),
  66. $this->equalTo(['zone_id' => '023e105f4ecef8ad9ca31a8372d0c353']),
  67. $this->equalTo([])
  68. );
  69. $certEndpoint = new Certificates($mock);
  70. $result = $certEndpoint->revokeCertificate(
  71. '11112222233333444455555',
  72. '023e105f4ecef8ad9ca31a8372d0c353'
  73. );
  74. $this->assertTrue($result);
  75. }
  76. public function testCreateCertificate()
  77. {
  78. $certificate = new \Cloudflare\API\Configurations\Certificate();
  79. $certificate->setHostnames(['foo.example.com', 'bar.exapmle.com']);
  80. $certificate->setRequestType(\Cloudflare\API\Configurations\Certificate::ORIGIN_ECC);
  81. $certificate->setRequestedValidity(365);
  82. $certificate->setCsr('some-csr-data-barbar');
  83. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getCertificate.json');
  84. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  85. $mock->method('post')->willReturn($response);
  86. $mock->expects($this->once())
  87. ->method('post')
  88. ->with(
  89. $this->equalTo('certificates'),
  90. $this->equalTo($certificate->getArray()),
  91. $this->equalTo([])
  92. );
  93. $certEndpoint = new Certificates($mock);
  94. $result = $certEndpoint->createCertificate($certificate);
  95. $this->assertTrue($result);
  96. }
  97. }