From efc4b968f0feee439a074f044977256646dfe25c Mon Sep 17 00:00:00 2001 From: lmeysel Date: Fri, 28 Jul 2023 15:21:57 +0200 Subject: [PATCH] backward compatibility/test fixes --- src/CommandRouteGenerator.php | 2 +- src/TypeScriptDeclarationGenerator.php | 26 +++++++++++++++--------- src/Ziggy.php | 2 +- tests/Unit/CommandRouteGeneratorTest.php | 14 ++++++++----- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 9c5fd031..e6aa91e4 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -62,7 +62,7 @@ protected function makeDirectory($path) } private function deriveDtsFile($path) { - if(str_ends_with($path, '.d.ts')) { + if(preg_match('/.d.ts$/', $path)) { return $path; } else { $dir = dirname($path) ?? '.'; diff --git a/src/TypeScriptDeclarationGenerator.php b/src/TypeScriptDeclarationGenerator.php index 466e07f8..5df48e01 100644 --- a/src/TypeScriptDeclarationGenerator.php +++ b/src/TypeScriptDeclarationGenerator.php @@ -6,24 +6,30 @@ class TypeScriptDeclarationGenerator { - private Collection $routes; + /** + * @var Collection + */ + private $routes; protected $indent = " "; - public function __construct(Collection $routes) + /** + * @param Collection $routes + */ + public function __construct($routes) { $this->routes = $routes; } private function preamble() { - return collect([ + return join("\n", [ "/*", " * Do not modify this file. It is auto-generated and corresponds to your routes", " * exposed by ziggy route helper. Changes will not be preserved.", " */", "export {}" - ])->join("\n"); + ]); } private function generateArgsType($route) @@ -39,7 +45,7 @@ private function generateArgsType($route) return "{ name: '$param' }"; } }); - return '['.$list->join(', ').']'; + return '['.join(', ', $list->toArray()).']'; } private function generateRouteDefinition() { @@ -47,18 +53,18 @@ private function generateRouteDefinition() { $routeArgs = $this->generateArgsType($route); return "'$name': $routeArgs,"; }); - return collect(["declare module 'ziggy-js' {", + return join("\n", ["declare module 'ziggy-js' {", $this->indent.'interface RouteLookup {', - str_repeat($this->indent, 2) . $overloads->join("\n" . str_repeat($this->indent, 2)), + str_repeat($this->indent, 2) . join("\n". str_repeat($this->indent, 2), $overloads->toArray()), $this->indent.'}', - "}"])->join("\n"); + "}"]); } public function generateDeclarations() { - return collect([ + return join("\n\n", [ $this->preamble(), $this->generateRouteDefinition() - ])->join("\n\n"); + ]); } } diff --git a/src/Ziggy.php b/src/Ziggy.php index 59f588d0..2fe89cb6 100644 --- a/src/Ziggy.php +++ b/src/Ziggy.php @@ -170,7 +170,7 @@ public function toArray(): array 'defaults' => method_exists(app('url'), 'getDefaultParameters') ? app('url')->getDefaultParameters() : [], - 'routes' => $this->applyFilters($this->group)->map(fn ($route) => $route->except('parameterNames'))->toArray(), + 'routes' => $this->applyFilters($this->group)->map(function ($route) { return $route->except('parameterNames'); })->toArray(), ]; } diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index 844ff867..ce8ee307 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -145,7 +145,7 @@ public function can_generate_file_using_config_path() /** @test */ public function can_generate_dts_file() { - Artisan::call('ziggy:generate --declarations'); + Artisan::call('ziggy:generate', ['--declarations' => true]); $this->assertFileExists(base_path('resources/js/ziggy.d.ts')); $this->assertFileExists(base_path('resources/js/ziggy.js')); @@ -154,10 +154,14 @@ public function can_generate_dts_file() /** @test */ public function can_generate_dts_file_without_routes() { - Artisan::call('ziggy:generate --declarations-only'); + Artisan::call('ziggy:generate', ['--declarations-only' => true]); $this->assertFileExists(base_path('resources/js/ziggy.d.ts')); - $this->assertFileDoesNotExist(base_path('resources/js/ziggy.js')); + + if(method_exists($this, 'assertFileDoesNotExist')) + $this->assertFileDoesNotExist(base_path('resources/js/ziggy.js')); + else + $this->assertFileNotExists(base_path('resources/js/ziggy.js')); } /** @test */ @@ -165,7 +169,7 @@ public function can_derive_dts_file_path_from_given_dts() { config(['ziggy.output.path' => 'resources/js/custom.d.ts']); - Artisan::call('ziggy:generate --declarations-only'); + Artisan::call('ziggy:generate', ['--declarations-only' => true]); $this->assertFileExists(base_path('resources/js/custom.d.ts')); } @@ -175,7 +179,7 @@ public function can_derive_dts_file_path_from_relative_file_without_extension() { config(['ziggy.output.path' => './ziggy']); - Artisan::call('ziggy:generate --declarations-only'); + Artisan::call('ziggy:generate', ['--declarations-only' => true]); $this->assertFileExists(base_path('ziggy.d.ts')); }