TestCase.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Tests;
  3. use App\Services\Config;
  4. use PHPUnit_Framework_TestCase;
  5. use Slim\Http\Body;
  6. use Slim\Http\Environment;
  7. use Slim\Http\Headers;
  8. use Slim\HTTP\Request;
  9. use Slim\Http\RequestBody;
  10. use Slim\Http\Response;
  11. use Slim\Http\Uri;
  12. class TestCase extends PHPUnit_Framework_TestCase
  13. {
  14. public $app;
  15. public $request, $response;
  16. public function setUp()
  17. {
  18. $this->setTestingEnv();
  19. }
  20. /**
  21. * @param $method
  22. * @param $path
  23. * @param $body
  24. * @param $options
  25. * @return Request
  26. */
  27. protected function requestFactory($method, $path, $body = [], $options = [])
  28. {
  29. $uri = Uri::createFromString($path);
  30. $headers = new Headers();
  31. $cookies = [];
  32. $_POST['_METHOD'] = $method;
  33. if (strtolower($method) != 'get' && is_array($body)) {
  34. foreach ($body as $key => $value) {
  35. $_POST[$key] = $value;
  36. }
  37. }
  38. $envMethod = 'POST';
  39. if (strtolower($method) == 'get') {
  40. $envMethod = 'GET';
  41. }
  42. $env = Environment::mock([
  43. 'REQUEST_URI' => $path,
  44. 'REQUEST_METHOD' => $envMethod,
  45. 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo'
  46. ]);
  47. $serverParams = $env->all();
  48. $body = $this->buildBody($body);
  49. //echo $body->getContents();
  50. // @todo
  51. // $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, []);
  52. $request = Request::createFromEnvironment($env);
  53. unset($_POST);
  54. return $request;
  55. }
  56. /**
  57. * @param $input
  58. * @return Body
  59. */
  60. protected function buildBody($input)
  61. {
  62. $getContent = function () use ($input) {
  63. if (is_array($input)) {
  64. return http_build_query($input);
  65. }
  66. return $input;
  67. };
  68. $content = $getContent();
  69. $body = new RequestBody();
  70. $body->write($content);
  71. $body->rewind();
  72. return $body;
  73. }
  74. public function createApp()
  75. {
  76. // Build App
  77. $app = require __DIR__ . '/../app/routes.php';
  78. $app->run(true);
  79. return $app;
  80. }
  81. public function request($method, $path, $options = [])
  82. {
  83. // Build App
  84. $app = $this->createApp();
  85. $this->app = $app;
  86. // Build Req,Res
  87. $response = new Response();
  88. $request = $this->requestFactory($method, $path, $options);
  89. // send Req
  90. $this->response = $app($request, $response);
  91. }
  92. public function get($path, $options = [])
  93. {
  94. $this->request('GET', $path, $options);
  95. }
  96. public function post($path, $body = [], $options = [])
  97. {
  98. $this->request('POST', $path, $body, $options);
  99. }
  100. public function put($path, $body = [], $options = [])
  101. {
  102. $this->request('PUT', $path, $body, $options);
  103. }
  104. public function delete($path, $body = [], $options = [])
  105. {
  106. $this->request('DELETE', $path, $body, $options);
  107. }
  108. /**
  109. * Set env in prod
  110. */
  111. public function setProdEnv()
  112. {
  113. Config::set('env', 'prod');
  114. }
  115. /**
  116. * Set testing env
  117. */
  118. public function setTestingEnv()
  119. {
  120. Config::set('env', 'testing');
  121. }
  122. /**
  123. * @param $code
  124. * @param null $response
  125. */
  126. public function checkErrorCode($code, $response = null)
  127. {
  128. if ($response == null) {
  129. $response = $this->response;
  130. }
  131. $data = json_decode($response->getBody(), true);
  132. $this->assertEquals($code, $data['error_code']);
  133. }
  134. }