12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052 |
- <?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 can_parse_body_parameters_as_array()
- {
- $route = $this->createRoute('GET', '/api/test', 'withBodyParametersAsArray');
- $generator = $this->generator->processRoute($route);
- $bodyParameters = $generator['bodyParameters'];
- $cleanBodyParameters = $generator['cleanBodyParameters'];
- $this->assertArraySubset([
- '*.first_name' => [
- 'type' => 'string',
- 'description' => 'The first name of the user.',
- 'required' => false,
- 'value' => 'John',
- ],
- '*.last_name' => [
- 'type' => 'string',
- 'description' => 'The last name of the user.',
- 'required' => false,
- 'value' => 'Doe',
- ],
- '*.contacts.*.first_name' => [
- 'type' => 'string',
- 'description' => 'The first name of the contact.',
- 'required' => false,
- 'value' => 'John',
- ],
- '*.contacts.*.last_name' => [
- 'type' => 'string',
- 'description' => 'The last name of the contact.',
- 'required' => false,
- 'value' => 'Doe',
- ],
- '*.roles.*' => [
- 'type' => 'string',
- 'description' => 'The name of the role.',
- 'required' => false,
- 'value' => 'Admin',
- ],
- ], $bodyParameters);
- $this->assertArraySubset([
- [
- 'first_name' => 'John',
- 'last_name' => 'Doe',
- 'contacts' => [
- [
- 'first_name' => 'John',
- 'last_name' => 'Doe',
- ]
- ],
- 'roles' => [
- 'Admin'
- ]
- ]
- ], $cleanBodyParameters);
- }
- /** @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']);
- }
- /** @test */
- public function can_use_closure_in_routes_uses()
- {
- /**
- * A short title.
- * A longer description.
- * Can be multiple lines.
- *
- * @queryParam location_id required The id of the location.
- * @bodyParam name required Name of the location
- */
- $handler = function () {
- return 'hi';
- };
- $route = $this->createRouteUsesCallable('GET', '/api/closure/test', $handler);
- $parsed = $this->generator->processRoute($route);
- $this->assertSame('A short title.', $parsed['metadata']['title']);
- $this->assertSame("A longer description.\nCan be multiple lines.", $parsed['metadata']['description']);
- $this->assertCount(1, $parsed['queryParameters']);
- $this->assertCount(1, $parsed['bodyParameters']);
- $this->assertSame('The id of the location.', $parsed['queryParameters']['location_id']['description']);
- $this->assertSame('Name of the location', $parsed['bodyParameters']['name']['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);
- abstract public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false);
- 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"}}}',
- ],
- ];
- }
- }
|