Browse Source

Added Dingo tests

Marcel Pociot 9 years ago
parent
commit
bf2c3a1fac

+ 2 - 1
composer.json

@@ -23,7 +23,8 @@
     },
     "require-dev": {
         "orchestra/testbench": "~3.0",
-        "phpunit/phpunit": "~4.0 || ~5.0"
+        "phpunit/phpunit": "~4.0 || ~5.0",
+        "dingo/api": "1.0.*@dev"
     },
     "autoload": {
         "psr-0": {

+ 7 - 7
src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

@@ -13,11 +13,11 @@ use Illuminate\Foundation\Http\FormRequest;
 abstract class AbstractGenerator
 {
     /**
-     * @param Route $route
+     * @param $route
      *
      * @return mixed
      */
-    abstract protected function getUri(Route $route);
+    abstract protected function getUri($route);
 
     /**
      * @param  \Illuminate\Routing\Route $route
@@ -25,7 +25,7 @@ abstract class AbstractGenerator
      *
      * @return array
      */
-    abstract public function processRoute(Route $route, $bindings = []);
+    abstract public function processRoute($route, $bindings = []);
 
     /**
      * @param array $routeData
@@ -54,11 +54,11 @@ abstract class AbstractGenerator
     }
 
     /**
-     * @param  \Illuminate\Routing\Route  $route
+     * @param  $route
      *
      * @return \Illuminate\Http\Response
      */
-    protected function getRouteResponse(Route $route, $bindings)
+    protected function getRouteResponse($route, $bindings)
     {
         $uri = $this->addRouteModelBindings($route, $bindings);
 
@@ -68,12 +68,12 @@ abstract class AbstractGenerator
     }
 
     /**
-     * @param Route $route
+     * @param $route
      * @param array $bindings
      *
      * @return mixed
      */
-    protected function addRouteModelBindings(Route $route, $bindings)
+    protected function addRouteModelBindings($route, $bindings)
     {
         $uri = $this->getUri($route);
         foreach ($bindings as $model => $id) {

+ 8 - 11
src/Mpociot/ApiDoc/Generators/DingoGenerator.php

@@ -2,7 +2,7 @@
 
 namespace Mpociot\ApiDoc\Generators;
 
-use Illuminate\Support\Facades\App;
+use Exception;
 
 class DingoGenerator extends AbstractGenerator
 {
@@ -14,26 +14,23 @@ class DingoGenerator extends AbstractGenerator
      */
     public function processRoute($route, $bindings = [])
     {
-        $response = $this->getRouteResponse($route, $bindings);
-
+        try {
+            $response = $this->getRouteResponse($route, $bindings);
+        } catch( Exception $e){
+            $response = '';
+        }
         $routeAction = $route->getAction();
         $routeGroup = $this->getRouteGroup($routeAction['uses']);
         $routeDescription = $this->getRouteDescription($routeAction['uses']);
 
-        if ($response->headers->get('Content-Type') === 'application/json') {
-            $content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT);
-        } else {
-            $content = $response->getContent();
-        }
-
         return $this->getParameters([
             'resource' => $routeGroup,
             'title' => $routeDescription['short'],
             'description' => $routeDescription['long'],
             'methods' => $route->getMethods(),
-            'uri' => $route->getUri(),
+            'uri' => $route->uri(),
             'parameters' => [],
-            'response' => $content,
+            'response' => $response,
         ], $routeAction);
     }
 

+ 2 - 2
src/Mpociot/ApiDoc/Generators/LaravelGenerator.php

@@ -13,7 +13,7 @@ class LaravelGenerator extends AbstractGenerator
      *
      * @return mixed
      */
-    protected function getUri(Route $route)
+    protected function getUri($route)
     {
         return $route->getUri();
     }
@@ -24,7 +24,7 @@ class LaravelGenerator extends AbstractGenerator
      *
      * @return array
      */
-    public function processRoute(Route $route, $bindings = [])
+    public function processRoute($route, $bindings = [])
     {
         $response = $this->getRouteResponse($route, $bindings);
 

+ 318 - 0
tests/DingoGeneratorTest.php

@@ -0,0 +1,318 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests;
+
+use Dingo\Api\Provider\LaravelServiceProvider;
+use Illuminate\Routing\Route;
+use Mpociot\ApiDoc\Tests\Fixtures\DingoTestController;
+use Orchestra\Testbench\TestCase;
+use Mpociot\ApiDoc\Generators\DingoGenerator;
+use Mpociot\ApiDoc\Tests\Fixtures\TestRequest;
+use Mpociot\ApiDoc\Tests\Fixtures\TestController;
+use Illuminate\Support\Facades\Route as RouteFacade;
+
+class DingoGeneratorTest extends TestCase
+{
+    /**
+     * @var \Mpociot\ApiDoc\Generators\DingoGenerator
+     */
+    protected $generator;
+
+    protected function getPackageProviders($app)
+    {
+        return [
+            LaravelServiceProvider::class
+        ];
+    }
+
+    /**
+     * Setup the test environment.
+     */
+    public function setUp()
+    {
+        parent::setUp();
+
+        $this->generator = new DingoGenerator();
+    }
+
+    public function testCanParseMethodDescription()
+    {
+        $api = app('Dingo\Api\Routing\Router');
+        $api->version('v1', function ($api) {
+            $api->get('/api/test', TestController::class.'@parseMethodDescription');
+        });
+        $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0];
+
+        $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']);
+    }
+
+    public function testCanParseRouteMethods()
+    {
+        $api = app('Dingo\Api\Routing\Router');
+        $api->version('v1', function ($api) {
+            $api->get('/get', TestController::class.'@dummy');
+            $api->post('/post', TestController::class.'@dummy');
+            $api->put('/put', TestController::class.'@dummy');
+            $api->delete('/delete', TestController::class.'@dummy');
+        });
+        $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0];
+        $parsed = $this->generator->processRoute($route);
+        $this->assertSame(['GET', 'HEAD'], $parsed['methods']);
+
+        $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[1];
+        $parsed = $this->generator->processRoute($route);
+        $this->assertSame(['POST'], $parsed['methods']);
+
+        $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[2];
+        $parsed = $this->generator->processRoute($route);
+        $this->assertSame(['PUT'], $parsed['methods']);
+
+        $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[3];
+        $parsed = $this->generator->processRoute($route);
+        $this->assertSame(['DELETE'], $parsed['methods']);
+    }
+
+    public function testCanParseFormRequestRules()
+    {
+        $api = app('Dingo\Api\Routing\Router');
+        $api->version('v1', function ($api) {
+            $api->post('/post', DingoTestController::class.'@parseFormRequestRules');
+        });
+
+        $route = app('Dingo\Api\Routing\Router')->getRoutes()['v1']->getRoutes()[0];
+        $parsed = $this->generator->processRoute($route);
+        $parameters = $parsed['parameters'];
+
+        $testRequest = new TestRequest();
+        $rules = $testRequest->rules();
+
+        foreach ($rules as $name => $rule) {
+            $attribute = $parameters[$name];
+
+            switch ($name) {
+
+                case 'required':
+                    $this->assertTrue($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'accepted':
+                    $this->assertTrue($attribute['required']);
+                    $this->assertSame('boolean', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'active_url':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('url', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'alpha':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Only alphabetic characters allowed', $attribute['description'][0]);
+                    break;
+                case 'alpha_dash':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Allowed: alpha-numeric characters, as well as dashes and underscores.', $attribute['description'][0]);
+                    break;
+                case 'alpha_num':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Only alpha-numeric characters allowed', $attribute['description'][0]);
+                    break;
+                case 'array':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('array', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'between':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('numeric', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Between: `5` and `200`', $attribute['description'][0]);
+                    break;
+                case 'before':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('date', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must be a date preceding: `Saturday, 23-Apr-16 14:31:00 UTC`', $attribute['description'][0]);
+                    break;
+                case 'boolean':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('boolean', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'date':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('date', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'date_format':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('date', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Date format: `j.n.Y H:iP`', $attribute['description'][0]);
+                    break;
+                case 'different':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must have a different value than parameter: `alpha_num`', $attribute['description'][0]);
+                    break;
+                case 'digits':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('numeric', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must have an exact length of `2`', $attribute['description'][0]);
+                    break;
+                case 'digits_between':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('numeric', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must have a length between `2` and `10`', $attribute['description'][0]);
+                    break;
+                case 'email':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('email', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'exists':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Valid user firstname', $attribute['description'][0]);
+                    break;
+                case 'image':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('image', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must be an image (jpeg, png, bmp, gif, or svg)', $attribute['description'][0]);
+                    break;
+                case 'in':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('`jpeg`, `png`, `bmp`, `gif` or `svg`', $attribute['description'][0]);
+                    break;
+                case 'integer':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('integer', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'ip':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('ip', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'json':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must be a valid JSON string.', $attribute['description'][0]);
+                    break;
+                case 'max':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Maximum: `10`', $attribute['description'][0]);
+                    break;
+                case 'min':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Minimum: `20`', $attribute['description'][0]);
+                    break;
+                case 'mimes':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Allowed mime types: `jpeg`, `bmp` or `png`', $attribute['description'][0]);
+                    break;
+                case 'not_in':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Not in: `foo` or `bar`', $attribute['description'][0]);
+                    break;
+                case 'numeric':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('numeric', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+                case 'regex':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must match this regular expression: `(.*)`', $attribute['description'][0]);
+                    break;
+                case 'required_if':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if `foo` is `bar`', $attribute['description'][0]);
+                    break;
+                case 'required_unless':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required unless `foo` is `bar`', $attribute['description'][0]);
+                    break;
+                case 'required_with':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are present.', $attribute['description'][0]);
+                    break;
+                case 'required_with_all':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are present.', $attribute['description'][0]);
+                    break;
+                case 'required_without':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if the parameters `foo`, `bar` or `baz` are not present.', $attribute['description'][0]);
+                    break;
+                case 'required_without_all':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if the parameters `foo`, `bar` and `baz` are not present.', $attribute['description'][0]);
+                    break;
+                case 'same':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must be the same as `foo`', $attribute['description'][0]);
+                    break;
+                case 'size':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must have the size of `51`', $attribute['description'][0]);
+                    break;
+                case 'timezone':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Must be a valid timezone identifier', $attribute['description'][0]);
+                    break;
+                case 'url':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('url', $attribute['type']);
+                    $this->assertCount(0, $attribute['description']);
+                    break;
+
+            }
+        }
+    }
+}

+ 29 - 0
tests/Fixtures/DingoTestController.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Illuminate\Routing\Controller;
+
+class DingoTestController extends Controller
+{
+
+    public function dummy()
+    {
+        return '';
+    }
+
+    /**
+     * Example title.
+     * This will be the long description.
+     * It can also be multiple lines long.
+     */
+    public function parseMethodDescription()
+    {
+        return '';
+    }
+
+    public function parseFormRequestRules(DingoTestRequest $request)
+    {
+        return '';
+    }
+}

+ 52 - 0
tests/Fixtures/DingoTestRequest.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tests\Fixtures;
+
+use Dingo\Api\Http\FormRequest;
+
+class DingoTestRequest extends FormRequest
+{
+
+    public function rules()
+    {
+        return [
+            'required' => 'required',
+            'accepted' => 'accepted',
+            'after' => 'after:2016-04-23 14:31:00',
+            'active_url' => 'active_url',
+            'alpha' => 'alpha',
+            'alpha_dash' => 'alpha_dash',
+            'alpha_num' => 'alpha_num',
+            'array' => 'array',
+            'before' => 'before:2016-04-23 14:31:00',
+            'between' => 'between:5,200',
+            'boolean' => 'boolean',
+            'date' => 'date',
+            'date_format' => 'date_format:j.n.Y H:iP',
+            'different' => 'different:alpha_num',
+            'digits' => 'digits:2',
+            'digits_between' => 'digits_between:2,10',
+            'exists' => 'exists:users,firstname',
+            'in' => 'in:jpeg,png,bmp,gif,svg',
+            'integer' => 'integer',
+            'ip' => 'ip',
+            'json' => 'json',
+            'min' => 'min:20',
+            'max' => 'max:10',
+            'mimes' => 'mimes:jpeg,bmp,png',
+            'not_in' => 'not_in:foo,bar',
+            'numeric' => 'numeric',
+            'regex' => 'regex:(.*)',
+            'required_if' => 'required_if:foo,bar',
+            'required_unless' => 'required_unless:foo,bar',
+            'required_with' => 'required_with:foo,bar,baz',
+            'required_with_all' => 'required_with_all:foo,bar,baz',
+            'required_without' => 'required_without:foo,bar,baz',
+            'required_without_all' => 'required_without_all:foo,bar,baz',
+            'same' => 'same:foo',
+            'size' => 'size:51',
+            'timezone' => 'timezone',
+            'url' => 'url',
+        ];
+    }
+}