123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- namespace Mpociot\ApiDoc\Tests\Unit;
- use Orchestra\Testbench\TestCase;
- use Mpociot\ApiDoc\Tools\Generator;
- use Illuminate\Support\Facades\Config;
- use Illuminate\Support\Facades\Storage;
- use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
- use Illuminate\Contracts\Filesystem\FileNotFoundException;
- abstract class GeneratorTestCase extends TestCase
- {
- /**
- * @var \Mpociot\ApiDoc\Tools\Generator
- */
- protected $generator;
- protected function getPackageProviders($app)
- {
- return [
- ApiDocGeneratorServiceProvider::class,
- ];
- }
- /**
- * Setup the test environment.
- */
- public function setUp()
- {
- parent::setUp();
- $this->generator = new Generator();
- }
- /** @test */
- public function can_parse_endpoint_description()
- {
- $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
- $parsed = $this->generator->processRoute($route);
- $this->assertSame('Example title.', $parsed['title']);
- $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['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' => '',
- ],
- '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 can_parse_route_group()
- {
- $route = $this->createRoute('GET', '/api/test', 'dummy');
- $routeGroup = $this->generator->processRoute($route)['group'];
- $this->assertSame('Group A', $routeGroup);
- }
- /** @test */
- public function method_can_override_controller_group()
- {
- $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
- $routeGroup = $this->generator->processRoute($route)['group'];
- $this->assertSame('Group B', $routeGroup);
- }
- /** @test */
- public function can_parse_auth_tags()
- {
- $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
- $authenticated = $this->generator->processRoute($route)['authenticated'];
- $this->assertTrue($authenticated);
- $route = $this->createRoute('GET', '/api/test', 'dummy');
- $authenticated = $this->generator->processRoute($route)['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_response_tag()
- {
- $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($parsed['response'], true));
- }
- /** @test */
- public function can_parse_transformer_tag()
- {
- $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertSame(
- $parsed['response'],
- '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
- );
- }
- /** @test */
- public function can_parse_transformer_tag_with_model()
- {
- $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertSame(
- $parsed['response'],
- '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
- );
- }
- /** @test */
- public function can_parse_transformer_collection_tag()
- {
- $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertSame(
- $parsed['response'],
- '{"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);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertSame(
- $parsed['response'],
- '{"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 = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertArraySubset([
- 'id' => 4,
- 'name' => 'banana',
- 'color' => 'red',
- 'weight' => '1 kg',
- 'delicious' => true,
- ], json_decode($parsed['response'], true));
- }
- /** @test */
- public function can_parse_response_file_tag()
- {
- // copy file to storage
- $fixtureFileJson = file_get_contents(__DIR__.'/../Fixtures/response_test.json');
- Storage::put('response_test.json', $fixtureFileJson);
- $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
- $parsed = $this->generator->processRoute($route);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $this->assertSame(
- $parsed['response'],
- $fixtureFileJson
- );
- Storage::delete('response_test.json');
- }
- /** @test */
- public function uses_configured_settings_when_calling_route()
- {
- $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
- $rules = [
- 'response_calls' => [
- 'methods' => ['*'],
- 'headers' => [
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- 'header' => 'value',
- ],
- 'bindings' => [
- '{id}' => 3,
- ],
- 'env' => [
- 'APP_ENV' => 'documentation',
- ],
- 'query' => [
- 'queryParam' => 'queryValue',
- ],
- 'body' => [
- 'bodyParam' => 'bodyValue',
- ],
- ],
- ];
- $parsed = $this->generator->processRoute($route, $rules);
- $this->assertTrue(is_array($parsed));
- $this->assertArrayHasKey('showresponse', $parsed);
- $this->assertTrue($parsed['showresponse']);
- $response = json_decode($parsed['response'], true);
- $this->assertEquals(3, $response['{id}']);
- $this->assertEquals('documentation', $response['APP_ENV']);
- $this->assertEquals('queryValue', $response['queryParam']);
- $this->assertEquals('bodyValue', $response['bodyParam']);
- $this->assertEquals('value', $response['header']);
- }
- abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
- }
|