mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-23 14:34:04 +00:00
94 lines
2.6 KiB
PHP
94 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Segments\ControllersLogic;
|
|
|
|
use App\Classes\ValueObjects\Constants\RoleTypes;
|
|
use App\Models\Segment;
|
|
use App\Models\SegmentConstant;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
use Tests\TestCaseChild;
|
|
|
|
abstract class SegmentsControllerLogicTestCase extends TestCaseChild
|
|
{
|
|
/** @var User */
|
|
protected $admin;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->admin = $this->createAdminUser();
|
|
}
|
|
|
|
protected function createAdminUser(): User
|
|
{
|
|
$user = new User();
|
|
$user->name = 'segments_logic_admin';
|
|
$user->email = 'seg-logic-admin-' . uniqid() . '@example.com';
|
|
$user->password = bcrypt('password');
|
|
$user->type = RoleTypes::ADMIN;
|
|
$user->status = 1;
|
|
$user->save();
|
|
|
|
return $user;
|
|
}
|
|
|
|
protected function grantPermission(User $user, string $permissionName): void
|
|
{
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
|
|
|
$permissionId = DB::table('permissions')
|
|
->where('name', $permissionName)
|
|
->where('guard_name', 'api')
|
|
->value('id');
|
|
|
|
if ($permissionId === null) {
|
|
$permissionId = DB::table('permissions')->insertGetId([
|
|
'name' => $permissionName,
|
|
'guard_name' => 'api',
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
DB::table('model_has_permissions')->updateOrInsert([
|
|
'permission_id' => $permissionId,
|
|
'model_type' => User::class,
|
|
'model_id' => $user->id,
|
|
], []);
|
|
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
|
$this->admin = $user->fresh();
|
|
}
|
|
|
|
protected function actingAsAdmin(): self
|
|
{
|
|
$this->actingAs($this->admin, 'api');
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function createSegment(string $name = 'segments_logic_segment', int $companyModuleId = 1): Segment
|
|
{
|
|
$segment = new Segment();
|
|
$segment->name = substr($name, 0, 45);
|
|
$segment->company_module_id = $companyModuleId;
|
|
$segment->save();
|
|
|
|
return $segment;
|
|
}
|
|
|
|
protected function createConstant(Segment $segment, string $reference, array $value): SegmentConstant
|
|
{
|
|
$constant = new SegmentConstant();
|
|
$constant->segment_id = $segment->id;
|
|
$constant->reference = substr($reference, 0, 45);
|
|
$constant->value = $value;
|
|
$constant->save();
|
|
|
|
return $constant;
|
|
}
|
|
|
|
}
|