Skip to content

Commit

Permalink
Avoid wrapping Artisan command --url option with url() helper when po…
Browse files Browse the repository at this point in the history
…ssible (#361)

* Add sometimes-failing test for edge cases

* Only wrap URL passed to artisan command if it is present and not valid

* Remove edge case test and rename pathname one

* Simplify URL wrapping
  • Loading branch information
bakerkretzmar committed Nov 13, 2020
1 parent fa91156 commit 865333a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/CommandRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CommandRouteGenerator extends Command
{
protected $signature = 'ziggy:generate {path=./resources/js/ziggy.js} {--url=/} {--group=}';
protected $signature = 'ziggy:generate {path=./resources/js/ziggy.js} {--url=} {--group=}';

protected $description = 'Generate js file for including in build process';

Expand All @@ -24,20 +24,17 @@ public function __construct(Filesystem $files)
public function handle()
{
$path = $this->argument('path');
$group = $this->option('group');

$generatedRoutes = $this->generate($group);
$generatedRoutes = $this->generate($this->option('group'));

$this->makeDirectory($path);

$this->files->put(base_path($path), $generatedRoutes);

$this->info('File generated!');
}

private function generate($group = false)
{
$payload = (new Ziggy($group, url($this->option('url'))))->toJson();
$payload = (new Ziggy($group, $this->option('url') ? url($this->option('url')) : null))->toJson();

return <<<JAVASCRIPT
const Ziggy = {$payload};
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Tests\TestCase;

class CommandRouteGeneratorTest extends TestCase
Expand Down Expand Up @@ -63,6 +64,19 @@ public function can_generate_file_with_custom_url()
$this->assertFileEquals('./tests/fixtures/custom-url.js', base_path('resources/js/ziggy.js'));
}

/** @test */
public function can_generate_file_with_custom_pathname()
{
$router = app('router');
$router->get('posts/{post}/comments', $this->noop())->name('postComments.index');
$router->getRoutes()->refreshNameLookups();
URL::defaults(['locale' => 'en']);

Artisan::call('ziggy:generate', ['--url' => '/foo/bar']);

$this->assertFileEquals('./tests/fixtures/custom-pathname.js', base_path('resources/js/ziggy.js'));
}

/** @test */
public function can_generate_file_with_config_applied()
{
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/custom-pathname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Ziggy = {"url":"http:\/\/ziggy.dev\/foo\/bar","port":null,"defaults":{"locale":"en"},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"]}}};

if (typeof window !== 'undefined' && typeof window.Ziggy !== 'undefined') {
for (let name in window.Ziggy.routes) {
Ziggy.routes[name] = window.Ziggy.routes[name];
}
}

export { Ziggy };

0 comments on commit 865333a

Please sign in to comment.