|
@@ -17,34 +17,21 @@ use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
class ResponseCallsTest extends BaseLaravelTest
|
|
|
{
|
|
|
- protected function setUp(): void
|
|
|
- {
|
|
|
- parent::setUp();
|
|
|
- $this->setConfig(['database_connections_to_transact' => []]);
|
|
|
- }
|
|
|
-
|
|
|
/** @test */
|
|
|
public function can_call_route_and_fetch_response()
|
|
|
{
|
|
|
$route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
|
|
|
|
|
|
- $rules = [
|
|
|
- 'response_calls' => [
|
|
|
- 'methods' => ['*'],
|
|
|
- ],
|
|
|
- ];
|
|
|
-
|
|
|
- $strategy = new ResponseCalls(new DocumentationConfig([]));
|
|
|
- $results = $strategy(ExtractedEndpointData::fromRoute($route), $this->convertRules($rules));
|
|
|
+ $responses = $this->invokeStrategy($route, settings: ['methods' => '*']);
|
|
|
|
|
|
- $this->assertEquals(200, $results[0]['status']);
|
|
|
+ $this->assertEquals(200, $responses[0]['status']);
|
|
|
$this->assertArraySubset([
|
|
|
'id' => 4,
|
|
|
'name' => 'banana',
|
|
|
'color' => 'red',
|
|
|
'weight' => '1 kg',
|
|
|
'delicious' => true,
|
|
|
- ], json_decode($results[0]['content'], true));
|
|
|
+ ], json_decode($responses[0]['content'], true));
|
|
|
}
|
|
|
|
|
|
/** @test */
|
|
@@ -52,17 +39,8 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
{
|
|
|
$route = RouteFacade::post('/withFormDataParams', [TestController::class, 'withFormDataParams']);
|
|
|
|
|
|
- $this->setConfig([
|
|
|
- 'routes.0.apply.response_calls' => [],
|
|
|
- 'strategies.responses' => [
|
|
|
- [ResponseCalls::class,
|
|
|
- ['only' => 'POST *']
|
|
|
- ],
|
|
|
- ]
|
|
|
- ]);
|
|
|
- $parsed = (new Extractor())->processRoute($route, config('scribe.routes.0.apply'));
|
|
|
+ $responses = $this->invokeStrategy($route, settings: ['methods' => 'POST *']);
|
|
|
|
|
|
- $responses = $parsed->responses->toArray();
|
|
|
$this->assertCount(1, $responses);
|
|
|
$this->assertArraySubset([
|
|
|
"status" => 200,
|
|
@@ -76,18 +54,6 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
{
|
|
|
$route = LaravelRouteFacade::post('/echo/{id}', [TestController::class, 'echoesRequestValues']);
|
|
|
|
|
|
- $rules = [
|
|
|
- 'response_calls' => [
|
|
|
- 'methods' => ['*'],
|
|
|
- 'queryParams' => [
|
|
|
- 'queryParam' => 'queryValue',
|
|
|
- ],
|
|
|
- 'bodyParams' => [
|
|
|
- 'bodyParam' => 'bodyValue',
|
|
|
- ],
|
|
|
- ],
|
|
|
- ];
|
|
|
-
|
|
|
$endpointData = ExtractedEndpointData::fromRoute($route, [
|
|
|
'auth' => ['headers', 'Authorization', 'Bearer bearerToken'],
|
|
|
'headers' => [
|
|
@@ -97,13 +63,19 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
],
|
|
|
]);
|
|
|
|
|
|
- $strategy = new ResponseCalls(new DocumentationConfig([]));
|
|
|
- $results = $strategy($endpointData,
|
|
|
- $this->convertRules($rules));
|
|
|
+ $responses = $this->invokeStrategy($endpointData, settings: [
|
|
|
+ 'methods' => ['*'],
|
|
|
+ 'queryParams' => [
|
|
|
+ 'queryParam' => 'queryValue',
|
|
|
+ ],
|
|
|
+ 'bodyParams' => [
|
|
|
+ 'bodyParam' => 'bodyValue',
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
|
|
|
- $this->assertEquals(200, $results[0]['status']);
|
|
|
+ $this->assertEquals(200, $responses[0]['status']);
|
|
|
|
|
|
- $responseContent = json_decode($results[0]['content'], true);
|
|
|
+ $responseContent = json_decode($responses[0]['content'], true);
|
|
|
$this->assertEquals('queryValue', $responseContent['queryParam']);
|
|
|
$this->assertEquals('bodyValue', $responseContent['bodyParam']);
|
|
|
$this->assertEquals('headerValue', $responseContent['header']);
|
|
@@ -114,29 +86,17 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
public function can_override_application_config_during_response_call()
|
|
|
{
|
|
|
$route = LaravelRouteFacade::post('/echoesConfig', [TestController::class, 'echoesConfig']);
|
|
|
-
|
|
|
- $rules = [
|
|
|
- 'response_calls' => [
|
|
|
- 'methods' => ['*'],
|
|
|
- ],
|
|
|
- ];
|
|
|
-
|
|
|
- $strategy = new ResponseCalls(new DocumentationConfig([]));
|
|
|
- $results = $strategy(ExtractedEndpointData::fromRoute($route), $this->convertRules($rules));
|
|
|
- $originalValue = json_decode($results[0]['content'], true)['app.env'];
|
|
|
+ $responses = $this->invokeStrategy($route, settings: ['methods' => '*']);
|
|
|
+ $originalValue = json_decode($responses[0]['content'], true)['app.env'];
|
|
|
|
|
|
$now = time();
|
|
|
- $rules = [
|
|
|
- 'response_calls' => [
|
|
|
- 'methods' => ['*'],
|
|
|
- 'config' => [
|
|
|
- 'app.env' => $now,
|
|
|
- ],
|
|
|
+ $responses = $this->invokeStrategy($route, settings: [
|
|
|
+ 'methods' => ['*'],
|
|
|
+ 'config' => [
|
|
|
+ 'app.env' => $now,
|
|
|
],
|
|
|
- ];
|
|
|
-
|
|
|
- $results = $strategy(ExtractedEndpointData::fromRoute($route), $this->convertRules($rules));
|
|
|
- $newValue = json_decode($results[0]['content'], true)['app.env'];
|
|
|
+ ],);
|
|
|
+ $newValue = json_decode($responses[0]['content'], true)['app.env'];
|
|
|
$this->assertEquals($now, $newValue);
|
|
|
$this->assertNotEquals($originalValue, $newValue);
|
|
|
}
|
|
@@ -153,18 +113,6 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
|
|
|
$route = LaravelRouteFacade::post('/echo/{id}', [TestController::class, 'echoesRequestValues']);
|
|
|
|
|
|
- $rules = [
|
|
|
- 'response_calls' => [
|
|
|
- 'methods' => ['*'],
|
|
|
- 'queryParams' => [
|
|
|
- 'queryParam' => 'queryValue',
|
|
|
- ],
|
|
|
- 'bodyParams' => [
|
|
|
- 'bodyParam' => 'bodyValue',
|
|
|
- ],
|
|
|
- ],
|
|
|
- ];
|
|
|
-
|
|
|
$endpointData = ExtractedEndpointData::fromRoute($route, [
|
|
|
'auth' => ['headers', 'Authorization', 'Bearer bearerToken'],
|
|
|
'headers' => [
|
|
@@ -173,13 +121,19 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
'header' => 'headerValue',
|
|
|
],
|
|
|
]);
|
|
|
+ $responses = $this->invokeStrategy($endpointData, settings: [
|
|
|
+ 'methods' => ['*'],
|
|
|
+ 'queryParams' => [
|
|
|
+ 'queryParam' => 'queryValue',
|
|
|
+ ],
|
|
|
+ 'bodyParams' => [
|
|
|
+ 'bodyParam' => 'bodyValue',
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
|
|
|
- $strategy = new ResponseCalls(new DocumentationConfig([]));
|
|
|
- $results = $strategy($endpointData, $this->convertRules($rules));
|
|
|
-
|
|
|
- $this->assertEquals(200, $results[0]['status']);
|
|
|
+ $this->assertEquals(200, $responses[0]['status']);
|
|
|
|
|
|
- $responseContent = json_decode($results[0]['content'], true);
|
|
|
+ $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']);
|
|
@@ -193,12 +147,6 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
{
|
|
|
$route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
|
|
|
|
|
|
- $rules = [
|
|
|
- 'response_calls' => [
|
|
|
- 'methods' => ['*'],
|
|
|
- ],
|
|
|
- ];
|
|
|
-
|
|
|
$endpointData = ExtractedEndpointData::fromRoute($route, [
|
|
|
'responses' => new ResponseCollection([
|
|
|
[
|
|
@@ -207,14 +155,21 @@ class ResponseCallsTest extends BaseLaravelTest
|
|
|
],
|
|
|
]),
|
|
|
]);
|
|
|
- $strategy = new ResponseCalls(new DocumentationConfig([]));
|
|
|
- $results = $strategy($endpointData, $this->convertRules($rules));
|
|
|
+ $responses = $this->invokeStrategy($endpointData, settings: ['methods' => '*']);
|
|
|
|
|
|
- $this->assertNull($results);
|
|
|
+ $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
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|