123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966 |
- <?php
- /** @noinspection ALL */
- namespace Mpociot\ApiDoc\Tests\Unit;
- use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
- use Illuminate\Support\Arr;
- use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
- use Mpociot\ApiDoc\Extracting\Generator;
- use Mpociot\ApiDoc\Tests\Fixtures\TestController;
- use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
- use Mpociot\ApiDoc\Tools\DocumentationConfig;
- use Orchestra\Testbench\TestCase;
- abstract class GeneratorTestCase extends TestCase
- {
- use ArraySubsetAsserts;
- /**
- * @var \Mpociot\ApiDoc\Extracting\Generator
- */
- protected $generator;
- private $config = [
- 'strategies' => [
- 'metadata' => [
- \Mpociot\ApiDoc\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
- ],
- 'urlParameters' => [
- \Mpociot\ApiDoc\Extracting\Strategies\UrlParameters\GetFromUrlParamTag::class,
- ],
- 'queryParameters' => [
- \Mpociot\ApiDoc\Extracting\Strategies\QueryParameters\GetFromQueryParamTag::class,
- ],
- 'headers' => [
- \Mpociot\ApiDoc\Extracting\Strategies\RequestHeaders\GetFromRouteRules::class,
- ],
- 'bodyParameters' => [
- \Mpociot\ApiDoc\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
- ],
- 'responses' => [
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\UseTransformerTags::class,
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseTag::class,
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseFileTag::class,
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\UseApiResourceTags::class,
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\ResponseCalls::class,
- ],
- ],
- 'default_group' => 'general',
- ];
- public static $globalValue = null;
- protected function getPackageProviders($app)
- {
- return [
- ApiDocGeneratorServiceProvider::class,
- ];
- }
- /**
- * Setup the test environment.
- */
- public function setUp(): void
- {
- parent::setUp();
- $factory = app(\Illuminate\Database\Eloquent\Factory::class);
- $factory->define(TestUser::class, function () {
- return [
- 'id' => 4,
- 'first_name' => 'Tested',
- 'last_name' => 'Again',
- 'email' => 'a@b.com',
- ];
- });
- $this->generator = new Generator(new DocumentationConfig($this->config));
- }
- /** @test */
- public function can_parse_endpoint_description()
- {
- $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame('Example title.', $parsed['metadata']['title']);
- $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['metadata']['description']);
- }
- /** @test */
- public function can_parse_body_parameters()
- {
- $route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
- $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
- $this->assertArraySubset([
- 'user_id' => [
- 'type' => 'integer',
- 'required' => true,
- 'description' => 'The id of the user.',
- 'value' => 9,
- ],
- 'room_id' => [
- 'type' => 'string',
- 'required' => false,
- 'description' => 'The id of the room.',
- ],
- 'forever' => [
- 'type' => 'boolean',
- 'required' => false,
- 'description' => 'Whether to ban the user forever.',
- 'value' => false,
- ],
- 'another_one' => [
- 'type' => 'number',
- 'required' => false,
- 'description' => 'Just need something here.',
- ],
- 'yet_another_param' => [
- 'type' => 'object',
- 'required' => true,
- 'description' => 'Some object params.',
- ],
- 'yet_another_param.name' => [
- 'type' => 'string',
- 'description' => 'Subkey in the object param.',
- 'required' => true,
- ],
- 'even_more_param' => [
- 'type' => 'array',
- 'required' => false,
- 'description' => 'Some array params.',
- ],
- 'even_more_param.*' => [
- 'type' => 'float',
- 'description' => 'Subkey in the array param.',
- 'required' => false,
- ],
- 'book.name' => [
- 'type' => 'string',
- 'description' => '',
- 'required' => false,
- ],
- 'book.author_id' => [
- 'type' => 'integer',
- 'description' => '',
- 'required' => false,
- ],
- 'book[pages_count]' => [
- 'type' => 'integer',
- 'description' => '',
- 'required' => false,
- ],
- 'ids.*' => [
- 'type' => 'integer',
- 'description' => '',
- 'required' => false,
- ],
- 'users.*.first_name' => [
- 'type' => 'string',
- 'description' => 'The first name of the user.',
- 'required' => false,
- 'value' => 'John',
- ],
- 'users.*.last_name' => [
- 'type' => 'string',
- 'description' => 'The last name of the user.',
- 'required' => false,
- 'value' => 'Doe',
- ],
- ], $bodyParameters);
- }
- /** @test */
- public function it_ignores_non_commented_form_request()
- {
- $route = $this->createRoute('GET', '/api/test', 'withNonCommentedFormRequestParameter');
- $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
- $this->assertArraySubset([
- 'direct_one' => [
- 'type' => 'string',
- 'description' => 'Is found directly on the method.',
- ],
- ], $bodyParameters);
- }
- /** @test */
- public function can_parse_form_request_body_parameters()
- {
- $route = $this->createRoute('GET', '/api/test', 'withFormRequestParameter');
- $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
- $this->assertArraySubset([
- 'user_id' => [
- 'type' => 'integer',
- 'required' => true,
- 'description' => 'The id of the user.',
- 'value' => 9,
- ],
- 'room_id' => [
- 'type' => 'string',
- 'required' => false,
- 'description' => 'The id of the room.',
- ],
- 'forever' => [
- 'type' => 'boolean',
- 'required' => false,
- 'description' => 'Whether to ban the user forever.',
- 'value' => false,
- ],
- 'another_one' => [
- 'type' => 'number',
- 'required' => false,
- 'description' => 'Just need something here.',
- ],
- 'yet_another_param' => [
- 'type' => 'object',
- 'required' => true,
- 'description' => '',
- ],
- 'even_more_param' => [
- 'type' => 'array',
- 'required' => false,
- 'description' => '',
- ],
- ], $bodyParameters);
- }
- /** @test */
- public function can_parse_multiple_form_request_body_parameters()
- {
- $route = $this->createRoute('GET', '/api/test', 'withMultipleFormRequestParameters');
- $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
- $this->assertArraySubset([
- 'user_id' => [
- 'type' => 'integer',
- 'required' => true,
- 'description' => 'The id of the user.',
- 'value' => 9,
- ],
- 'room_id' => [
- 'type' => 'string',
- 'required' => false,
- 'description' => 'The id of the room.',
- ],
- 'forever' => [
- 'type' => 'boolean',
- 'required' => false,
- 'description' => 'Whether to ban the user forever.',
- 'value' => false,
- ],
- 'another_one' => [
- 'type' => 'number',
- 'required' => false,
- 'description' => 'Just need something here.',
- ],
- 'yet_another_param' => [
- 'type' => 'object',
- 'required' => true,
- 'description' => '',
- ],
- 'even_more_param' => [
- 'type' => 'array',
- 'required' => false,
- 'description' => '',
- ],
- ], $bodyParameters);
- }
- /** @test */
- public function can_parse_query_parameters()
- {
- $route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
- $queryParameters = $this->generator->processRoute($route)['queryParameters'];
- $this->assertArraySubset([
- 'location_id' => [
- 'required' => true,
- 'description' => 'The id of the location.',
- ],
- 'user_id' => [
- 'required' => true,
- 'description' => 'The id of the user.',
- 'value' => 'me',
- ],
- 'page' => [
- 'required' => true,
- 'description' => 'The page number.',
- 'value' => '4',
- ],
- 'filters' => [
- 'required' => false,
- 'description' => 'The filters.',
- ],
- ], $queryParameters);
- }
- /** @test */
- public function it_does_not_generate_values_for_excluded_params_and_excludes_them_from_clean_params()
- {
- $route = $this->createRoute('GET', '/api/test', 'withExcludedExamples');
- $parsed = $this->generator->processRoute($route);
- $cleanBodyParameters = $parsed['cleanBodyParameters'];
- $cleanQueryParameters = $parsed['cleanQueryParameters'];
- $bodyParameters = $parsed['bodyParameters'];
- $queryParameters = $parsed['queryParameters'];
- $this->assertArrayHasKey('included', $cleanBodyParameters);
- $this->assertArrayNotHasKey('excluded_body_param', $cleanBodyParameters);
- $this->assertEmpty($cleanQueryParameters);
- $this->assertArraySubset([
- 'included' => [
- 'required' => true,
- 'type' => 'string',
- 'description' => 'Exists in examples.',
- ],
- 'excluded_body_param' => [
- 'type' => 'integer',
- 'description' => 'Does not exist in examples.',
- ],
- ], $bodyParameters);
- $this->assertArraySubset([
- 'excluded_query_param' => [
- 'description' => 'Does not exist in examples.',
- ],
- ], $queryParameters);
- }
- /** @test */
- public function can_parse_route_group()
- {
- $route = $this->createRoute('GET', '/api/test', 'dummy');
- $routeGroup = $this->generator->processRoute($route)['metadata']['groupName'];
- $this->assertSame('Group A', $routeGroup);
- }
- /** @test */
- public function method_can_override_controller_group()
- {
- $route = $this->createRoute('GET', '/group/1', 'withGroupOverride');
- $parsedRoute = $this->generator->processRoute($route);
- $this->assertSame('Group B', $parsedRoute['metadata']['groupName']);
- $this->assertSame('', $parsedRoute['metadata']['groupDescription']);
- $route = $this->createRoute('GET', '/group/2', 'withGroupOverride2');
- $parsedRoute = $this->generator->processRoute($route);
- $this->assertSame('Group B', $parsedRoute['metadata']['groupName']);
- $this->assertSame('', $parsedRoute['metadata']['groupDescription']);
- $this->assertSame('This is also in Group B. No route description. Route title before gropp.', $parsedRoute['metadata']['title']);
- $route = $this->createRoute('GET', '/group/3', 'withGroupOverride3');
- $parsedRoute = $this->generator->processRoute($route);
- $this->assertSame('Group B', $parsedRoute['metadata']['groupName']);
- $this->assertSame('', $parsedRoute['metadata']['groupDescription']);
- $this->assertSame('This is also in Group B. Route title after group.', $parsedRoute['metadata']['title']);
- $route = $this->createRoute('GET', '/group/4', 'withGroupOverride4');
- $parsedRoute = $this->generator->processRoute($route);
- $this->assertSame('Group C', $parsedRoute['metadata']['groupName']);
- $this->assertSame('Group description after group.', $parsedRoute['metadata']['groupDescription']);
- $this->assertSame('This is in Group C. Route title before group.', $parsedRoute['metadata']['title']);
- }
- /** @test */
- public function can_parse_auth_tags()
- {
- $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
- $authenticated = $this->generator->processRoute($route)['metadata']['authenticated'];
- $this->assertTrue($authenticated);
- $route = $this->createRoute('GET', '/api/test', 'dummy');
- $authenticated = $this->generator->processRoute($route)['metadata']['authenticated'];
- $this->assertFalse($authenticated);
- }
- /** @test */
- public function can_parse_route_methods()
- {
- $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame(['GET'], $parsed['methods']);
- $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame(['POST'], $parsed['methods']);
- $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame(['PUT'], $parsed['methods']);
- $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame(['DELETE'], $parsed['methods']);
- }
- /** @test */
- public function can_parse_apiresource_tags()
- {
- $route = $this->createRoute('POST', '/withEloquentApiResource', 'withEloquentApiResource');
- $config = $this->config;
- $config['strategies']['responses'] = [\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseApiResourceTags::class];
- $generator = new Generator(new DocumentationConfig($config));
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertArraySubset([
- 'data' => [
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ],
- ], json_decode($response['content'], true));
- }
- /** @test */
- public function can_parse_apiresourcecollection_tags()
- {
- $route = $this->createRoute('POST', '/withEloquentApiResourceCollection', 'withEloquentApiResourceCollection');
- $config = $this->config;
- $config['strategies']['responses'] = [\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseApiResourceTags::class];
- $generator = new Generator(new DocumentationConfig($config));
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $content = json_decode($response['content'], true);
- $this->assertIsArray($content);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ], $content['data'][0]);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ], $content['data'][1]);
- }
- /** @test */
- public function can_parse_apiresourcecollection_tags_with_collection_class()
- {
- $route = $this->createRoute('POST', '/withEloquentApiResourceCollectionClass', 'withEloquentApiResourceCollectionClass');
- $config = $this->config;
- $config['strategies']['responses'] = [\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseApiResourceTags::class];
- $generator = new Generator(new DocumentationConfig($config));
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $content = json_decode($response['content'], true);
- $this->assertIsArray($content);
- $this->assertArraySubset([
- 'data' => [
- [
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ],
- [
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ],
- ],
- 'links' => [
- 'self' => 'link-value',
- ],
- ], $content);
- }
- /** @test */
- public function can_parse_response_tag()
- {
- $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($response['content'], true));
- }
- /** @test */
- public function can_parse_response_tag_with_status_code()
- {
- $route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(422, $response['status']);
- $this->assertArraySubset([
- 'message' => 'Validation error',
- ], json_decode($response['content'], true));
- }
- /** @test */
- public function can_parse_multiple_response_tags()
- {
- $route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($parsed['responses'][0]));
- $this->assertEquals(200, $parsed['responses'][0]['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($parsed['responses'][0]['content'], true));
- $this->assertTrue(is_array($parsed['responses'][1]));
- $this->assertEquals(401, $parsed['responses'][1]['status']);
- $this->assertArraySubset([
- 'message' => 'Unauthorized',
- ], json_decode($parsed['responses'][1]['content'], true));
- }
- /**
- * @param $serializer
- * @param $expected
- *
- * @test
- * @dataProvider dataResources
- */
- public function can_parse_transformer_tag($serializer, $expected)
- {
- config(['apidoc.fractal.serializer' => $serializer]);
- $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertSame(
- $expected,
- $response['content']
- );
- }
- /** @test */
- public function can_parse_transformer_tag_with_model()
- {
- $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertSame(
- '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
- $response['content']
- );
- }
- /** @test */
- public function can_parse_transformer_tag_with_status_code()
- {
- $route = $this->createRoute('GET', '/transformerTagWithStatusCode', 'transformerTagWithStatusCode');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(201, $response['status']);
- $this->assertSame(
- '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
- $response['content']
- );
- }
- /** @test */
- public function can_parse_transformer_collection_tag()
- {
- $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertSame(
- $response['content'],
- '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
- '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
- );
- }
- /** @test */
- public function can_parse_transformer_collection_tag_with_model()
- {
- $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertSame(
- $response['content'],
- '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
- '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
- );
- }
- /** @test */
- public function can_call_route_and_generate_response()
- {
- $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
- $rules = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ],
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($response['content'], true));
- }
- /** @test */
- public function does_not_make_response_call_if_success_response_already_gotten()
- {
- $route = $this->createRoute('POST', '/withResponseTag', 'withResponseTag', true);
- $rules = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ],
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $config = [
- 'strategies' => [
- 'responses' => [
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseTag::class,
- \Mpociot\ApiDoc\Extracting\Strategies\Responses\ResponseCalls::class,
- ],
- ],
- ];
- $generator = new Generator(new DocumentationConfig($config));
- $parsed = $generator->processRoute($route, $rules);
- $this->assertCount(1, $parsed['responses']);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- 'responseTag' => true,
- ], json_decode($response['content'], true));
- // This may probably not be the best way to test this, but 🤷♀️
- $this->assertNull(static::$globalValue);
- }
- /** @test */
- public function can_override_config_during_response_call()
- {
- $route = $this->createRoute('POST', '/echoesConfig', 'echoesConfig', true);
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $response = json_decode(Arr::first($parsed['responses'])['content'], true);
- $originalValue = $response['app.env'];
- $now = time();
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'config' => [
- 'app.env' => $now,
- ],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $response = json_decode(Arr::first($parsed['responses'])['content'], true);
- $newValue = $response['app.env'];
- $this->assertEquals($now, $newValue);
- $this->assertNotEquals($originalValue, $newValue);
- }
- /** @test */
- public function can_override_url_path_parameters_with_urlparam_annotation()
- {
- $route = $this->createRoute('POST', '/echoesUrlParameters/{param}', 'echoesUrlParameters', true);
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $response = json_decode(Arr::first($parsed['responses'])['content'], true);
- $this->assertEquals(4, $response['param']);
- }
- /** @test */
- public function ignores_or_inserts_optional_url_path_parameters_according_to_annotations()
- {
- $route = $this->createRoute('POST', '/echoesUrlParameters/{param}/{param2?}/{param3}/{param4?}', 'echoesUrlParameters', true);
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $response = json_decode(Arr::first($parsed['responses'])['content'], true);
- $this->assertEquals(4, $response['param']);
- $this->assertNotNull($response['param2']);
- $this->assertEquals(1, $response['param3']);
- $this->assertNull($response['param4']);
- }
- /** @test */
- public function can_parse_response_file_tag()
- {
- // copy file to storage
- $filePath = __DIR__.'/../Fixtures/response_test.json';
- $fixtureFileJson = file_get_contents($filePath);
- copy($filePath, storage_path('response_test.json'));
- $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertSame(
- $response['content'],
- $fixtureFileJson
- );
- unlink(storage_path('response_test.json'));
- }
- /** @test */
- public function can_add_or_replace_key_value_pair_in_response_file()
- {
- // copy file to storage
- $filePath = __DIR__.'/../Fixtures/response_test.json';
- $fixtureFileJson = file_get_contents($filePath);
- copy($filePath, storage_path('response_test.json'));
- $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
- $parsed = $this->generator->processRoute($route);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $this->assertNotSame(
- $response['content'],
- $fixtureFileJson
- );
- unlink(storage_path('response_test.json'));
- }
- /** @test */
- public function can_parse_multiple_response_file_tags_with_status_codes()
- {
- // copy file to storage
- $successFilePath = __DIR__.'/../Fixtures/response_test.json';
- $successFixtureFileJson = file_get_contents($successFilePath);
- copy($successFilePath, storage_path('response_test.json'));
- $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
- $errorFixtureFileJson = file_get_contents($errorFilePath);
- copy($errorFilePath, storage_path('response_error_test.json'));
- $route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($parsed['responses'][0]));
- $this->assertEquals(200, $parsed['responses'][0]['status']);
- $this->assertSame(
- $parsed['responses'][0]['content'],
- $successFixtureFileJson
- );
- $this->assertTrue(is_array($parsed['responses'][1]));
- $this->assertEquals(401, $parsed['responses'][1]['status']);
- $this->assertSame(
- $parsed['responses'][1]['content'],
- $errorFixtureFileJson
- );
- unlink(storage_path('response_test.json'));
- unlink(storage_path('response_error_test.json'));
- }
- /** @test */
- public function generates_consistent_examples_when_faker_seed_is_set()
- {
- $route = $this->createRoute('GET', '/withBodyParameters', 'withBodyParameters');
- $paramName = 'room_id';
- $results = [];
- $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- // Examples should have different values
- $this->assertNotEquals(count($results), 1);
- $generator = new Generator(new DocumentationConfig($this->config + ['faker_seed' => 12345]));
- $results = [];
- $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
- // Examples should have same values
- $this->assertEquals(count($results), 1);
- }
- /** @test */
- public function uses_configured_settings_when_calling_route()
- {
- $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
- $rules = [
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- 'header' => 'value',
- ],
- 'response_calls' => [
- 'methods' => ['*'],
- 'queryParams' => [
- 'queryParam' => 'queryValue',
- ],
- 'bodyParams' => [
- 'bodyParam' => 'bodyValue',
- ],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $response = Arr::first($parsed['responses']);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertTrue(is_array($response));
- $this->assertEquals(200, $response['status']);
- $responseContent = json_decode($response['content'], true);
- $this->assertEquals('queryValue', $responseContent['queryParam']);
- $this->assertEquals('bodyValue', $responseContent['bodyParam']);
- $this->assertEquals('value', $responseContent['header']);
- }
- /** @test */
- public function can_use_arrays_in_routes_uses()
- {
- $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame('Example title.', $parsed['metadata']['title']);
- $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['metadata']['description']);
- }
- abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
- abstract public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
- public function dataResources()
- {
- return [
- [
- null,
- '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
- ],
- [
- 'League\Fractal\Serializer\JsonApiSerializer',
- '{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
- ],
- ];
- }
- }
|