Skip to content

Commit

Permalink
Merge pull request #111 from musimana/feature/CreateIsAdminResourceTrait
Browse files Browse the repository at this point in the history
feat: Create IsAdminResource Trait
  • Loading branch information
musimana authored Jul 6, 2024
2 parents 937b303 + 89a49a4 commit 7d10604
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ final class CreateEditSummaryResource implements PageItemInterface
/**
* Get the resource as an array.
*
* @return array{content: string, title: string, url: string}
* @return array{content: string, title: string, url: string|false}
*/
public function getItem(Page $page): array
{
return [
'content' => $page->getMetaDescription(),
'title' => $page->getTitle(),
'url' => $page->getUrlEdit(),
'url' => $page->getUrlAdminEdit(),
];
}
}
8 changes: 2 additions & 6 deletions app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Traits\Admin\IsAdminResource;
use App\Traits\HasPageView;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -19,6 +20,7 @@ final class Page extends Model
{
use HasFactory,
HasPageView,
IsAdminResource,
SoftDeletes;

/**
Expand Down Expand Up @@ -68,12 +70,6 @@ public function getUrl(): string
: route('page.show', $this->slug);
}

/** Get the admin URL for the page edit view. */
public function getUrlEdit(): string
{
return route('admin.page.edit', $this->slug);
}

/** Returns all Page models that should be in the sitemap (in_sitemap = 1). */
public function scopeInSitemap(Builder|QueryBuilder $query): Builder|QueryBuilder
{
Expand Down
18 changes: 18 additions & 0 deletions app/Services/ReflectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Services;

use ReflectionClass;

final class ReflectionService
{
/**
* Get the class name for the given class.
*
* @param class-string $class
*/
public static function getClassName(string $class): ?string
{
return (new ReflectionClass($class))->getShortName();
}
}
18 changes: 18 additions & 0 deletions app/Traits/Admin/HasAdminEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Traits\Admin;

use App\Services\ReflectionService;

trait HasAdminEdit
{
/** Get the admin URL for the resource's edit view. */
public function getUrlAdminEdit(): string|false
{
$class_name = ReflectionService::getClassName(self::class);

return $class_name
? route('admin.' . strtolower($class_name) . '.edit', $this->id)
: false;
}
}
8 changes: 8 additions & 0 deletions app/Traits/Admin/IsAdminResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Traits\Admin;

trait IsAdminResource
{
use HasAdminEdit;
}
4 changes: 2 additions & 2 deletions routes/web/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Route::prefix('admin')->middleware(['auth', 'verified', 'admin'])->name('admin.')->group(function () {
/** PageController Routes */
Route::controller(PageController::class)->group(function () {
Route::get('pages/{page:slug}', 'edit')->name('page.edit');
Route::patch('pages/{page:slug}', 'update')->name('page.update');
Route::get('pages/{page}', 'edit')->name('page.edit');
Route::patch('pages/{page}', 'update')->name('page.update');
});
});
2 changes: 1 addition & 1 deletion tests/Browser/Pages/Admin/Page/AdminEditPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
/** Get the URL for the page. */
public function url(): string
{
return $this->page->getUrlEdit();
return $this->page->getUrlAdminEdit() ?: '';
}

/** Assert that the browser is on the page. */
Expand Down
2 changes: 1 addition & 1 deletion tests/Browser/TestAuth/WebMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testUserMiddleware(): void
->on(new Dashboard($user))
->screenshotWholePage('dashboard-user-' . str_replace(['@', '.'], '_', $user->email))

->visit($page->getUrlEdit())
->visit($page->getUrlAdminEdit())
->assertSee('404')
->assertSee('NOT FOUND')
->screenshotWholePage('cms-user-denied')
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Http/Controllers/Admin/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
})->with('pages');

test('edit renders the 404 view for unknown pages', function () {
$url = url('admin/pages/foo');
$url = url('admin/pages/101');
$user = User::factory()->isAdmin()->create();
$actual = $this->actingAs($user)->get($url);
$session = session()->all();
Expand Down Expand Up @@ -164,7 +164,7 @@
});

test('update returns a 404 status for unknown pages', function () {
$url = url('admin/pages/foo');
$url = url('admin/pages/101');
$user = User::factory()->isAdmin()->create();

$actual = $this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
->toMatchArray([
'content' => $page->getMetaDescription(),
'title' => $page->getTitle(),
'url' => $page->getUrlEdit(),
'url' => $page->getUrlAdminEdit(),
]);
})->with('pages');
13 changes: 13 additions & 0 deletions tests/Unit/App/Services/ReflectionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use App\Models\User;
use App\Services\ReflectionService;

test('getClassName returns ok', function (string $class, string|false $expected) {
/** @var class-string $class */
$actual = ReflectionService::getClassName($class);

expect($actual)->toEqual($expected);
})->with([
[User::class, 'User'],
]);

0 comments on commit 7d10604

Please sign in to comment.