Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public function response(array $data = [], $status = 200): JsonResponse

public function index(Request $request)
{
$this->authorize('index', $this->searchService->getModel());
$this->authorize('index', $this->searchService->getTableName());

$data = $this->searchService->all($request);
return $this->resource::collection($data);
}

public function show(Request $request, int $id)
{
$this->authorize('show', $this->searchService->getModel());
$this->authorize('show', $this->searchService->getTableName());

$request = $request->merge(['id' => $id]);
$validatedData = $this->validateService->handle($request->all(), ValidateService::SHOW);
Expand All @@ -49,7 +49,7 @@ public function show(Request $request, int $id)

public function store(Request $request): JsonResponse
{
$this->authorize('store', $this->searchService->getModel());
$this->authorize('store', $this->persistenceService->getTableName());

$validatedData = $this->validateService->handle($request->all(), ValidateService::STORE);
$id = $this->persistenceService->store($validatedData);
Expand All @@ -58,7 +58,7 @@ public function store(Request $request): JsonResponse

public function update(Request $request, $id): JsonResponse
{
$this->authorize('update', $this->searchService->getModel());
$this->authorize('update', $this->persistenceService->getTableName());

$request = $request->merge(['id' => $id]);
$validatedData = $this->validateService->handle($request->all(), ValidateService::UPDATE);
Expand All @@ -69,7 +69,7 @@ public function update(Request $request, $id): JsonResponse

public function destroy(Request $request, int $id): JsonResponse
{
$this->authorize('destroy', $this->searchService->getModel());
$this->authorize('destroy', $this->persistenceService->getTableName());

$request = $request->merge(['id' => $id]);
$validatedData = $this->validateService->handle($request->all(), ValidateService::DESTROY);
Expand Down
42 changes: 0 additions & 42 deletions src/Policy/Policy.php

This file was deleted.

8 changes: 6 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Illuminate\Support\Str;
use LaravelDomainOriented\Commands\CreateDomain;
use LaravelDomainOriented\Commands\RemoveDomain;

Expand Down Expand Up @@ -36,8 +37,11 @@ private function registerPolicies()
$domainNames = require (app_path('domains.php'));

foreach ($domainNames as $domainName) {
$policy = 'App\\Domain\\'.$domainName.'\\'.$domainName.'Policy';
Gate::policy('Illuminate\Database\Eloquent\Model', $policy);
$namespace = 'App\\Domain\\'.$domainName;
$policy = $namespace.'\\'.$domainName.'Policy';
$tableName = Str::snake(Str::pluralStudly($domainName));

Gate::policy($tableName, $policy);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Services/PersistenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public function destroy(int $id): bool
$model->delete();
return true;
}

public function getTableName(): string
{
return $this->model->getTable();
}
}
4 changes: 2 additions & 2 deletions src/Services/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function findById(int $id)
return $builder->findOrFail($id);
}

public function getModel(): SearchModel
public function getTableName(): string
{
return $this->model;
return $this->model->getTable();
}

public function beforeSearch(Builder $builder, Guard $auth): Builder
Expand Down
32 changes: 29 additions & 3 deletions src/Stubs/Policy.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@

namespace App\Domain\{{singularName}};

use LaravelDomainOriented\Policy\Policy;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Contracts\Auth\Authenticatable as AuthUser;

class {{singularName}}Policy extends Policy
class {{singularName}}Policy
{
//
use HandlesAuthorization;

public function index(): bool
{
return true;
}

public function show(): bool
{
return true;
}

public function store(): bool
{
return true;
}

public function update(): bool
{
return true;
}

public function destroy(): bool
{
return true;
}
}
33 changes: 33 additions & 0 deletions tests/Feature/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use Illuminate\Foundation\Auth\User;
use Illuminate\Routing\Router;
use LaravelDomainOriented\Tests\Cases\DBTestCase;

Expand All @@ -24,6 +25,7 @@ public function setUp(): void
/** @test **/
public function it_should_call_list_route_and_assert_count_of_items()
{
$this->loginWithFakeUser();
$response = $this->getJson('tests');
$response->assertOk();

Expand All @@ -32,9 +34,17 @@ public function it_should_call_list_route_and_assert_count_of_items()
$this->assertCount(count($this->data), $data['data']);
}

/** @test **/
public function it_should_try_call_list_route_without_login_and_get_403()
{
$response = $this->getJson('tests');
$response->assertForbidden();
}

/** @test **/
public function it_should_call_find_route_and_assert_item()
{
$this->loginWithFakeUser();
$response = $this->getJson('tests/1');
$response->assertOk();

Expand All @@ -46,13 +56,15 @@ public function it_should_call_find_route_and_assert_item()
/** @test **/
public function it_should_call_find_route_with_non_existent_id_and_assert_status_404()
{
$this->loginWithFakeUser();
$response = $this->getJson('tests/15');
$response->assertStatus(404);
}

/** @test **/
public function it_should_create_a_item()
{
$this->loginWithFakeUser();
$response = $this->postJson('tests', [
'name' => 'XXX'
]);
Expand All @@ -66,6 +78,7 @@ public function it_should_create_a_item()
/** @test **/
public function it_should_try_create_a_item_and_assert_status_422()
{
$this->loginWithFakeUser();
$response = $this->postJson('tests', [
'name' => 1
]);
Expand All @@ -75,6 +88,7 @@ public function it_should_try_create_a_item_and_assert_status_422()
/** @test **/
public function it_should_update_a_item()
{
$this->loginWithFakeUser();
$updateName = 'XXX';
$response = $this->putJson('tests/1', [
'name' => $updateName
Expand All @@ -89,6 +103,7 @@ public function it_should_update_a_item()
/** @test **/
public function it_should_try_update_a_item_and_assert_status_422()
{
$this->loginWithFakeUser();
$response = $this->putJson('tests/1', [
'name' => 1
]);
Expand All @@ -98,6 +113,7 @@ public function it_should_try_update_a_item_and_assert_status_422()
/** @test **/
public function it_should_delete_a_item()
{
$this->loginWithFakeUser();
$this->withoutMiddleware();
$response = $this->deleteJson('tests/1');
$response->assertOk();
Expand All @@ -107,4 +123,21 @@ public function it_should_delete_a_item()
$this->assertTrue($data['data']['isDeleted']);
$this->assertSoftDeleted('tests');
}

private function loginWithFakeUser()
{
$user = new MyUserModel([
'id' => 1,
'name' => 'test user',
]);

$this->be($user);
}
}

class MyUserModel extends User {
protected $fillable = [
'id',
'name',
];
}