Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed May 17, 2024
1 parent 54c00b2 commit 0f708d1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 59 deletions.
32 changes: 14 additions & 18 deletions tests/Unit/BladeRouteGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Tighten\Ziggy\BladeRouteGenerator;
use Tighten\Ziggy\Ziggy;
Expand All @@ -9,12 +10,11 @@
});

test('generate named routes', function () {
app('router')->get('/', fn () => ''); // Not named, should not be included in JSON output
app('router')->get('posts', fn () => '')->name('posts.index');
app('router')->post('posts', fn () => '')->name('posts.store');
app('router')->get('posts/{post}', fn () => '')->name('posts.show');
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->getRoutes()->refreshNameLookups();
Route::get('/', fn () => ''); // Not named, should not be included in JSON output
Route::get('posts', fn () => '')->name('posts.index');
Route::post('posts', fn () => '')->name('posts.store');
Route::get('posts/{post}', fn () => '')->name('posts.show');
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');

$output = (new BladeRouteGenerator)->generate();
$config = json_decode(Str::between($output, 'const Ziggy=', ';!'), true);
Expand All @@ -28,8 +28,7 @@
});

test('generate mergeable route list on repeated compiles', function () {
app('router')->get('posts', fn () => '')->name('posts.index');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts', fn () => '')->name('posts.index');

(new BladeRouteGenerator)->generate();
$output = (new BladeRouteGenerator)->generate();
Expand All @@ -44,8 +43,7 @@
});

test('generate basic route config', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');

expect((new BladeRouteGenerator)->generate())->toContain(json_encode([
'postComments.index' => [
Expand All @@ -57,8 +55,7 @@
});

test('generate route config for custom domain', function () {
app('router')->domain('{account}.myapp.com')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->getRoutes()->refreshNameLookups();
Route::domain('{account}.myapp.com')->get('posts/{post}/comments', fn () => '')->name('postComments.index');

expect((new BladeRouteGenerator)->generate())->toContain(json_encode([
'postComments.index' => [
Expand All @@ -71,10 +68,9 @@
});

test('generate route config for groups', function (array $groups, array $names) {
app('router')->get('posts', fn () => '')->name('posts.index');
app('router')->get('posts/{post}', fn () => '')->name('posts.show');
app('router')->get('users/{user}', fn () => '')->name('users.show');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts', fn () => '')->name('posts.index');
Route::get('posts/{post}', fn () => '')->name('posts.show');
Route::get('users/{user}', fn () => '')->name('users.show');

config(['ziggy.groups' => [
'guest' => ['posts.*'],
Expand All @@ -98,7 +94,7 @@
});

test('render script tag', function () {
app('router')->get('posts', fn () => '')->name('posts.index');
Route::get('posts', fn () => '')->name('posts.index');

$config = (new Ziggy)->toJson();
$routeFunction = file_get_contents(__DIR__ . '/../../dist/route.umd.js');
Expand All @@ -111,7 +107,7 @@
});

test('render merge script tag', function () {
app('router')->get('posts', fn () => '')->name('posts.index');
Route::get('posts', fn () => '')->name('posts.index');

$config = json_encode((new Ziggy)->toArray()['routes']);

Expand Down
38 changes: 16 additions & 22 deletions tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Tighten\Ziggy\Output\File;

Expand Down Expand Up @@ -28,18 +29,16 @@
});

test('generate routes file', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');

artisan('ziggy:generate');

expect(base_path('resources/js/ziggy.js'))->toBeFile('./tests/fixtures/ziggy.js');
});

test('generate file with custom url', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
URL::defaults(['locale' => 'en']);

artisan('ziggy:generate --url http://example.org');
Expand All @@ -48,19 +47,17 @@
});

test('generate file with custom pathname', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');

artisan('ziggy:generate --url /foo/bar');

expect(base_path('resources/js/ziggy.js'))->toBeFile('./tests/fixtures/custom-pathname.js');
});

test('generate file respecting config', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
app('router')->get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config

config(['ziggy.except' => ['admin.*']]);

Expand All @@ -70,9 +67,8 @@
});

test('generate file with custom output formatter', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config

config([
'ziggy' => [
Expand All @@ -89,9 +85,8 @@
});

test('generate file for groups', function () {
app('router')->get('posts/{post}/comments', fn () => '')->name('postComments.index');
app('router')->get('admin', fn () => '')->name('admin.dashboard');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('admin', fn () => '')->name('admin.dashboard');

config([
'ziggy.except' => ['admin.*'],
Expand All @@ -114,11 +109,10 @@
});

test('generate dts file', function () {
app('router')->get('posts', fn () => '')->name('posts.index');
app('router')->post('posts/{post}/comments', fn ($post, $comment) => '')->name('comments.store');
app('router')->get('posts/{post}/comments/{comment:uuid}', fn ($post, $comment) => '')->name('comments.show');
app('router')->post('posts/{post}/reactions/{reaction?}', fn ($post, $reaction) => '')->name('reactions.store');
app('router')->getRoutes()->refreshNameLookups();
Route::get('posts', fn () => '')->name('posts.index');
Route::post('posts/{post}/comments', fn ($post, $comment) => '')->name('comments.store');
Route::get('posts/{post}/comments/{comment:uuid}', fn ($post, $comment) => '')->name('comments.show');
Route::post('posts/{post}/reactions/{reaction?}', fn ($post, $reaction) => '')->name('reactions.store');

artisan('ziggy:generate --types');

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/FolioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
use Laravel\Folio\Folio;
use Laravel\Folio\FolioServiceProvider;
use Tighten\Ziggy\Ziggy;
Expand Down Expand Up @@ -48,8 +49,7 @@
});

test('normal routes override folio routes', function () {
app('router')->get('about', fn () => '')->name('about');
app('router')->getRoutes()->refreshNameLookups();
Route::get('about', fn () => '')->name('about');

File::ensureDirectoryExists(resource_path('views/pages'));
File::put(resource_path('views/pages/about.blade.php'), '<?php Laravel\Folio\name("about");');
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/RouteCachingTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

use Illuminate\Support\Facades\Route;
use Tighten\Ziggy\Ziggy;

test('exclude routes with generated names', function () {
app('router')->get('users', fn () => '')->name('users');
app('router')->get('cached', fn () => '')->name('generated::ZRopaJJwzA27wRLa');
app('router')->getRoutes()->refreshNameLookups();
Route::get('users', fn () => '')->name('users');
Route::get('cached', fn () => '')->name('generated::ZRopaJJwzA27wRLa');

expect((new Ziggy)->toArray()['routes'])->toBe([
'users' => [
Expand Down
26 changes: 12 additions & 14 deletions tests/Unit/RouteModelBindingTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Route;
use Tighten\Ziggy\Ziggy;

beforeEach(function () {
$router = app('router');
$router->get('users/{user}', fn (User $user) => '')->name('users');
$router->get('admins/{admin}', fn (Admin $admin) => '')->name('admins');
$router->get('tags/{tag}', fn (Tag $tag) => '')->name('tags');
$router->get('tokens/{token}', fn ($token) => '')->name('tokens');
$router->get('users/{user}/{number}', fn (User $user, int $n) => '')->name('users.numbers');
$router->post('users', fn (User $user) => '')->name('users.store');
$router->get('comments/{comment}', fn (Comment $comment) => '')->name('comments');
$router->get('replies/{reply}', fn (Reply $reply) => '')->name('replies');
$router->get('blog/{category}/{post:slug}', fn (PostCategory $category, Post $post) => '')->name('posts');
$router->get('blog/{category}/{post:slug}/{tag:slug}', fn (PostCategory $category, Post $post, Tag $tag) => '')->name('posts.tags');
$router->getRoutes()->refreshNameLookups();
Route::get('users/{user}', fn (User $user) => '')->name('users');
Route::get('admins/{admin}', fn (Admin $admin) => '')->name('admins');
Route::get('tags/{tag}', fn (Tag $tag) => '')->name('tags');
Route::get('tokens/{token}', fn ($token) => '')->name('tokens');
Route::get('users/{user}/{number}', fn (User $user, int $n) => '')->name('users.numbers');
Route::post('users', fn (User $user) => '')->name('users.store');
Route::get('comments/{comment}', fn (Comment $comment) => '')->name('comments');
Route::get('replies/{reply}', fn (Reply $reply) => '')->name('replies');
Route::get('blog/{category}/{post:slug}', fn (PostCategory $category, Post $post) => '')->name('posts');
Route::get('blog/{category}/{post:slug}/{tag:slug}', fn (PostCategory $category, Post $post, Tag $tag) => '')->name('posts.tags');
});

test('register implicit route model bindings', function () {
Expand Down Expand Up @@ -176,8 +175,7 @@
});

test('handle abstract classes in route model bindings', function () {
app('router')->get('models/{model}', fn (Model $model) => '')->name('models');
app('router')->getRoutes()->refreshNameLookups();
Route::get('models/{model}', fn (Model $model) => '')->name('models');

expect((new Ziggy)->toArray()['routes']['models'])->toBe([
'uri' => 'models/{model}',
Expand Down

0 comments on commit 0f708d1

Please sign in to comment.