Jelajahi Sumber

Add tests for endpoints/groups sorting

shalvah 3 tahun lalu
induk
melakukan
9ad9e9d69f

+ 2 - 0
src/Commands/GenerateDocumentation.php

@@ -248,6 +248,8 @@ class GenerateDocumentation extends Command
         if ($this->forcing && !$this->shouldExtract) {
             throw new \Exception("Can't use --force and --no-extraction together.");
         }
+
+        Camel::$groupFileNames = [];
     }
 
     protected function writeEndpointsToDisk(array $grouped): void

+ 1 - 1
tests/Fixtures/custom.0.yaml

@@ -2,7 +2,7 @@
     - POST
   uri: userDefined/yeah
   metadata:
-    groupName: 1. Group 1
+    groupName: '1. Group 1'
     title: 'User defined'
     description: 'This endpoint allows you to do something.'
     authenticated: false

+ 57 - 10
tests/GenerateDocumentationTest.php

@@ -3,7 +3,6 @@
 namespace Knuckles\Scribe\Tests;
 
 use Illuminate\Support\Facades\Route as RouteFacade;
-use Knuckles\Scribe\ScribeServiceProvider;
 use Knuckles\Scribe\Tests\Fixtures\TestController;
 use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
 use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
@@ -362,14 +361,13 @@ class GenerateDocumentationTest extends BaseLaravelTest
     public function will_not_extract_if_noExtraction_flag_is_set()
     {
         config(['scribe.routes.0.exclude' => ['*']]);
-
         Utils::copyDirectory(__DIR__.'/Fixtures/.scribe', '.scribe');
+
         $output = $this->artisan('scribe:generate', ['--no-extraction' => true]);
-        $this->assertStringNotContainsString("Processing route", $output);
 
-        $html = file_get_contents('public/docs/index.html');
+        $this->assertStringNotContainsString("Processing route", $output);
 
-        $crawler = new Crawler($html);
+        $crawler = new Crawler(file_get_contents('public/docs/index.html'));
         [$intro, $auth] = $crawler->filter('h1 + p')->getIterator();
         $this->assertEquals('Heyaa introduction!👋', trim($intro->firstChild->textContent));
         $this->assertEquals('This is just a test.', trim($auth->firstChild->textContent));
@@ -386,14 +384,13 @@ class GenerateDocumentationTest extends BaseLaravelTest
         RouteFacade::get('/api/action1', [TestGroupController::class, 'action1']);
         RouteFacade::get('/api/action2', [TestGroupController::class, 'action2']);
         config(['scribe.routes.0.prefixes' => ['api/*']]);
-
-        if (!is_dir('.scribe/endpoints')) mkdir('.scribe/endpoints', 0777, true);
+        if (!is_dir('.scribe/endpoints'))
+            mkdir('.scribe/endpoints', 0777, true);
         copy(__DIR__ . '/Fixtures/custom.0.yaml', '.scribe/endpoints/custom.0.yaml');
 
         $this->artisan('scribe:generate');
-        $html = file_get_contents('public/docs/index.html');
 
-        $crawler = new Crawler($html);
+        $crawler = new Crawler(file_get_contents('public/docs/index.html'));
         $headings = $crawler->filter('h1')->getIterator();
         // There should only be four headings — intro, auth and two groups
         $this->assertCount(4, $headings);
@@ -401,9 +398,59 @@ class GenerateDocumentationTest extends BaseLaravelTest
         $this->assertEquals('1. Group 1', trim($group1->textContent));
         $this->assertEquals('2. Group 2', trim($group2->textContent));
         $expectedEndpoints = $crawler->filter('h2');
-        $this->assertCount(3, $expectedEndpoints->getIterator());
+        $this->assertEquals(3, $expectedEndpoints->count());
+        // Enforce the order of the endpoints
+        // Ideally, we should also check the groups they're under
         $this->assertEquals("Some endpoint.", $expectedEndpoints->getNode(0)->textContent);
         $this->assertEquals("User defined", $expectedEndpoints->getNode(1)->textContent);
         $this->assertEquals("GET api/action2", $expectedEndpoints->getNode(2)->textContent);
     }
+
+    /** @test */
+    public function respects_endpoints_and_group_sort_order()
+    {
+        RouteFacade::get('/api/action1', [TestGroupController::class, 'action1']);
+        RouteFacade::get('/api/action1b', [TestGroupController::class, 'action1b']);
+        RouteFacade::get('/api/action2', [TestGroupController::class, 'action2']);
+        config(['scribe.routes.0.prefixes' => ['api/*']]);
+
+        $this->artisan('scribe:generate');
+
+        // First: verify the current order of the groups and endpoints
+        $crawler = new Crawler(file_get_contents('public/docs/index.html'));
+        $h1s = $crawler->filter('h1');
+        $this->assertEquals('1. Group 1', trim($h1s->getNode(2)->textContent));
+        $this->assertEquals('2. Group 2', trim($h1s->getNode(3)->textContent));
+        $expectedEndpoints = $crawler->filter('h2');
+        $this->assertEquals("Some endpoint.", $expectedEndpoints->getNode(0)->textContent);
+        $this->assertEquals("Another endpoint.", $expectedEndpoints->getNode(1)->textContent);
+        $this->assertEquals("GET api/action2", $expectedEndpoints->getNode(2)->textContent);
+
+        // Now swap the endpoints
+        $group = Yaml::parseFile('.scribe/endpoints/0.yaml');
+        $this->assertEquals('api/action1', $group['endpoints'][0]['uri']);
+        $this->assertEquals('api/action1b', $group['endpoints'][1]['uri']);
+        $action1 = $group['endpoints'][0];
+        $group['endpoints'][0] = $group['endpoints'][1];
+        $group['endpoints'][1] = $action1;
+        file_put_contents('.scribe/endpoints/0.yaml', Yaml::dump(
+            $group, 20, 2,
+            Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP
+        ));
+        // And then the groups
+        rename('.scribe/endpoints/0.yaml', '.scribe/endpoints/temp.yaml');
+        rename('.scribe/endpoints/1.yaml', '.scribe/endpoints/0.yaml');
+        rename('.scribe/endpoints/temp.yaml', '.scribe/endpoints/1.yaml');
+
+        $this->artisan('scribe:generate');
+
+        $crawler = new Crawler(file_get_contents('public/docs/index.html'));
+        $h1s = $crawler->filter('h1');
+        $this->assertEquals('2. Group 2', trim($h1s->getNode(2)->textContent));
+        $this->assertEquals('1. Group 1', trim($h1s->getNode(3)->textContent));
+        $expectedEndpoints = $crawler->filter('h2');
+        $this->assertEquals("GET api/action2", $expectedEndpoints->getNode(0)->textContent);
+        $this->assertEquals("Another endpoint.", $expectedEndpoints->getNode(1)->textContent);
+        $this->assertEquals("Some endpoint.", $expectedEndpoints->getNode(2)->textContent);
+    }
 }