Skip to content

Commit

Permalink
remove complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
jcergolj committed Oct 13, 2020
1 parent 40e7611 commit 27d2ec4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 19 deletions.
27 changes: 19 additions & 8 deletions app/Http/Livewire/HasLivewireAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public function hydrate()
throw new AuthenticationException();
}

if (! isset($this->permissionName)) {
$splitted = explode('-', self::getName());
$this->permissionName = Str::plural($splitted[1]).'.'.$splitted[0];
}
$this->getPermissionName();

$this->authorize('for-route', [$this->permissionName, $this->getModel()]);
}
Expand All @@ -48,10 +45,24 @@ public function getModel()
return $this->setModel();
}

foreach (get_object_vars($this) as $var) {
if ($var instanceof Model) {
return $var;
}
return collect(get_object_vars($this))
->filter(function ($variable) {
return $variable instanceof Model;
})->first();
}

/**
* Get permission name.
*
* @return void
*/
public function getPermissionName()
{
if (isset($this->permissionName)) {
return;
}

$splitted = explode('-', self::getName());
$this->permissionName = Str::plural($splitted[1]).'.'.$splitted[0];
}
}
22 changes: 11 additions & 11 deletions app/Scopes/VisibleToScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,40 @@ public function apply(Builder $builder, Model $model)
}

/**
* Should we return early form global scope.
* Should we return early form global scope base on permission and owner field.
*
* @param \App\Models\User $user
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
private function returnEarly($user)
public function returnEarlyPermission($user, $model)
{
if ($user === null) {
$permission = $user->getPermission($model->getTable().'.index');

if (! $permission->pivot->owner_restricted === true) {
return true;
}

if ($user->isAdmin()) {
if (! Schema::hasColumn($model->getTable(), AppServiceProvider::OWNER_FIELD)) {
return true;
}

return false;
}

/**
* Should we return early form global scope base on permission and owner field.
* Should we return early form global scope.
*
* @param \App\Models\User $user
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
public function returnEarlyPermission($user, $model)
private function returnEarly($user)
{
$permission = $user->getPermission($model->getTable().'.index');

if (! $permission->pivot->owner_restricted === true) {
if ($user === null) {
return true;
}

if (! Schema::hasColumn($model->getTable(), AppServiceProvider::OWNER_FIELD)) {
if ($user->isAdmin()) {
return true;
}

Expand Down
79 changes: 79 additions & 0 deletions stubs/component.create.test.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Tests\Feature\Http\Livewire;

use App\Http\Livewire\Create{{ DummyText }}Component;
use App\Http\Livewire\HasLivewireAuth;
use App\Models\{{ DummyText }};
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Response;
use Livewire\Livewire;
use Tests\TestCase;

/** @see \App\Http\Livewire\Create{{ DummyText }}Component */
class Create{{ DummyText }}ComponentTest extends TestCase
{
use RefreshDatabase;

/** @var \App\Models\User */
protected $admin;

public function setUp() : void
{
parent::setUp();

$this->admin = create_admin();
}

/** @test */
public function assert_create_{{ dummyText }}_component_uses_livewire_auth_trait()
{
$this->assertContains(HasLivewireAuth::class, class_uses(Create{{ DummyText }}Component::class));
}

/** @test */
public function render()
{
Livewire::actingAs($this->admin)
->test(Create{{ DummyText }}Component::class)
->assertSee('Save')
->assertStatus(Response::HTTP_OK);
}

/** @test */
public function store()
{
Livewire::actingAs($this->admin)
->test(Create{{ DummyText }}Component::class)
//
->call('store')
->assertRedirect('{{ dummyTextPlu }}');

$this->assertTrue(session()->has('flash'));

${{ dummyTextPlu }} = {{ DummyText }}::where('name', 'manager')
->where('label', 'Manager')
->get();

$this->assertCount(1, ${{ dummyTextPlu }});
}

/**
* @test
* @dataProvider clientFormValidationProvider
*/
public function test_store_validation_rules($clientFormInput, $clientFormValue, $rule)
{
Livewire::actingAs($this->admin)
->test(Create{{ DummyText }}Component::class)
->set($clientFormInput, $clientFormValue)
->call('store')
->assertHasErrors([$clientFormInput => $rule]);
}

public function clientFormValidationProvider()
{
return [
];
}
}

0 comments on commit 27d2ec4

Please sign in to comment.