123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- namespace Knuckles\Scribe\Tests\Strategies\Responses;
- use Dingo\Api\Routing\Router;
- use Illuminate\Routing\Route;
- use Illuminate\Support\Facades\Route as RouteFacade;
- use Knuckles\Scribe\Extracting\Generator;
- use Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls;
- use Knuckles\Scribe\ScribeServiceProvider;
- use Knuckles\Scribe\Tests\Fixtures\TestController;
- use Knuckles\Scribe\Tools\DocumentationConfig;
- use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
- use Orchestra\Testbench\TestCase;
- use Illuminate\Support\Facades\Route as LaravelRouteFacade;
- class ResponseCallsTest extends TestCase
- {
- use ArraySubsetAsserts;
- protected function getPackageProviders($app)
- {
- $providers = [
- ScribeServiceProvider::class,
- ];
- if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
- $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
- }
- return $providers;
- }
- /** @test */
- public function can_call_route_and_fetch_response()
- {
- $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
- $rules = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ],
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig([]));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
- $this->assertEquals(200, $results[0]['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($results[0]['content'], true));
- }
- /** @test */
- public function can_upload_file_parameters_in_response_calls()
- {
- $route = RouteFacade::post('/withFormDataParams', [TestController::class, 'withFormDataParams']);
- config(['scribe.routes.0.apply.response_calls.methods' => ['POST']]);
- $parsed = (new Generator())->processRoute($route, config('scribe.routes.0.apply'));
- $this->assertEquals([
- [
- "status" => 200,
- "content" => '{"filename":"scribe.php","filepath":"config","name":"cat.jpg"}',
- ],
- ], $parsed['responses']);
- }
- /** @test */
- public function uses_configured_settings_when_calling_route()
- {
- $route = LaravelRouteFacade::post('/echo/{id}', [TestController::class, 'shouldFetchRouteResponseWithEchoedSettings']);
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'queryParams' => [
- 'queryParam' => 'queryValue',
- ],
- 'bodyParams' => [
- 'bodyParam' => 'bodyValue',
- ],
- ],
- ];
- $context = [
- 'auth' => 'headers.Authorization.Bearer bearerToken',
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- 'header' => 'value',
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig([]));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
- $this->assertEquals(200, $results[0]['status']);
- $responseContent = json_decode($results[0]['content'], true);
- $this->assertEquals('queryValue', $responseContent['queryParam']);
- $this->assertEquals('bodyValue', $responseContent['bodyParam']);
- $this->assertEquals('value', $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']);
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig([]));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
- $originalValue = json_decode($results[0]['content'], true)['app.env'];
- $now = time();
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'config' => [
- 'app.env' => $now,
- ],
- ],
- ];
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
- $newValue = json_decode($results[0]['content'], true)['app.env'];
- $this->assertEquals($now, $newValue);
- $this->assertNotEquals($originalValue, $newValue);
- }
- /**
- * @test
- * @group dingo
- */
- public function can_call_route_and_fetch_response_with_dingo()
- {
- $route = $this->registerDingoRoute('post', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse');
- $rules = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ],
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig(['router' => 'dingo']));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
- $this->assertEquals(200, $results[0]['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($results[0]['content'], true));
- }
- /**
- * @test
- * @group dingo
- */
- public function uses_configured_settings_when_calling_route_with_dingo()
- {
- $route = $this->registerDingoRoute('post', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings');
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'queryParams' => [
- 'queryParam' => 'queryValue',
- ],
- 'bodyParams' => [
- 'bodyParam' => 'bodyValue',
- ],
- ],
- ];
- $context = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- 'header' => 'value',
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig(['router' => 'dingo']));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
- $this->assertEquals(200, $results[0]['status']);
- $responseContent = json_decode($results[0]['content'], true);
- $this->assertEquals('queryValue', $responseContent['queryParam']);
- $this->assertEquals('bodyValue', $responseContent['bodyParam']);
- $this->assertEquals('value', $responseContent['header']);
- }
- /**
- * @test
- * @group dingo
- */
- public function can_override_application_config_during_response_call_with_dingo()
- {
- $route = $this->registerDingoRoute('post', '/echoesConfig', 'echoesConfig');
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig(['router' => 'dingo']));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
- $originalValue = json_decode($results[0]['content'], true)['app.env'];
- $now = time();
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'config' => [
- 'app.env' => $now,
- ],
- ],
- ];
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, []);
- $newValue = json_decode($results[0]['content'], true)['app.env'];
- $this->assertEquals($now, $newValue);
- $this->assertNotEquals($originalValue, $newValue);
- }
- /** @test */
- public function does_not_make_response_call_if_success_response_already_gotten()
- {
- $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
- $rules = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ],
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $context = [
- 'responses' => [
- [
- 'status' => 200,
- 'content' => json_encode(['message' => 'LOL']),
- ],
- ],
- ];
- $strategy = new ResponseCalls(new DocumentationConfig([]));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
- $this->assertNull($results);
- }
- /** @test */
- public function does_not_make_response_call_if_forbidden_by_config()
- {
- $route = LaravelRouteFacade::post('/shouldFetchRouteResponse', [TestController::class, 'shouldFetchRouteResponse']);
- $rules = [
- 'response_calls' => [
- 'methods' => [],
- ],
- ];
- $context = ['responses' => []];
- $strategy = new ResponseCalls(new DocumentationConfig([]));
- $results = $strategy->makeResponseCallIfEnabledAndNoSuccessResponses($route, $rules, $context);
- $this->assertNull($results);
- }
- public function registerDingoRoute(string $httpMethod, string $path, string $controllerMethod)
- {
- $desiredRoute = null;
- /** @var Router $api */
- $api = app(Router::class);
- $api->version('v1', function (Router $api) use ($controllerMethod, $path, $httpMethod, &$desiredRoute) {
- $desiredRoute = $api->$httpMethod($path, [TestController::class, $controllerMethod]);
- });
- $routes = app(\Dingo\Api\Routing\Router::class)->getRoutes('v1');
- /*
- * Doing this bc we want an instance of Dingo\Api\Routing\Route, not Illuminate\Routing\Route, which the method above returns
- */
- return collect($routes)
- ->first(function (Route $route) use ($desiredRoute) {
- return $route->uri() === $desiredRoute->uri();
- });
- }
- }
|