PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/tests.php

http://github.com/fictivekin/Resty.php
PHP | 297 lines | 208 code | 82 blank | 7 comment | 1 complexity | b265e57319b98870e2e4be2647228a12 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. require dirname(__FILE__) . "/../vendor/autoload.php";
  3. use \FUnit as fu;
  4. use \Resty\Resty;
  5. define("HTTPBIN_URL", "http://httpbin.org/");
  6. /**
  7. * this is an error -> exception handler that is part of a super hack job here
  8. * to detect if errors were suppressed or not. Lame.
  9. */
  10. function exception_error_handler($errno, $errstr, $errfile, $errline)
  11. {
  12. if (error_reporting() === 0) {
  13. return;
  14. }
  15. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  16. }
  17. fu::setup(function () {
  18. fu::fixture('resty', new Resty());
  19. });
  20. fu::teardown(function () {
  21. fu::reset_fixtures();
  22. });
  23. fu::test('quacks like a duck', function () {
  24. $r = fu::fixture('resty');
  25. fu::ok($r instanceof Resty, "is a Resty object");
  26. fu::strict_equal($r->getBaseUrl(), null, "Base url is blank");
  27. fu::equal($r->getUserAgent(), 'Resty ' . Resty::VERSION, "Default user agent");
  28. $r->setUserAgent('Poop');
  29. fu::equal($r->getUserAgent(), 'Poop', "Poop user agent");
  30. });
  31. fu::test('Un-silence fopen test', function () {
  32. set_error_handler('exception_error_handler');
  33. $r = fu::fixture('resty');
  34. $r->silenceFopenWarning(false);
  35. try {
  36. $r->get('http://fai9rp9whqrp9b8hqp98bhpwohropsrihbpohtpowhi/');
  37. } catch (\ErrorException $e) {
  38. fu::ok(is_string($e->getMessage()), "ErrorException thrown -- not silenced");
  39. restore_error_handler();
  40. }
  41. restore_error_handler();
  42. });
  43. fu::test('Silence fopen test', function () {
  44. set_error_handler('exception_error_handler');
  45. $r = fu::fixture('resty');
  46. $r->silenceFopenWarning(true);
  47. try {
  48. $r->get('http://fai9rp9whqrp9b8hqp98bhpwohropsrihbpohtpowhi/');
  49. } catch (\ErrorException $e) {
  50. print $e->getMessage();
  51. fu::fail("ErrorException thrown -- not silenced");
  52. restore_error_handler();
  53. }
  54. fu::ok(true, "Error silenced");
  55. restore_error_handler();
  56. });
  57. fu::test('Raise fopen exception', function () {
  58. $r = fu::fixture('resty');
  59. $r->silenceFopenWarning(true);
  60. $r->raiseFopenException(true);
  61. try {
  62. $r->get('http://fai9rp9whqrp9b8hqp98bhpwohropsrihbpohtpowhi/');
  63. } catch (\Exception $e) {
  64. fu::ok(is_string($e->getMessage()), "Exception thrown");
  65. $r->raiseFopenException(false);
  66. }
  67. $r->raiseFopenException(false);
  68. });
  69. fu::test('Don\'t raise fopen exception', function () {
  70. $r = fu::fixture('resty');
  71. $r->silenceFopenWarning(true);
  72. $r->raiseFopenException(false);
  73. try {
  74. $r->get('http://fai9rp9whqrp9b8hqp98bhpwohropsrihbpohtpowhi/');
  75. } catch (\Exception $e) {
  76. fu::fail("Exception thrown");
  77. $r->silenceFopenWarning(false);
  78. }
  79. fu::ok(true, "No exception thrown");
  80. $r->silenceFopenWarning(false);
  81. });
  82. fu::test('gimme bar requests and responses', function () {
  83. $r = fu::fixture('resty');
  84. $r->setBaseURL('https://gimmebar.com/api/v1/');
  85. $resp = $r->get('public/assets/funkatron');
  86. $req = $r->getLastRequest();
  87. // request assertions
  88. $req_opts = $req['opts']['http'];
  89. fu::equal($req_opts['method'], 'GET', "GET method");
  90. fu::equal(
  91. $req['url'],
  92. 'https://gimmebar.com/api/v1/public/assets/funkatron',
  93. "URL was correct"
  94. );
  95. fu::strict_equal(
  96. $req_opts['content'],
  97. null,
  98. "Body content is null"
  99. );
  100. fu::strict_equal(
  101. $req['querydata'],
  102. null,
  103. "Querydata is null"
  104. );
  105. fu::strict_equal(
  106. $req['options'],
  107. null,
  108. "options is null"
  109. );
  110. fu::ok(
  111. in_array('Connection: close', $req_opts['header']),
  112. "Connection: close was sent"
  113. );
  114. fu::equal(
  115. $req_opts['user_agent'],
  116. $r->getUserAgent(),
  117. "Default user agent"
  118. );
  119. fu::equal(
  120. $req_opts['timeout'],
  121. Resty::DEFAULT_TIMEOUT,
  122. "Default timeout was used"
  123. );
  124. fu::strict_equal(
  125. $req_opts['ignore_errors'],
  126. 1,
  127. "errors were ignored in HTTP stream wrapper"
  128. );
  129. // respose assertions
  130. fu::ok(is_int($resp['status']), 'response status should be an integer');
  131. fu::equal($resp['status'], 200, 'response status should be 200');
  132. fu::ok($resp['body'] instanceof \StdClass, 'Response body should be a StdClass');
  133. });
  134. fu::test('httpbin.org GET status responses', function () {
  135. $r = fu::fixture('resty');
  136. $r->setBaseURL(HTTPBIN_URL);
  137. $resp = $r->get('status/200');
  138. fu::strict_equal(200, $resp['status'], "Status is 200");
  139. $resp = $r->get('status/404');
  140. fu::strict_equal(404, $resp['status'], "Status is 404");
  141. $resp = $r->get('status/500');
  142. fu::strict_equal(500, $resp['status'], "Status is 500");
  143. });
  144. fu::test('httpbin.org GET JSON decoding', function () {
  145. $r = fu::fixture('resty');
  146. $r->setBaseURL(HTTPBIN_URL);
  147. $r->jsonToArray(false);
  148. $resp = $r->get('get');
  149. fu::ok(is_object($resp['body']), "decoded JSON is object");
  150. fu::ok($resp['body'] instanceof \StdClass, 'Response body should be a StdClass');
  151. $r->jsonToArray(true);
  152. $resp = $r->get('get');
  153. fu::ok(is_array($resp['body']), "decoded JSON is array");
  154. $r->jsonToArray(false);
  155. });
  156. fu::test('httpbin.org POST form stuff', function () {
  157. $r = fu::fixture('resty');
  158. $r->setBaseURL(HTTPBIN_URL);
  159. $resp = $r->post("post", array("foo"=>"bar", "foo2"=>"bar2"));
  160. fu::has("form", $resp['body']);
  161. fu::has("foo", $resp['body']->form, "foo is in form data");
  162. fu::has("foo2", $resp['body']->form, "foo2 is in form data");
  163. fu::strict_equal("bar", $resp['body']->form->foo, "foo value is correct");
  164. fu::strict_equal("bar2", $resp['body']->form->foo2, "foo2 value is correct");
  165. // same test, but as an object (json_decoded to be sure)
  166. $querydata = json_decode('{"foo":"bar", "foo2":"bar2"}');
  167. $resp = $r->post("post", $querydata);
  168. fu::has("form", $resp['body']);
  169. fu::has("foo", $resp['body']->form, "foo is in form data");
  170. fu::has("foo2", $resp['body']->form, "foo2 is in form data");
  171. fu::strict_equal("bar", $resp['body']->form->foo, "foo value is correct");
  172. fu::strict_equal("bar2", $resp['body']->form->foo2, "foo2 value is correct");
  173. });
  174. fu::test('httpbin.org POST JSON stuff', function () {
  175. $r = fu::fixture('resty');
  176. $r->setBaseURL(HTTPBIN_URL);
  177. $resp = $r->postJson("post", array("foo"=>"bar", "foo2"=>"bar2"));
  178. $req = $r->getLastRequest();
  179. fu::strict_equal('application/json', $req['headers']['Content-Type'], "Request Content-Type is application/json");
  180. fu::strict_equal('POST', $req['method'], "Request method is POST");
  181. fu::has("json", $resp['body']);
  182. fu::has("foo", $resp['body']->json, "foo is in json data");
  183. fu::has("foo2", $resp['body']->json, "foo2 is in json data");
  184. fu::strict_equal("bar", $resp['body']->json->foo, "foo value is correct");
  185. fu::strict_equal("bar2", $resp['body']->json->foo2, "foo2 value is correct");
  186. });
  187. fu::test('httpbin.org PUT JSON stuff', function () {
  188. $r = fu::fixture('resty');
  189. $r->setBaseURL(HTTPBIN_URL);
  190. $resp = $r->putJson("put", array("foo"=>"bar", "foo2"=>"bar2"));
  191. $req = $r->getLastRequest();
  192. fu::strict_equal('application/json', $req['headers']['Content-Type'], "Request Content-Type is application/json");
  193. fu::strict_equal('PUT', $req['method'], "Request method is PUT");
  194. fu::has("json", $resp['body']);
  195. fu::has("foo", $resp['body']->json, "foo is in json data");
  196. fu::has("foo2", $resp['body']->json, "foo2 is in json data");
  197. fu::strict_equal("bar", $resp['body']->json->foo, "foo value is correct");
  198. fu::strict_equal("bar2", $resp['body']->json->foo2, "foo2 value is correct");
  199. });
  200. fu::test('httpbin.org PATCH JSON stuff', function () {
  201. $r = fu::fixture('resty');
  202. $r->setBaseURL(HTTPBIN_URL);
  203. $resp = $r->patchJson("patch", array("foo"=>"bar", "foo2"=>"bar2"));
  204. $req = $r->getLastRequest();
  205. print_r($req);
  206. print_r($resp);
  207. fu::strict_equal('application/json', $req['headers']['Content-Type'], "Request Content-Type is application/json");
  208. fu::strict_equal('PATCH', $req['method'], "Request method is PATCH");
  209. fu::has("json", $resp['body']);
  210. fu::has("foo", $resp['body']->json, "foo is in json data");
  211. fu::has("foo2", $resp['body']->json, "foo2 is in json data");
  212. fu::strict_equal("bar", $resp['body']->json->foo, "foo value is correct");
  213. fu::strict_equal("bar2", $resp['body']->json->foo2, "foo2 value is correct");
  214. });
  215. fu::run();