123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Knuckles\Scribe\Tests\Strategies\Responses;
- use Illuminate\Routing\Route;
- use Illuminate\Support\Facades\Route as RouteFacade;
- use Knuckles\Camel\Extraction\ExtractedEndpointData;
- use Knuckles\Camel\Extraction\ResponseCollection;
- use Knuckles\Scribe\Extracting\Extractor;
- use Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls;
- use Knuckles\Scribe\Scribe;
- use Knuckles\Scribe\Tests\BaseLaravelTest;
- use Knuckles\Scribe\Tests\Fixtures\TestController;
- use Knuckles\Scribe\Tools\DocumentationConfig;
- use Illuminate\Support\Facades\Route as LaravelRouteFacade;
- use Symfony\Component\HttpFoundation\Request;
- class ResponseCallsTest extends BaseLaravelTest
- {
- /** @test */
- public function can_call_route_and_fetch_response()
- {
- $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
- $responses = $this->invokeStrategy($route);
- $this->assertEquals(200, $responses[0]['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($responses[0]['content'], true));
- }
- /** @test */
- public function can_upload_file_parameters_in_response_calls()
- {
- $route = RouteFacade::post('/withFormDataParams', [TestController::class, 'withFormDataParams']);
- /* This doesn't work. It always gives an error, "the file failed to upload". However, uploading files worked when they are extracted params
- $endpointData = ExtractedEndpointData::fromRoute($route, [ 'headers' => [ 'accept' => 'application/json' ] ]);
- $responses = $this->invokeStrategy($endpointData, settings: [
- 'fileParams' => [ 'image' => 'config/scribe.php' ],
- 'bodyParams' => [ 'name' => 'cat.jpg' ]
- ]);
- */
- $this->setConfig([
- 'strategies.responses' => [
- [ResponseCalls::class,
- ['only' => 'POST *']
- ],
- ]
- ]);
- $parsed = (new Extractor())->processRoute($route);
- $responses = $parsed->responses->toArray();
- $this->assertCount(1, $responses);
- $this->assertArraySubset([
- "status" => 200,
- "description" => null,
- "content" => '{"filename":"scribe.php","filepath":"config","name":"cat.jpg"}',
- ], $responses[0]);
- }
- /** @test */
- public function uses_configured_settings_when_calling_route()
- {
- $route = LaravelRouteFacade::post('/echo/{id}', [TestController::class, 'echoesRequestValues']);
- $endpointData = ExtractedEndpointData::fromRoute($route, [
- 'auth' => ['headers', 'Authorization', 'Bearer bearerToken'],
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- 'header' => 'headerValue',
- ],
- ]);
- $responses = $this->invokeStrategy($endpointData, settings: [
- 'queryParams' => [
- 'queryParam' => 'queryValue',
- ],
- 'bodyParams' => [
- 'bodyParam' => 'bodyValue',
- ],
- ]);
- $this->assertEquals(200, $responses[0]['status']);
- $responseContent = json_decode($responses[0]['content'], true);
- $this->assertEquals('queryValue', $responseContent['queryParam']);
- $this->assertEquals('bodyValue', $responseContent['bodyParam']);
- $this->assertEquals('headerValue', $responseContent['header']);
- $this->assertEquals('Bearer bearerToken', $responseContent['auth']);
- }
- /** @test */
- public function can_override_application_config_during_response_call()
- {
- $route = LaravelRouteFacade::post('/echoesConfig', [TestController::class, 'echoesConfig']);
- $responses = $this->invokeStrategy($route);
- $originalValue = json_decode($responses[0]['content'], true)['app.env'];
- $now = time();
- $responses = $this->invokeStrategy($route, settings: [
- 'config' => [
- 'app.env' => $now,
- ],
- ],);
- $newValue = json_decode($responses[0]['content'], true)['app.env'];
- $this->assertEquals($now, $newValue);
- $this->assertNotEquals($originalValue, $newValue);
- }
- /** @test */
- public function calls_beforeResponseCall_hook()
- {
- Scribe::beforeResponseCall(function (Request $request, ExtractedEndpointData $endpointData) {
- $request->headers->set("header", "overridden_".$request->headers->get("header"));
- $request->headers->set("Authorization", "overridden_".$request->headers->get("Authorization"));
- $request->query->set("queryParam", "overridden_".$request->query->get("queryParam"));
- $request->request->set("bodyParam", "overridden_".$endpointData->uri.$request->request->get("bodyParam"));
- });
- $route = LaravelRouteFacade::post('/echo/{id}', [TestController::class, 'echoesRequestValues']);
- $endpointData = ExtractedEndpointData::fromRoute($route, [
- 'auth' => ['headers', 'Authorization', 'Bearer bearerToken'],
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- 'header' => 'headerValue',
- ],
- ]);
- $responses = $this->invokeStrategy($endpointData, settings: [
- 'queryParams' => [
- 'queryParam' => 'queryValue',
- ],
- 'bodyParams' => [
- 'bodyParam' => 'bodyValue',
- ],
- ]);
- $this->assertEquals(200, $responses[0]['status']);
- $responseContent = json_decode($responses[0]['content'], true);
- $this->assertEquals('overridden_queryValue', $responseContent['queryParam']);
- $this->assertEquals('overridden_headerValue', $responseContent['header']);
- $this->assertEquals('overridden_Bearer bearerToken', $responseContent['auth']);
- $this->assertEquals('overridden_echo/{id}bodyValue', $responseContent['bodyParam']);
- Scribe::beforeResponseCall(fn() => null);
- }
- /** @test */
- public function does_not_make_response_call_if_success_response_already_gotten()
- {
- $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
- $endpointData = ExtractedEndpointData::fromRoute($route, [
- 'responses' => new ResponseCollection([
- [
- 'status' => 200,
- 'content' => json_encode(['message' => 'LOL']),
- ],
- ]),
- ]);
- $responses = $this->invokeStrategy($endpointData);
- $this->assertNull($responses);
- }
- protected function convertRules(array $rules): mixed
- {
- return Extractor::transformOldRouteRulesIntoNewSettings('responses', $rules, ResponseCalls::class);
- }
- protected function invokeStrategy(ExtractedEndpointData|Route $route, $settings = []): ?array
- {
- $strategy = new ResponseCalls(new DocumentationConfig([]));
- return $strategy(
- $route instanceof ExtractedEndpointData ? $route : ExtractedEndpointData::fromRoute($route), $settings
- );
- }
- }
|