Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed Oct 12, 2023
1 parent 21cb057 commit 6adc66f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 95 deletions.
7 changes: 0 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ protected function getPackageProviders($app)
return [ZiggyServiceProvider::class];
}

protected function laravelVersion(int $v = null)
{
$version = (int) head(explode('.', app()->version()));

return isset($v) ? $version >= $v : $version;
}

protected function noop(): Closure
{
return function () {
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ public function can_generate_dts_file()
/** @test */
public function can_generate_dts_file_with_scoped_bindings()
{
if (! $this->laravelVersion(7)) {
$this->markTestSkipped('Requires Laravel >=7');
}

app('router')->get('posts', $this->noop())->name('posts.index');
app('router')->get('posts/{post}/comments/{comment:uuid}', PostCommentController::class)->name('postComments.show');
app('router')->post('posts/{post}/comments', PostCommentController::class)->name('postComments.store');
Expand Down
27 changes: 6 additions & 21 deletions tests/Unit/RouteModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ protected function setUp(): void
$router->get('replies/{reply}', function (Reply $reply) {
return '';
})->name('replies');

if ($this->laravelVersion(7)) {
$router->get('blog/{category}/{post:slug}', function (PostCategory $category, Post $post) {
return '';
})->name('posts');
$router->get('blog/{category}/{post:slug}/{tag:slug}', function (PostCategory $category, Post $post, Tag $tag) {
return '';
})->name('posts.tags');
}
$router->get('blog/{category}/{post:slug}', function (PostCategory $category, Post $post) {
return '';
})->name('posts');
$router->get('blog/{category}/{post:slug}/{tag:slug}', function (PostCategory $category, Post $post, Tag $tag) {
return '';
})->name('posts.tags');

$router->getRoutes()->refreshNameLookups();
}
Expand Down Expand Up @@ -128,10 +125,6 @@ public function can_handle_bound_and_unbound_parameters_in_the_same_route()
/** @test */
public function can_handle_multiple_scoped_bindings()
{
if (! $this->laravelVersion(7)) {
$this->markTestSkipped('Requires Laravel >=7');
}

$this->assertSame([
'posts' => [
'uri' => 'blog/{category}/{post}',
Expand All @@ -158,10 +151,6 @@ public function can_handle_multiple_scoped_bindings()
/** @test */
public function can_merge_implicit_and_scoped_bindings()
{
if (! $this->laravelVersion(7)) {
$this->markTestSkipped('Requires Laravel >=7');
}

$this->assertSame([
'users' => [
'uri' => 'users/{user}',
Expand Down Expand Up @@ -245,10 +234,6 @@ public function can_merge_implicit_and_scoped_bindings()
/** @test */
public function can_include_bindings_in_json()
{
if (! $this->laravelVersion(7)) {
$this->markTestSkipped('Requires Laravel >=7');
}

$json = '{"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"users":{"uri":"users\/{user}","methods":["GET","HEAD"],"parameters":["user"],"bindings":{"user":"uuid"}},"admins":{"uri":"admins\/{admin}","methods":["GET","HEAD"],"parameters":["admin"],"bindings":{"admin":"uuid"}},"tags":{"uri":"tags\/{tag}","methods":["GET","HEAD"],"parameters":["tag"],"bindings":{"tag":"id"}},"tokens":{"uri":"tokens\/{token}","methods":["GET","HEAD"],"parameters":["token"]},"users.numbers":{"uri":"users\/{user}\/{number}","methods":["GET","HEAD"],"parameters":["user","number"],"bindings":{"user":"uuid"}},"users.store":{"uri":"users","methods":["POST"]},"comments":{"uri":"comments\/{comment}","methods":["GET","HEAD"],"parameters":["comment"],"bindings":{"comment":"uuid"}},"replies":{"uri":"replies\/{reply}","methods":["GET","HEAD"],"parameters":["reply"],"bindings":{"reply":"uuid"}},"posts":{"uri":"blog\/{category}\/{post}","methods":["GET","HEAD"],"parameters":["category","post"],"bindings":{"category":"id","post":"slug"}},"posts.tags":{"uri":"blog\/{category}\/{post}\/{tag}","methods":["GET","HEAD"],"parameters":["category","post","tag"],"bindings":{"category":"id","post":"slug","tag":"slug"}}}}';

$this->assertSame($json, (new Ziggy)->toJson());
Expand Down
156 changes: 93 additions & 63 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,11 @@ protected function setUp(): void
$router->get('posts/{post}/comments', $this->noop())->name('postComments.index');
$router->post('posts', $this->noop())->middleware(['auth', 'role:admin'])->name('posts.store');
$router->get('admin/users', $this->noop())->middleware(['role:admin'])->name('admin.users.index');

if ($this->laravelVersion(7)) {
$router->get('/posts/{post}/comments/{comment:uuid}', $this->noop())->name('postComments.show');
}
$router->get('/posts/{post}/comments/{comment:uuid}', $this->noop())->name('postComments.show');

$router->getRoutes()->refreshNameLookups();
}

/**
* If running Laravel 7 or higher, add the 'postComments.show' route.
*/
protected function addPostCommentsRouteWithBindings(array &$routes): void
{
if ($this->laravelVersion(7)) {
$routes['postComments.show'] = [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
];
}
}

/** @test */
public function can_filter_to_only_include_routes_matching_a_pattern()
{
Expand Down Expand Up @@ -85,10 +65,16 @@ public function can_filter_to_exclude_routes_matching_a_pattern()
'methods' => ['GET', 'HEAD'],
'parameters' => ['post'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertSame($expected, $routes->toArray()['routes']);
}

Expand Down Expand Up @@ -133,10 +119,16 @@ public function can_set_excluded_routes_using_except_config()
'methods' => ['GET', 'HEAD'],
'parameters' => ['post'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertSame($expected, $routes);
}

Expand Down Expand Up @@ -176,10 +168,16 @@ public function returns_unfiltered_routes_when_both_only_and_except_configs_set(
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertSame($expected, $routes);
}

Expand Down Expand Up @@ -294,10 +292,16 @@ public function can_filter_routes_from_multiple_groups_using_negative_patterns()
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertSame($expected, $routes);
}

Expand Down Expand Up @@ -334,10 +338,16 @@ public function can_ignore_passed_group_not_set_in_config()
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertSame($expected, $routes);
}

Expand Down Expand Up @@ -376,10 +386,16 @@ public function can_include_middleware()
'methods' => ['GET', 'HEAD'],
'middleware' => ['role:admin'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertEquals($expected, $routes);
}

Expand Down Expand Up @@ -467,10 +483,16 @@ public function can_include_only_middleware_set_in_config()
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$this->assertEquals($expected, $routes);
}

Expand Down Expand Up @@ -510,22 +532,26 @@ public function can_order_fallback_routes_last()
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
];

$this->addPostCommentsRouteWithBindings($expected);

$expected['users.index'] = [
'uri' => 'users',
'methods' => ['GET', 'HEAD'],
];

$expected['fallback'] = [
'uri' => '{fallbackPlaceholder}',
'methods' => ['GET', 'HEAD'],
'wheres' => [
'fallbackPlaceholder' => '.*',
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
'users.index' => [
'uri' => 'users',
'methods' => ['GET', 'HEAD'],
],
'fallback' => [
'uri' => '{fallbackPlaceholder}',
'methods' => ['GET', 'HEAD'],
'wheres' => [
'fallbackPlaceholder' => '.*',
],
'parameters' => ['fallbackPlaceholder'],
],
'parameters' => ['fallbackPlaceholder'],
];

$this->assertSame($expected, $routes);
Expand Down Expand Up @@ -567,11 +593,17 @@ public function route_payload_can_array_itself()
'uri' => 'admin/users',
'methods' => ['GET', 'HEAD'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
],
];

$this->addPostCommentsRouteWithBindings($expected['routes']);

$this->assertSame($expected, $ziggy->toArray());
}

Expand All @@ -590,20 +622,18 @@ public function route_payload_can_json_itself()
'methods' => ['GET', 'HEAD'],
'parameters' => ['post'],
],
'postComments.show' => [
'uri' => 'posts/{post}/comments/{comment}',
'methods' => ['GET', 'HEAD'],
'parameters' => ['post', 'comment'],
'bindings' => [
'comment' => 'uuid',
],
],
],
];

$this->addPostCommentsRouteWithBindings($expected['routes']);

$json = '{"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"],"parameters":["post"]}}}';

if ($this->laravelVersion(7)) {
$json = str_replace(
'}}}',
'},"postComments.show":{"uri":"posts\/{post}\/comments\/{comment}","methods":["GET","HEAD"],"parameters":["post","comment"],"bindings":{"comment":"uuid"}}}}',
$json,
);
}
$json = '{"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"postComments.index":{"uri":"posts\/{post}\/comments","methods":["GET","HEAD"],"parameters":["post"]},"postComments.show":{"uri":"posts\/{post}\/comments\/{comment}","methods":["GET","HEAD"],"parameters":["post","comment"],"bindings":{"comment":"uuid"}}}}';

$this->assertSame($expected, json_decode(json_encode(new Ziggy), true));
$this->assertSame($json, json_encode(new Ziggy));
Expand Down

0 comments on commit 6adc66f

Please sign in to comment.