Can you use the __construct method and pass your slug?
Jan 22, 2018
4
Level 54
Refactoring code snippet for test suite
I'm trying to figure out how I can better refactor this section in my code because it is located in every test file in my test suite. The only difference is the the slug is different for the permission.
Any ideas?
public function setUp()
{
parent::setUp();
$this->user = factory(User::class)->create();
$this->role = factory(Role::class)->create(['slug' => 'admin']);
$this->permission = factory(Permission::class)->create(['slug' => 'slug-here']);
$this->role->givePermissionTo($this->permission);
$this->user->assignRole($this->role);
}
Level 17
You could add a method to your TestCase.php:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
protected function setupData($slug)
{
$this->user = factory(User::class)->create();
$this->role = factory(Role::class)->create(['slug' => 'admin']);
$this->permission = factory(Permission::class)->create(['slug' => $slug]);
$this->role->givePermissionTo($this->permission);
$this->user->assignRole($this->role);
}
Then in your tests:
public function setUp()
{
parent::setUp();
$this->setupData('choose-your-slug');
}
1 like
Please or to participate in this conversation.