소스 검색

Fix tests

shalvah 4 년 전
부모
커밋
2c719ef4fa

+ 4 - 8
phpunit.xml

@@ -13,11 +13,6 @@
         <testsuite name="Full Test">
             <file>tests/GenerateDocumentationTest.php</file>
         </testsuite>
-        <testsuite name="Generator Tests">
-            <file>tests/Unit/DingoGeneratorTest.php</file>
-            <file>tests/Unit/LaravelGeneratorTest.php</file>
-            <file>tests/Unit/GeneratorPluginSystemTest.php</file>
-        </testsuite>
         <testsuite name="Strategies">
             <directory>tests/Strategies</directory>
         </testsuite>
@@ -25,11 +20,12 @@
             <file>tests/Unit/RouteMatcherDingoTest.php</file>
             <file>tests/Unit/RouteMatcherTest.php</file>
         </testsuite>
-        <testsuite name="Other Unit Tests 1">
-            <file>tests/Unit/OpenAPISpecWriterTest.php</file>
+        <testsuite name="Unit Tests 1">
+            <file>tests/Unit/GeneratorTest.php</file>
             <file>tests/Unit/AnnotationParserTest.php</file>
         </testsuite>
-        <testsuite name="Other Unit Tests 2">
+        <testsuite name="Unit Tests 2">
+            <file>tests/Unit/OpenAPISpecWriterTest.php</file>
             <file>tests/Unit/PostmanCollectionWriterTest.php</file>
         </testsuite>
     </testsuites>

+ 2 - 2
tests/Fixtures/TestController.php

@@ -4,7 +4,7 @@ namespace Knuckles\Scribe\Tests\Fixtures;
 
 use Illuminate\Http\Request;
 use Illuminate\Routing\Controller;
-use Knuckles\Scribe\Tests\Unit\GeneratorTestCase;
+use Knuckles\Scribe\Tests\Unit\GeneratorTest;
 use Knuckles\Scribe\Tools\Utils;
 
 /**
@@ -296,7 +296,7 @@ class TestController extends Controller
      */
     public function withResponseTag()
     {
-        GeneratorTestCase::$globalValue = rand();
+        GeneratorTest::$globalValue = rand();
 
         return '';
     }

+ 18 - 20
tests/Fixtures/openapi.yaml

@@ -1,28 +1,12 @@
 openapi: 3.0.3
 info:
-    title: null
+    title: Laravel API
     description: ''
     version: 3.9.9
 servers:
     -
         url: 'http://localhost'
 paths:
-    /api/withDescription:
-        get:
-            summary: 'Example title.'
-            description: "This will be the long description.\nIt can also be multiple lines long."
-            parameters:
-                -
-                    in: header
-                    name: Custom-Header
-                    description: ''
-                    example: NotSoCustom
-                    schema:
-                        type: string
-            responses: {  }
-            tags:
-                - 'Group A'
-            security: []
     /api/withFormDataParams:
         post:
             summary: 'Endpoint with body form data parameters.'
@@ -181,7 +165,7 @@ paths:
             responses: {  }
             tags:
                 - 'Group A'
-    '/api/echoesUrlParameters/{param}-{param2}/{param3}':
+    '/api/echoesUrlParameters/{param}/{param2}/{param3}/{param4}':
         get:
             summary: ''
             description: ''
@@ -213,12 +197,12 @@ paths:
                                 example:
                                     param: '4'
                                     param2: consequatur
-                                    param3: null
+                                    param3: consequatur
                                     param4: null
                                 properties:
                                     param: { type: string, example: '4' }
                                     param2: { type: string, example: consequatur }
-                                    param3: { type: string, example: null }
+                                    param3: { type: string, example: consequatur }
                                     param4: { type: string, example: null }
             tags:
                 - Other😎
@@ -246,6 +230,20 @@ paths:
                     present:
                         summary: 'When the value is present'
                         value: consequatur
+            -
+                in: path
+                name: param3
+                description: 'Optional parameter.'
+                required: true
+                schema:
+                    type: string
+                examples:
+                    omitted:
+                        summary: 'When the value is omitted'
+                        value: ''
+                    present:
+                        summary: 'When the value is present'
+                        value: consequatur
             -
                 in: path
                 name: param4

+ 18 - 20
tests/GenerateDocumentationTest.php

@@ -58,7 +58,7 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function can_process_traditional_laravel_route_syntax()
     {
-        RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
+        RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
 
         config(['scribe.routes.0.match.prefixes' => ['api/*']]);
         $output = $this->artisan('scribe:generate');
@@ -69,7 +69,7 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function can_process_traditional_laravel_head_routes()
     {
-        RouteFacade::addRoute('HEAD', '/api/test', TestController::class . '@withEndpointDescription');
+        RouteFacade::addRoute('HEAD', '/api/test', [TestController::class, 'withEndpointDescription']);
 
         config(['scribe.routes.0.match.prefixes' => ['api/*']]);
         $output = $this->artisan('scribe:generate');
@@ -104,7 +104,7 @@ class GenerateDocumentationTest extends TestCase
             $api->get('/closure', function () {
                 return 'foo';
             });
-            $api->get('/test', TestController::class . '@withEndpointDescription');
+            $api->get('/test', [TestController::class, 'withEndpointDescription']);
         });
 
         config(['scribe.router' => 'dingo']);
@@ -130,9 +130,9 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function can_skip_methods_and_classes_with_hidefromapidocumentation_tag()
     {
-        RouteFacade::get('/api/skip', TestController::class . '@skip');
+        RouteFacade::get('/api/skip', [TestController::class, 'skip']);
         RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
-        RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
+        RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
 
         config(['scribe.routes.0.match.prefixes' => ['api/*']]);
         $output = $this->artisan('scribe:generate');
@@ -145,12 +145,11 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function can_skip_nonexistent_response_files()
     {
-        RouteFacade::get('/api/non-existent', TestController::class . '@withNonExistentResponseFile');
+        RouteFacade::get('/api/non-existent', [TestController::class, 'withNonExistentResponseFile']);
 
         config(['scribe.routes.0.match.prefixes' => ['api/*']]);
         $output = $this->artisan('scribe:generate');
 
-        $this->assertStringContainsString('Skipping route: [GET] api/non-existent', $output);
         $this->assertStringContainsString('@responseFile i-do-not-exist.json does not exist', $output);
     }
 
@@ -226,11 +225,11 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function generated_postman_collection_file_is_correct()
     {
-        RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
-        RouteFacade::post('/api/withFormDataParams', TestController::class . '@withFormDataParams');
-        RouteFacade::post('/api/withBodyParameters', TestController::class . '@withBodyParameters');
-        RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
-        RouteFacade::get('/api/withAuthTag', TestController::class . '@withAuthenticatedTag');
+        // RouteFacade::get('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
+        RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
+        RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
+        RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
+        RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
         RouteFacade::get('/api/echoesUrlParameters/{param}-{param2}/{param3?}', [TestController::class, 'echoesUrlParameters']);
         // We want to have the same values for params each time
         config(['scribe.faker_seed' => 1234]);
@@ -260,12 +259,11 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function generated_openapi_spec_file_is_correct()
     {
-        RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
-        RouteFacade::post('/api/withFormDataParams', TestController::class . '@withFormDataParams');
-        RouteFacade::get('/api/withResponseTag', TestController::class . '@withResponseTag');
-        RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
-        RouteFacade::get('/api/withAuthTag', TestController::class . '@withAuthenticatedTag');
-        RouteFacade::get('/api/echoesUrlParameters/{param}-{param2}/{param3?}', [TestController::class, 'echoesUrlParameters']);
+        RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
+        RouteFacade::get('/api/withResponseTag', [TestController::class, 'withResponseTag']);
+        RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
+        RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
+        RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);
 
         // We want to have the same values for params each time
         config(['scribe.faker_seed' => 1234]);
@@ -291,7 +289,7 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function can_append_custom_http_headers()
     {
-        RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders');
+        RouteFacade::get('/api/headers', [TestController::class, 'checkCustomHeaders']);
 
         config(['scribe.routes.0.match.prefixes' => ['api/*']]);
         config([
@@ -309,7 +307,7 @@ class GenerateDocumentationTest extends TestCase
     /** @test */
     public function can_parse_utf8_response()
     {
-        RouteFacade::get('/api/utf8', TestController::class . '@withUtf8ResponseTag');
+        RouteFacade::get('/api/utf8', [TestController::class, 'withUtf8ResponseTag']);
 
         config(['scribe.routes.0.prefixes' => ['api/*']]);
         $this->artisan('scribe:generate');

+ 0 - 78
tests/Unit/DingoGeneratorTest.php

@@ -1,78 +0,0 @@
-<?php
-
-namespace Knuckles\Scribe\Tests\Unit;
-
-use Dingo\Api\Routing\RouteCollection;
-use Dingo\Api\Routing\Router;
-use Illuminate\Routing\Route;
-use Knuckles\Scribe\Extracting\Generator;
-use Knuckles\Scribe\ScribeServiceProvider;
-use Knuckles\Scribe\Tests\Fixtures\TestController;
-use Knuckles\Scribe\Tools\DocumentationConfig;
-use Orchestra\Testbench\TestCase;
-
-/**
- * @group dingo
- */
-class DingoGeneratorTest extends TestCase
-{
-    protected function getPackageProviders($app)
-    {
-        return [
-            ScribeServiceProvider::class,
-            \Dingo\Api\Provider\LaravelServiceProvider::class,
-        ];
-    }
-
-    public function setUp(): void
-    {
-        parent::setUp();
-
-        $config = $this->config;
-        $config['router'] = 'dingo';
-        $this->generator = new Generator(new DocumentationConfig($config));
-    }
-
-    public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
-    {
-        $desiredRoute = null;
-        /** @var Router $api */
-        $api = app(Router::class);
-        $api->version('v1', function (Router $api) use ($class, $controllerMethod, $path, $httpMethod, &$desiredRoute) {
-            $desiredRoute = $api->$httpMethod($path, $class . "@$controllerMethod");
-        });
-        $routes = app(\Dingo\Api\Routing\Router::class)->getRoutes('v1');
-
-        /*
-         * Doing this bc we want an instance of Dingo\Api\Routing\Route, not Illuminate\Routing\Route, which the method above returns
-         */
-        return collect($routes)
-            ->first(function (Route $route) use ($desiredRoute) {
-                return $route->uri() === $desiredRoute->uri();
-            });
-    }
-
-    public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
-    {
-        $route = null;
-        /** @var Router $api */
-        $api = app(Router::class);
-        $api->version('v1', function (Router $api) use ($class, $controllerMethod, $path, $httpMethod, &$route) {
-            $route = $api->$httpMethod($path, [$class, $controllerMethod]);
-        });
-
-        return $route;
-    }
-
-    public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false)
-    {
-        $route = null;
-        /** @var Router $api */
-        $api = app(Router::class);
-        $api->version('v1', function (Router $api) use ($handler, $path, $httpMethod, &$route) {
-            $route = $api->$httpMethod($path, $handler);
-        });
-
-        return $route;
-    }
-}

+ 15 - 15
tests/Unit/GeneratorTestCase.php → tests/Unit/GeneratorTest.php

@@ -3,13 +3,14 @@
 namespace Knuckles\Scribe\Tests\Unit;
 
 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
+use Illuminate\Routing\Route;
 use Knuckles\Scribe\ScribeServiceProvider;
 use Knuckles\Scribe\Extracting\Generator;
 use Knuckles\Scribe\Tests\Fixtures\TestController;
 use Knuckles\Scribe\Tools\DocumentationConfig;
 use PHPUnit\Framework\TestCase;
 
-abstract class GeneratorTestCase extends TestCase
+class GeneratorTest extends TestCase
 {
     use ArraySubsetAsserts;
 
@@ -17,6 +18,7 @@ abstract class GeneratorTestCase extends TestCase
      * @var \Knuckles\Scribe\Extracting\Generator
      */
     protected $generator;
+
     protected $config = [
         'strategies' => [
             'metadata' => [
@@ -52,17 +54,6 @@ abstract class GeneratorTestCase extends TestCase
 
     public static $globalValue = null;
 
-    protected function getPackageProviders($app)
-    {
-        $providers = [
-            ScribeServiceProvider::class,
-        ];
-        if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
-            $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
-        }
-        return $providers;
-    }
-
     /**
      * Setup the test environment.
      */
@@ -269,11 +260,20 @@ abstract class GeneratorTestCase extends TestCase
         $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);
+    public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
+    {
+        return new Route([$httpMethod], $path, ['uses' => $class . "@$controllerMethod"]);
+    }
 
-    abstract public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
+    public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
+    {
+        return new Route([$httpMethod], $path, ['uses' => [$class, $controllerMethod]]);
+    }
 
-    abstract public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false);
+    public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false)
+    {
+        return new Route([$httpMethod], $path, ['uses' => $handler]);
+    }
 
     public function authRules()
     {

+ 0 - 24
tests/Unit/LaravelGeneratorTest.php

@@ -1,24 +0,0 @@
-<?php
-
-namespace Knuckles\Scribe\Tests\Unit;
-
-use Illuminate\Routing\Route;
-use Knuckles\Scribe\Tests\Fixtures\TestController;
-
-class LaravelGeneratorTest extends GeneratorTestCase
-{
-    public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
-    {
-        return new Route([$httpMethod], $path, ['uses' => $class . "@$controllerMethod"]);
-    }
-
-    public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
-    {
-        return new Route([$httpMethod], $path, ['uses' => [$class, $controllerMethod]]);
-    }
-
-    public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false)
-    {
-        return new Route([$httpMethod], $path, ['uses' => $handler]);
-    }
-}

+ 2 - 2
tests/Unit/PostmanCollectionWriterTest.php

@@ -234,7 +234,7 @@ class PostmanCollectionWriterTest extends TestCase
         $collection = $writer->generatePostmanCollection($endpoints);
 
         $this->assertEquals(['type' => 'bearer'], $collection['auth']);
-        $this->assertNull($collection['item'][0]['item'][0]['request']['auth']);
+        $this->assertArrayNotHasKey('auth', $collection['item'][0]['item'][0]['request']);
         $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
 
         $config['auth']['in'] = 'query';
@@ -257,7 +257,7 @@ class PostmanCollectionWriterTest extends TestCase
                 ],
             ]
         ], $collection['auth']);
-        $this->assertNull($collection['item'][0]['item'][0]['request']['auth']);
+        $this->assertArrayNotHasKey('auth', $collection['item'][0]['item'][0]['request']);
         $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
     }