Skip to content

Commit

Permalink
Sort group filenames numerically (fixes #273)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jul 13, 2021
1 parent c91eed5 commit c77fed2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/GroupedEndpoints/GroupedEndpointsFromApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ protected function writeEndpointsToDisk(array $grouped): void
&& isset(Camel::$groupFileNames[$group['name']])) {
$fileName = Camel::$groupFileNames[$group['name']];
} else {
$fileName = "$fileNameIndex.yaml";
// Format numbers as two digits so they are sorted properly when retrieving later
// (ie "10.yaml" comes after "9.yaml", not after "1.yaml")
$fileName = sprintf("%02d.yaml", $fileNameIndex);
$fileNameIndex++;
}

Expand Down
File renamed without changes.
30 changes: 15 additions & 15 deletions tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function can_append_custom_http_headers()
]);
$this->artisan('scribe:generate');

$endpointDetails = Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/0.yaml')['endpoints'][0];
$endpointDetails = Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/00.yaml')['endpoints'][0];
$this->assertEquals("customAuthToken", $endpointDetails['headers']["Authorization"]);
$this->assertEquals("NotSoCustom", $endpointDetails['headers']["Custom-Header"]);
}
Expand Down Expand Up @@ -292,12 +292,12 @@ public function sorts_group_naturally()
config(['scribe.routes.0.match.prefixes' => ['api/*']]);
$this->artisan('scribe:generate');

$this->assertFileExists(__DIR__ . '/../.scribe/endpoints/0.yaml');
$this->assertFileExists(__DIR__ . '/../.scribe/endpoints/1.yaml');
$this->assertFileExists(__DIR__ . '/../.scribe/endpoints/2.yaml');
$this->assertEquals('1. Group 1', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/0.yaml')['name']);
$this->assertEquals('2. Group 2', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/1.yaml')['name']);
$this->assertEquals('10. Group 10', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/2.yaml')['name']);
$this->assertFileExists(__DIR__ . '/../.scribe/endpoints/00.yaml');
$this->assertFileExists(__DIR__ . '/../.scribe/endpoints/01.yaml');
$this->assertFileExists(__DIR__ . '/../.scribe/endpoints/02.yaml');
$this->assertEquals('1. Group 1', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/00.yaml')['name']);
$this->assertEquals('2. Group 2', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/01.yaml')['name']);
$this->assertEquals('10. Group 10', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/02.yaml')['name']);
}

/** @test */
Expand All @@ -324,7 +324,7 @@ public function will_not_overwrite_manually_modified_content_unless_force_flag_i
$this->artisan('scribe:generate');

$authFilePath = '.scribe/auth.md';
$group1FilePath = '.scribe/endpoints/0.yaml';
$group1FilePath = '.scribe/endpoints/00.yaml';

$group = Yaml::parseFile($group1FilePath);
$this->assertEquals('api/action1', $group['endpoints'][0]['uri']);
Expand Down Expand Up @@ -376,9 +376,9 @@ public function generates_correct_url_params_from_resource_routes_and_field_bind

$this->artisan('scribe:generate');

$groupA = Yaml::parseFile('.scribe/endpoints/0.yaml');
$groupA = Yaml::parseFile('.scribe/endpoints/00.yaml');
$this->assertEquals('providers/{provider_slug}/users/{user_id}/addresses', $groupA['endpoints'][0]['uri']);
$groupB = Yaml::parseFile('.scribe/endpoints/1.yaml');
$groupB = Yaml::parseFile('.scribe/endpoints/01.yaml');
$this->assertEquals('providers/{provider_slug}/users/{user_id}/addresses/{uuid}', $groupB['endpoints'][0]['uri']);
}

Expand Down Expand Up @@ -452,19 +452,19 @@ public function respects_endpoints_and_group_sort_order()
$this->assertEquals("GET api/action2", $expectedEndpoints->getNode(2)->textContent);

// Now swap the endpoints
$group = Yaml::parseFile('.scribe/endpoints/0.yaml');
$group = Yaml::parseFile('.scribe/endpoints/00.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(
file_put_contents('.scribe/endpoints/00.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/00.yaml', '.scribe/endpoints/temp.yaml');
rename('.scribe/endpoints/01.yaml', '.scribe/endpoints/00.yaml');
rename('.scribe/endpoints/temp.yaml', '.scribe/endpoints/1.yaml');

$this->artisan('scribe:generate');
Expand Down Expand Up @@ -500,7 +500,7 @@ public function will_auto_set_content_type_to_multipart_if_file_params_are_prese

$this->artisan('scribe:generate');

$group = Yaml::parseFile('.scribe/endpoints/0.yaml');
$group = Yaml::parseFile('.scribe/endpoints/00.yaml');
$this->assertEquals('no-file', $group['endpoints'][0]['uri']);
$this->assertEquals('application/json', $group['endpoints'][0]['headers']['Content-Type']);
$this->assertEquals('top-level-file', $group['endpoints'][1]['uri']);
Expand Down

0 comments on commit c77fed2

Please sign in to comment.