diff --git a/tests/Fixtures/TestController.php b/tests/Fixtures/TestController.php index fbd2cf35..67b40529 100644 --- a/tests/Fixtures/TestController.php +++ b/tests/Fixtures/TestController.php @@ -26,7 +26,7 @@ public function withEndpointDescription() * @bodyParam user_id int required The id of the user. * @bodyParam room_id string The id of the room. */ - public function withBodyParameters(TestRequest $request) + public function withBodyParameters() { return ''; } @@ -36,7 +36,7 @@ public function checkCustomHeaders(Request $request) return $request->headers->all(); } - public function fetchRouteResponse() + public function shouldFetchRouteResponse() { $fixture = new \stdClass(); $fixture->id = 1; @@ -54,7 +54,12 @@ public function fetchRouteResponse() ]; } - public function utf8() + /** + * @response { + * "result": "Лорем ипсум долор сит амет" + * } + */ + public function withUtf8ResponseTag() { return ['result' => 'Лорем ипсум долор сит амет']; } @@ -68,8 +73,12 @@ public function skip() /** * @response { - * "data": [] - *} + * "id": 4, + * "name": "banana", + * "color": "red", + * "weight": "1 kg", + * "delicious": true + * } */ public function withResponseTag() { diff --git a/tests/Fixtures/TestResourceController.php b/tests/Fixtures/TestResourceController.php index aae43b86..7ec2bb55 100644 --- a/tests/Fixtures/TestResourceController.php +++ b/tests/Fixtures/TestResourceController.php @@ -10,6 +10,10 @@ class TestResourceController extends Controller /** * Display a listing of the resource. * + * @response { + * "index_resource": true + * } + * * @return \Illuminate\Http\Response */ public function index() @@ -22,6 +26,10 @@ public function index() /** * Show the form for creating a new resource. * + * @response { + * "create_resource": true + * } + * * @return \Illuminate\Http\Response */ public function create() @@ -48,6 +56,10 @@ public function store(Request $request) /** * Display the specified resource. * + * @response { + * "show_resource": true + * } + * * @param int $id * * @return \Illuminate\Http\Response @@ -62,6 +74,10 @@ public function show($id) /** * Show the form for editing the specified resource. * + * @response { + * "edit_resource": true + * } + * * @param int $id * * @return \Illuminate\Http\Response diff --git a/tests/Fixtures/collection.json b/tests/Fixtures/collection.json index 5dfe5567..e34669c4 100644 --- a/tests/Fixtures/collection.json +++ b/tests/Fixtures/collection.json @@ -1 +1 @@ -{"variables":[],"info":{"name":"","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"general","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/localhost\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/localhost\/api\/fetch","request":{"url":"http:\/\/localhost\/api\/fetch","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]} \ No newline at end of file +{"variables":[],"info":{"name":"","_postman_id":"","description":"","schema":"https:\/\/schema.getpostman.com\/json\/collection\/v2.0.0\/collection.json"},"item":[{"name":"general","description":"","item":[{"name":"Example title.","request":{"url":"http:\/\/localhost\/api\/test","method":"GET","body":{"mode":"formdata","formdata":[]},"description":"This will be the long description.\nIt can also be multiple lines long.","response":[]}},{"name":"http:\/\/localhost\/api\/responseTag","request":{"url":"http:\/\/localhost\/api\/responseTag","method":"POST","body":{"mode":"formdata","formdata":[]},"description":"","response":[]}}]}]} diff --git a/tests/Fixtures/index.md b/tests/Fixtures/index.md index 14b0648e..9d00f14f 100644 --- a/tests/Fixtures/index.md +++ b/tests/Fixtures/index.md @@ -41,7 +41,7 @@ var settings = { "url": "http://localhost/api/test", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -62,13 +62,13 @@ null - -## api/fetch + +## api/responseTag > Example request: ```bash -curl -X GET -G "http://localhost/api/fetch" \ +curl -X GET -G "http://localhost/api/responseTag" \ -H "Accept: application/json" ``` @@ -76,10 +76,10 @@ curl -X GET -G "http://localhost/api/fetch" \ var settings = { "async": true, "crossDomain": true, - "url": "http://localhost/api/fetch", + "url": "http://localhost/api/responseTag", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -92,18 +92,18 @@ $.ajax(settings).done(function (response) { ```json { - "id": 1, - "name": "Banana", - "color": "Red", - "weight": "300 grams", + "id": 4, + "name": "banana", + "color": "red", + "weight": "1 kg", "delicious": true } ``` ### HTTP Request -`GET api/fetch` +`GET api/responseTag` - + diff --git a/tests/Fixtures/partial_resource_index.md b/tests/Fixtures/partial_resource_index.md index 71dbe7a4..3f68bc11 100644 --- a/tests/Fixtures/partial_resource_index.md +++ b/tests/Fixtures/partial_resource_index.md @@ -21,7 +21,7 @@ Welcome to the generated API reference. #general - + ## Display a listing of the resource. > Example request: @@ -38,7 +38,7 @@ var settings = { "url": "http://localhost/api/users", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -59,9 +59,9 @@ $.ajax(settings).done(function (response) { `GET api/users` - + - + ## Show the form for creating a new resource. > Example request: @@ -78,7 +78,7 @@ var settings = { "url": "http://localhost/api/users/create", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -99,6 +99,6 @@ $.ajax(settings).done(function (response) { `GET api/users/create` - + diff --git a/tests/Fixtures/resource_index.md b/tests/Fixtures/resource_index.md index 766d712e..5d4962b9 100644 --- a/tests/Fixtures/resource_index.md +++ b/tests/Fixtures/resource_index.md @@ -21,7 +21,7 @@ Welcome to the generated API reference. #general - + ## Display a listing of the resource. > Example request: @@ -38,7 +38,7 @@ var settings = { "url": "http://localhost/api/users", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -59,9 +59,9 @@ $.ajax(settings).done(function (response) { `GET api/users` - + - + ## Show the form for creating a new resource. > Example request: @@ -78,7 +78,7 @@ var settings = { "url": "http://localhost/api/users/create", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -99,9 +99,9 @@ $.ajax(settings).done(function (response) { `GET api/users/create` - + - + ## Store a newly created resource in storage. > Example request: @@ -118,7 +118,7 @@ var settings = { "url": "http://localhost/api/users", "method": "POST", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -132,9 +132,9 @@ $.ajax(settings).done(function (response) { `POST api/users` - + - + ## Display the specified resource. > Example request: @@ -151,7 +151,7 @@ var settings = { "url": "http://localhost/api/users/{user}", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -164,7 +164,7 @@ $.ajax(settings).done(function (response) { ```json { - "show_resource": "1" + "show_resource": true } ``` @@ -172,9 +172,9 @@ $.ajax(settings).done(function (response) { `GET api/users/{user}` - + - + ## Show the form for editing the specified resource. > Example request: @@ -191,7 +191,7 @@ var settings = { "url": "http://localhost/api/users/{user}/edit", "method": "GET", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -204,7 +204,7 @@ $.ajax(settings).done(function (response) { ```json { - "edit_resource": "1" + "edit_resource": true } ``` @@ -212,9 +212,9 @@ $.ajax(settings).done(function (response) { `GET api/users/{user}/edit` - + - + ## Update the specified resource in storage. > Example request: @@ -231,7 +231,7 @@ var settings = { "url": "http://localhost/api/users/{user}", "method": "PUT", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -247,9 +247,9 @@ $.ajax(settings).done(function (response) { `PATCH api/users/{user}` - + - + ## Remove the specified resource from storage. > Example request: @@ -266,7 +266,7 @@ var settings = { "url": "http://localhost/api/users/{user}", "method": "DELETE", "headers": { - "accept": "application/json" + "accept": "application/json", } } @@ -280,6 +280,6 @@ $.ajax(settings).done(function (response) { `DELETE api/users/{user}` - + diff --git a/tests/GenerateDocumentationTest.php b/tests/GenerateDocumentationTest.php index b9bd9f11..dfa14090 100644 --- a/tests/GenerateDocumentationTest.php +++ b/tests/GenerateDocumentationTest.php @@ -27,7 +27,7 @@ public function setUp() public function tearDown() { // delete the generated docs - compatible cross-platform - $dir = __DIR__ . '/../public/docs';/* + $dir = __DIR__ . '/../public/docs'; if (is_dir($dir)) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), @@ -39,7 +39,7 @@ public function tearDown() $todo($fileinfo->getRealPath()); } rmdir($dir); - }*/ + } } /** @@ -61,7 +61,7 @@ public function console_command_does_not_work_with_closure() RouteFacade::get('/api/closure', function () { return 'hi'; }); - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $output = $this->artisan('apidoc:generate'); @@ -78,7 +78,7 @@ public function console_command_does_not_work_with_closure_using_dingo() $api->get('/closure', function () { return 'foo'; }); - $api->get('/test', DingoTestController::class . '@parseMethodDescription'); + $api->get('/test', TestController::class . '@withEndpointDescription'); }); config(['apidoc.router' => 'dingo']); @@ -94,7 +94,7 @@ public function console_command_does_not_work_with_closure_using_dingo() public function can_skip_single_routes() { RouteFacade::get('/api/skip', TestController::class . '@skip'); - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $output = $this->artisan('apidoc:generate'); @@ -157,8 +157,8 @@ public function can_parse_partial_resource_routes() /** @test */ public function generated_markdown_file_is_correct() { - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); - RouteFacade::get('/api/fetch', TestController::class . '@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::get('/api/responseTag', TestController::class . '@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); @@ -173,8 +173,8 @@ public function generated_markdown_file_is_correct() /** @test */ public function can_prepend_and_append_data_to_generated_markdown() { - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); - RouteFacade::get('/api/fetch', TestController::class . '@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::get('/api/responseTag', TestController::class . '@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); @@ -187,15 +187,15 @@ public function can_prepend_and_append_data_to_generated_markdown() $this->artisan('apidoc:generate'); $generatedMarkdown = __DIR__ . '/../public/docs/source/index.md'; - $this->assertContainsRaw($this->getFileContents($prependMarkdown), $this->getFileContents($generatedMarkdown)); - $this->assertContainsRaw($this->getFileContents($appendMarkdown), $this->getFileContents($generatedMarkdown)); + $this->assertContainsIgnoringWhitespace($this->getFileContents($prependMarkdown), $this->getFileContents($generatedMarkdown)); + $this->assertContainsIgnoringWhitespace($this->getFileContents($appendMarkdown), $this->getFileContents($generatedMarkdown)); } /** @test */ public function generated_postman_collection_file_is_correct() { - RouteFacade::get('/api/test', TestController::class . '@parseMethodDescription'); - RouteFacade::post('/api/fetch', TestController::class . '@fetchRouteResponse'); + RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription'); + RouteFacade::post('/api/responseTag', TestController::class . '@withResponseTag'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); $this->artisan('apidoc:generate'); @@ -212,26 +212,22 @@ public function can_append_custom_http_headers() RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders'); config(['apidoc.routes.0.match.prefixes' => ['api/*']]); - config(['apidoc.routes.0.apply.requests.headers' => [ - 'Authorization' => 'customAuthToken', - 'Custom-Header' => 'NotSoCustom', - ] + config([ + 'apidoc.routes.0.apply.headers' => [ + 'Authorization' => 'customAuthToken', + 'Custom-Header' => 'NotSoCustom', + ], ]); $this->artisan('apidoc:generate'); $generatedMarkdown = $this->getFileContents(__DIR__ . '/../public/docs/source/index.md'); - $this->assertContainsRaw('"authorization": [ - "customAuthToken" - ], - "custom-header": [ - "NotSoCustom" - ]', $generatedMarkdown); + $this->assertContainsIgnoringWhitespace('"Authorization": "customAuthToken","Custom-Header":"NotSoCustom"', $generatedMarkdown); } /** @test */ - public function generates_utf8_responses() + public function can_parse_utf8_response() { - RouteFacade::get('/api/utf8', TestController::class . '@utf8'); + RouteFacade::get('/api/utf8', TestController::class . '@withUtf8ResponseTag'); config(['apidoc.routes.0.prefixes' => ['api/*'],]); $this->artisan('apidoc:generate'); @@ -278,7 +274,7 @@ private function getFileContents($path) * @param $needle * @param $haystack */ - private function assertContainsRaw($needle, $haystack) + private function assertContainsIgnoringWhitespace($needle, $haystack) { $haystack = preg_replace('/\s/', '', $haystack); $needle = preg_replace('/\s/', '', $needle); diff --git a/tests/Unit/GeneratorTestCase.php b/tests/Unit/GeneratorTestCase.php index b8e815b8..6f567ce5 100644 --- a/tests/Unit/GeneratorTestCase.php +++ b/tests/Unit/GeneratorTestCase.php @@ -93,7 +93,13 @@ public function test_can_parse_response_tag() $this->assertTrue(is_array($parsed)); $this->assertArrayHasKey('showresponse', $parsed); $this->assertTrue($parsed['showresponse']); - $this->assertJsonStringEqualsJsonString($parsed['response'], '{ "data": []}'); + $this->assertJsonStringEqualsJsonString(json_encode([ + 'id' => 4, + 'name' => 'banana', + 'color' => 'red', + 'weight' => '1 kg', + 'delicious' => true, + ]), $parsed['response']); } /** @test */