Skip to content

Commit

Permalink
Merge branch 'develop' into jbk/upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed Nov 4, 2020
2 parents 837ccd5 + 4eb5935 commit 49ee0b4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ziggy-js",
"version": "1.0.0-beta.3",
"version": "1.0.0-beta.5",
"description": "Use your Laravel named routes in JavaScript.",
"keywords": [
"laravel",
Expand Down
2 changes: 1 addition & 1 deletion src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Ziggy implements JsonSerializable
protected $group;
protected $routes;

public function __construct(string $group = null, string $url = null)
public function __construct($group = null, string $url = null)
{
$this->group = $group;

Expand Down
4 changes: 1 addition & 3 deletions src/ZiggyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public function boot()
protected function registerDirective(BladeCompiler $bladeCompiler)
{
$bladeCompiler->directive('routes', function ($group) {
return app()->isLocal()
? "<?php echo app('" . BladeRouteGenerator::class . "')->generate({$group}); ?>"
: app(BladeRouteGenerator::class)->generate($group);
return "<?php echo app('" . BladeRouteGenerator::class . "')->generate({$group}); ?>";
});
}
}
44 changes: 32 additions & 12 deletions tests/Unit/BladeRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,44 @@ public function can_generate_routes_for_custom_domain()
}

/** @test */
public function can_set_csp_nonce()
public function can_generate_routes_for_given_group_or_groups()
{
$this->assertStringContainsString(
'<script type="text/javascript" nonce="supercalifragilisticexpialidocious">',
(new BladeRouteGenerator)->generate(false, 'supercalifragilisticexpialidocious')
);
}
$router = app('router');
$router->get('posts', $this->noop())->name('posts.index');
$router->get('posts/{post}', $this->noop())->name('posts.show');
$router->get('users/{user}', $this->noop())->name('users.show');
$router->getRoutes()->refreshNameLookups();

/** @test */
public function can_compile_routes_directive()
{
$compiler = app('blade.compiler');
config(['ziggy.groups' => [
'guest' => ['posts.*'],
'admin' => ['users.*'],
]]);

BladeRouteGenerator::$generated = false;
$script = (new BladeRouteGenerator)->generate();
$output = (new BladeRouteGenerator)->generate('guest');
$ziggy = json_decode(Str::after(Str::before($output, ";\n\n"), ' = '), true);

$this->assertCount(2, $ziggy['routes']);
$this->assertArrayHasKey('posts.index', $ziggy['routes']);
$this->assertArrayHasKey('posts.show', $ziggy['routes']);

BladeRouteGenerator::$generated = false;
$this->assertSame($script, $compiler->compileString('@routes'));
$output = (new BladeRouteGenerator)->generate(['guest', 'admin']);
$ziggy = json_decode(Str::after(Str::before($output, ";\n\n"), ' = '), true);

$this->assertCount(3, $ziggy['routes']);
$this->assertArrayHasKey('posts.index', $ziggy['routes']);
$this->assertArrayHasKey('posts.show', $ziggy['routes']);
$this->assertArrayHasKey('users.show', $ziggy['routes']);
}

/** @test */
public function can_set_csp_nonce()
{
$this->assertStringContainsString(
'<script type="text/javascript" nonce="supercalifragilisticexpialidocious">',
(new BladeRouteGenerator)->generate(false, 'supercalifragilisticexpialidocious')
);
}

/** @test */
Expand Down

0 comments on commit 49ee0b4

Please sign in to comment.