Advertisement
Guest User

Restful GuzzleClientTest

a guest
Dec 9th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Amco;
  4.  
  5. use Amco\Services\Restful\GuzzleClient;
  6.  
  7. /**
  8. * Test cases for Amco\Services\Restful\GuzzleClient
  9. */
  10. class GuzzleClientTest extends \PHPUnit_Framework_TestCase
  11. {
  12.  
  13. private $guzzleClient;
  14.  
  15.  
  16. public function setUp()
  17. {
  18. $this->guzzleClient = new GuzzleClient();
  19. }
  20.  
  21. public function testClientConstruct()
  22. {
  23. $this->assertNotNull($this->guzzleClient->getClient());
  24. }
  25.  
  26. public function testClientPost()
  27. {
  28. $response = $this->guzzleClient->post(
  29. "https://api.tbxnet.com/v2/UserServices/device",
  30. ["headers" => ["Authorization" => "IXP4RAI0w7ajTGTan7qlnY32r39BBb3x", "Content-Type" => "application/x-www-form-urlencoded", "Accept" => "application/json"]],
  31. ["form_params" => ["subscriberId" => "123456", "country" => "CO", "type" => "STB", "description" => "coship"]]
  32. );
  33.  
  34. $this->assertNotNull($response);
  35. $this->assertArrayHasKey("statusCode", $response);
  36. $this->assertArrayHasKey("body", $response);
  37. $this->assertEquals(200, $response["statusCode"]);
  38. }
  39.  
  40. public function testClientPostError()
  41. {
  42. $response = $this->guzzleClient->post(
  43. "https://api.tbxnet.com/v2/UserServices/device",
  44. ["headers" => ["Authorization" => "", "Content-Type" => "application/x-www-form-urlencoded", "Accept" => "application/json"]],
  45. ["form_params" => ["subscriberId" => "123456", "country" => "CO", "type" => "STB", "description" => "coship"]]
  46. );
  47.  
  48. $this->assertNotNull($response);
  49. $this->assertArrayHasKey("statusCode", $response);
  50. $this->assertArrayHasKey("body", $response);
  51. $this->assertEquals(400, $response["statusCode"]);
  52. }
  53.  
  54. public function testClientPostNoUrl()
  55. {
  56. $response = $this->guzzleClient->post(
  57. null,
  58. ["headers" => ["Authorization" => "", "Content-Type" => "application/x-www-form-urlencoded", "Accept" => "application/json"]],
  59. ["form_params" => ["subscriberId" => "123456", "country" => "CO", "type" => "STB", "description" => "coship"]]
  60. );
  61.  
  62. $this->assertNotNull($response);
  63. $this->assertArrayHasKey("statusCode", $response);
  64. $this->assertArrayHasKey("body", $response);
  65. $this->assertEquals(500, $response["statusCode"]);
  66. }
  67.  
  68. public function testClientPostBadUrl()
  69. {
  70. $response = $this->guzzleClient->post(
  71. "",
  72. ["headers" => ["Authorization" => "", "Content-Type" => "application/x-www-form-urlencoded", "Accept" => "application/json"]],
  73. ["form_params" => ["subscriberId" => "123456", "country" => "CO", "type" => "STB", "description" => "coship"]]
  74. );
  75.  
  76. $this->assertNotNull($response);
  77. $this->assertArrayHasKey("statusCode", $response);
  78. $this->assertArrayHasKey("body", $response);
  79. $this->assertEquals(500, $response["statusCode"]);
  80. }
  81.  
  82. public function testClientPut()
  83. {
  84. $response = $this->guzzleClient->put(
  85. "https://jsonplaceholder.typicode.com/posts/1",
  86. ["headers" => ["Authorization" => "IXP439BBb3x", "Accept" => "application/json"]],
  87. ["json" => ["id" => "1", "title" => "foo", "body" => "bar", "userId" => "1"]]
  88. );
  89.  
  90. $this->assertNotNull($response);
  91. $this->assertArrayHasKey("statusCode", $response);
  92. $this->assertArrayHasKey("body", $response);
  93. $this->assertEquals(200, $response["statusCode"]);
  94. }
  95.  
  96. public function testClientPutError()
  97. {
  98. $response = $this->guzzleClient->put(
  99. "https://jsonplaceholder.typicode.com/posts/non-existent-id",
  100. [],
  101. ["json" => ["id" => "1", "title" => "foo", "body" => "bar", "userId" => "1"]]
  102. );
  103.  
  104. $this->assertNotNull($response);
  105. $this->assertArrayHasKey("statusCode", $response);
  106. $this->assertArrayHasKey("body", $response);
  107. $this->assertEquals(404, $response["statusCode"]);
  108. }
  109.  
  110. public function testClientGet()
  111. {
  112. $response = $this->guzzleClient->get(
  113. "https://jsonplaceholder.typicode.com/posts",
  114. ["headers" => ["Authorization" => "IXP4RAI0w7ajTGTa", "Accept" => "application/json"]],
  115. ["query" => ["userId" => "1"]]
  116. );
  117.  
  118. $this->assertNotNull($response);
  119. $this->assertArrayHasKey("statusCode", $response);
  120. $this->assertArrayHasKey("body", $response);
  121. $this->assertEquals(200, $response["statusCode"]);
  122. }
  123.  
  124. public function testClientGetError()
  125. {
  126. $response = $this->guzzleClient->get(
  127. "https://jsonplaceholder.typicode.com/non-existent-url",
  128. ["headers" => ["Authorization" => "IXP4RAI0w7ajTGTa", "Accept" => "application/json"]],
  129. ["query" => ["userId" => "1"]]
  130. );
  131.  
  132. $this->assertNotNull($response);
  133. $this->assertArrayHasKey("statusCode", $response);
  134. $this->assertArrayHasKey("body", $response);
  135. $this->assertEquals(404, $response["statusCode"]);
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement