ResponseCallsTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Extracting\Strategies\Responses;
  3. use Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls;
  4. use Knuckles\Scribe\ScribeServiceProvider;
  5. use Knuckles\Scribe\Tests\Fixtures\TestController;
  6. use Knuckles\Scribe\Tools\DocumentationConfig;
  7. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  8. use Orchestra\Testbench\TestCase;
  9. use Illuminate\Support\Facades\Route as LaravelRouteFacade;
  10. use Dingo\Api\Routing\Router as DingoRouter;
  11. class ResponseCallsTest extends TestCase
  12. {
  13. use ArraySubsetAsserts;
  14. protected function getPackageProviders($app)
  15. {
  16. return [
  17. ScribeServiceProvider::class,
  18. ];
  19. }
  20. /** @test */
  21. public function can_call_route_and_fetch_response()
  22. {
  23. $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
  24. $rules = [
  25. 'headers' => [
  26. 'Content-Type' => 'application/json',
  27. 'Accept' => 'application/json',
  28. ],
  29. 'response_calls' => [
  30. 'methods' => ['*'],
  31. ],
  32. ];
  33. $strategy = new ResponseCalls(new DocumentationConfig([]));
  34. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
  35. $this->assertEquals(200, $results[0]['status']);
  36. $this->assertArraySubset([
  37. 'id' => 4,
  38. 'name' => 'banana',
  39. 'color' => 'red',
  40. 'weight' => '1 kg',
  41. 'delicious' => true,
  42. ], json_decode($results[0]['content'], true));
  43. }
  44. /** @test */
  45. public function uses_configured_settings_when_calling_route()
  46. {
  47. $route = LaravelRouteFacade::post('/echo/{id}', [TestController::class, 'shouldFetchRouteResponseWithEchoedSettings']);
  48. $rules = [
  49. 'response_calls' => [
  50. 'methods' => ['*'],
  51. 'queryParams' => [
  52. 'queryParam' => 'queryValue',
  53. ],
  54. 'bodyParams' => [
  55. 'bodyParam' => 'bodyValue',
  56. ],
  57. ],
  58. ];
  59. $context = [
  60. 'auth' => 'headers.Authorization.Bearer bearerToken',
  61. 'headers' => [
  62. 'Content-Type' => 'application/json',
  63. 'Accept' => 'application/json',
  64. 'header' => 'value',
  65. ],
  66. ];
  67. $strategy = new ResponseCalls(new DocumentationConfig([]));
  68. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
  69. $this->assertEquals(200, $results[0]['status']);
  70. $responseContent = json_decode($results[0]['content'], true);
  71. $this->assertEquals('queryValue', $responseContent['queryParam']);
  72. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  73. $this->assertEquals('value', $responseContent['header']);
  74. $this->assertEquals('Bearer bearerToken', $responseContent['auth']);
  75. }
  76. /** @test */
  77. public function can_override_application_config_during_response_call()
  78. {
  79. $route = LaravelRouteFacade::post('/echoesConfig', [TestController::class, 'echoesConfig']);
  80. $rules = [
  81. 'response_calls' => [
  82. 'methods' => ['*'],
  83. ],
  84. ];
  85. $strategy = new ResponseCalls(new DocumentationConfig([]));
  86. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
  87. $originalValue = json_decode($results[0]['content'], true)['app.env'];
  88. $now = time();
  89. $rules = [
  90. 'response_calls' => [
  91. 'methods' => ['*'],
  92. 'config' => [
  93. 'app.env' => $now,
  94. ],
  95. ],
  96. ];
  97. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
  98. $newValue = json_decode($results[0]['content'], true)['app.env'];
  99. $this->assertEquals($now, $newValue);
  100. $this->assertNotEquals($originalValue, $newValue);
  101. }
  102. /**
  103. * @test
  104. * @group dingo
  105. */
  106. public function can_call_route_and_fetch_response_with_dingo()
  107. {
  108. $route = $this->registerDingoRoute('post', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse');
  109. $rules = [
  110. 'headers' => [
  111. 'Content-Type' => 'application/json',
  112. 'Accept' => 'application/json',
  113. ],
  114. 'response_calls' => [
  115. 'methods' => ['*'],
  116. ],
  117. ];
  118. $strategy = new ResponseCalls(new DocumentationConfig([]));
  119. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
  120. $this->assertEquals(200, $results[0]['status']);
  121. $this->assertArraySubset([
  122. 'id' => 4,
  123. 'name' => 'banana',
  124. 'color' => 'red',
  125. 'weight' => '1 kg',
  126. 'delicious' => true,
  127. ], json_decode($results[0]['content'], true));
  128. }
  129. /**
  130. * @test
  131. * @group dingo
  132. */
  133. public function uses_configured_settings_when_calling_route_with_dingo()
  134. {
  135. $route = $this->registerDingoRoute('post','/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings');
  136. $rules = [
  137. 'response_calls' => [
  138. 'methods' => ['*'],
  139. 'queryParams' => [
  140. 'queryParam' => 'queryValue',
  141. ],
  142. 'bodyParams' => [
  143. 'bodyParam' => 'bodyValue',
  144. ],
  145. ],
  146. ];
  147. $context = [
  148. 'headers' => [
  149. 'Content-Type' => 'application/json',
  150. 'Accept' => 'application/json',
  151. 'header' => 'value',
  152. ],
  153. ];
  154. $strategy = new ResponseCalls(new DocumentationConfig([]));
  155. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
  156. $this->assertEquals(200, $results[0]['status']);
  157. $responseContent = json_decode($results[0]['content'], true);
  158. $this->assertEquals('queryValue', $responseContent['queryParam']);
  159. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  160. $this->assertEquals('value', $responseContent['header']);
  161. }
  162. /**
  163. * @test
  164. * @group dingo
  165. */
  166. public function can_override_application_config_during_response_call_with_dingo()
  167. {
  168. $route = $this->registerDingoRoute('post','/echoesConfig', 'echoesConfig');
  169. $rules = [
  170. 'response_calls' => [
  171. 'methods' => ['*'],
  172. ],
  173. ];
  174. $strategy = new ResponseCalls(new DocumentationConfig([]));
  175. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
  176. $originalValue = json_decode($results[0]['content'], true)['app.env'];
  177. $now = time();
  178. $rules = [
  179. 'response_calls' => [
  180. 'methods' => ['*'],
  181. 'config' => [
  182. 'app.env' => $now,
  183. ],
  184. ],
  185. ];
  186. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
  187. $newValue = json_decode($results[0]['content'], true)['app.env'];
  188. $this->assertEquals($now, $newValue);
  189. $this->assertNotEquals($originalValue, $newValue);
  190. }
  191. /** @test */
  192. public function does_not_make_response_call_if_success_response_already_gotten()
  193. {
  194. $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
  195. $rules = [
  196. 'headers' => [
  197. 'Content-Type' => 'application/json',
  198. 'Accept' => 'application/json',
  199. ],
  200. 'response_calls' => [
  201. 'methods' => ['*'],
  202. ],
  203. ];
  204. $context = [
  205. 'responses' => [
  206. [
  207. 'status' => 200,
  208. 'content' => json_encode(['message' => 'LOL']),
  209. ]
  210. ]
  211. ];
  212. $strategy = new ResponseCalls(new DocumentationConfig([]));
  213. $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
  214. $this->assertNull($results);
  215. }
  216. public function registerDingoRoute(string $httpMethod, string $path, string $controllerMethod)
  217. {
  218. $route = null;
  219. /** @var DingoRouter $api */
  220. $api = app(DingoRouter::class);
  221. $api->version('v1', function (DingoRouter $api) use ($controllerMethod, $path, $httpMethod, &$route) {
  222. $route = $api->$httpMethod($path, [TestController::class . $controllerMethod]);
  223. });
  224. return $route;
  225. }
  226. }