瀏覽代碼

Merge branch 'kiberzauras-patch-1'

Marcel Pociot 8 年之前
父節點
當前提交
0be8f19e53

+ 3 - 0
src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

@@ -76,6 +76,9 @@ abstract class AbstractGenerator
             return [trim($split[0]) => trim($split[1])];
         })->collapse()->toArray();
 
+        //Changes url with parameters like /users/{user} to /users/1
+        $uri = preg_replace('/{(.*)}/', 1, $uri);
+
         return $this->callRoute(array_shift($methods), $uri, [], [], [], $headers);
     }
 

+ 99 - 0
tests/Fixtures/TestResourceController.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Illuminate\Http\Request;
+use Illuminate\Routing\Controller;
+
+class TestResourceController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        return [
+        	'index_resource' => true
+        ];
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        return [
+        	'create_resource' => true
+        ];
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        return [
+        	'store_resource' => true
+        ];
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function show($id)
+    {
+        return [
+        	'show_resource' => $id
+        ];
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function edit($id)
+    {
+        return [
+        	'edit_resource' => $id
+        ];
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, $id)
+    {
+        return [
+        	'update_resource' => $id
+        ];
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy($id)
+    {
+        return [
+        	'destroy_resource' => $id
+        ];
+    }
+}

+ 285 - 0
tests/Fixtures/resource_index.md

@@ -0,0 +1,285 @@
+---
+title: API Reference
+
+language_tabs:
+- bash
+- javascript
+
+includes:
+
+search: true
+
+toc_footers:
+- <a href='http://github.com/mpociot/documentarian'>Documentation Powered by Documentarian</a>
+---
+<!-- START_INFO -->
+# Info
+
+Welcome to the generated API reference.
+[Get Postman Collection](http://localhost/docs/collection.json)
+
+<!-- END_INFO -->
+
+#general
+<!-- START_2ea88ff35aa222f5582e50f39a2b35fd -->
+## Display a listing of the resource.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user",
+    "method": "GET",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+> Example response:
+
+```json
+{
+    "index_resource": true
+}
+```
+
+### HTTP Request
+`GET api/user`
+
+`HEAD api/user`
+
+
+<!-- END_2ea88ff35aa222f5582e50f39a2b35fd -->
+<!-- START_99a7210df460e7fd8ad2508ee28b9763 -->
+## Show the form for creating a new resource.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user/create" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user/create",
+    "method": "GET",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+> Example response:
+
+```json
+{
+    "create_resource": true
+}
+```
+
+### HTTP Request
+`GET api/user/create`
+
+`HEAD api/user/create`
+
+
+<!-- END_99a7210df460e7fd8ad2508ee28b9763 -->
+<!-- START_f0654d3f2fc63c11f5723f233cc53c83 -->
+## Store a newly created resource in storage.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user",
+    "method": "POST",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+
+### HTTP Request
+`POST api/user`
+
+
+<!-- END_f0654d3f2fc63c11f5723f233cc53c83 -->
+<!-- START_7a5835399fad9a53bc0430d6e3054297 -->
+## Display the specified resource.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user/{user}" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user/{user}",
+    "method": "GET",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+> Example response:
+
+```json
+{
+    "show_resource": "1"
+}
+```
+
+### HTTP Request
+`GET api/user/{user}`
+
+`HEAD api/user/{user}`
+
+
+<!-- END_7a5835399fad9a53bc0430d6e3054297 -->
+<!-- START_5ed9d10b12650f9536edfa994fafae15 -->
+## Show the form for editing the specified resource.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user/{user}/edit" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user/{user}/edit",
+    "method": "GET",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+> Example response:
+
+```json
+{
+    "edit_resource": "1"
+}
+```
+
+### HTTP Request
+`GET api/user/{user}/edit`
+
+`HEAD api/user/{user}/edit`
+
+
+<!-- END_5ed9d10b12650f9536edfa994fafae15 -->
+<!-- START_a4a2abed1e8e8cad5e6a3282812fe3f3 -->
+## Update the specified resource in storage.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user/{user}" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user/{user}",
+    "method": "PUT",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+
+### HTTP Request
+`PUT api/user/{user}`
+
+`PATCH api/user/{user}`
+
+
+<!-- END_a4a2abed1e8e8cad5e6a3282812fe3f3 -->
+<!-- START_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->
+## Remove the specified resource from storage.
+
+> Example request:
+
+```bash
+curl "http://localhost/api/user/{user}" \
+-H "Accept: application/json"
+```
+
+```javascript
+var settings = {
+    "async": true,
+    "crossDomain": true,
+    "url": "http://localhost/api/user/{user}",
+    "method": "DELETE",
+    "headers": {
+        "accept": "application/json"
+    }
+}
+
+$.ajax(settings).done(function (response) {
+    console.log(response);
+});
+```
+
+
+### HTTP Request
+`DELETE api/user/{user}`
+
+
+<!-- END_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->

+ 12 - 0
tests/GenerateDocumentationTest.php

@@ -10,6 +10,7 @@ use Mpociot\ApiDoc\Generators\LaravelGenerator;
 use Mpociot\ApiDoc\Tests\Fixtures\DingoTestController;
 use Orchestra\Testbench\TestCase;
 use Mpociot\ApiDoc\Tests\Fixtures\TestController;
+use Mpociot\ApiDoc\Tests\Fixtures\TestResourceController;
 use Illuminate\Support\Facades\Route as RouteFacade;
 
 class GenerateDocumentationTest extends TestCase
@@ -97,6 +98,17 @@ class GenerateDocumentationTest extends TestCase
         $this->assertContains('Processed route: [GET,HEAD] api/test', $output);
     }
 
+    public function testCanParseResourceRoutes()
+    {
+        RouteFacade::resource('/api/user', TestResourceController::class);
+        $output = $this->artisan('api:generate', [
+            '--routePrefix' => 'api/*',
+        ]);
+        $generatedMarkdown = file_get_contents(__DIR__.'/../public/docs/source/index.md');
+        $fixtureMarkdown = file_get_contents(__DIR__.'/Fixtures/resource_index.md');
+        $this->assertSame($generatedMarkdown, $fixtureMarkdown);
+    }
+
     public function testGeneratedMarkdownFileIsCorrect()
     {
         RouteFacade::get('/api/test', TestController::class.'@parseMethodDescription');