opnform/tests/TestHelpers.php

193 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace Tests;
use App\Models\Forms\Form;
use App\Models\User;
2024-02-23 10:54:12 +00:00
use App\Models\Workspace;
2022-09-20 19:59:52 +00:00
use Database\Factories\FormFactory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
trait TestHelpers
{
/**
* Creates a workspace for a user
*/
public function createUserWorkspace(User $user)
{
2024-02-23 10:54:12 +00:00
if (! $user) {
return null;
}
2022-09-20 19:59:52 +00:00
$workspace = Workspace::create([
'name' => 'Form Testing',
'icon' => '🧪',
]);
$user->workspaces()->sync([
$workspace->id => [
2024-02-23 10:54:12 +00:00
'role' => 'admin',
],
2022-09-20 19:59:52 +00:00
], false);
return $workspace;
}
/**
* Generates a Form instance (not saved)
2024-02-23 10:54:12 +00:00
*
2022-09-20 19:59:52 +00:00
* @return array
*/
public function makeForm(User $user, Workspace $workspace, array $data = [])
{
$dbProperties = [
[
'name' => 'Name',
'type' => 'text',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Message',
'type' => 'text',
'hidden' => false,
'required' => false,
2024-02-23 10:54:12 +00:00
'multi_lines' => true,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Number',
'type' => 'number',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Select',
'type' => 'select',
'hidden' => false,
'required' => false,
'allow_creation' => false,
'select' => [
2024-02-23 10:54:12 +00:00
'options' => [['id' => 'First', 'name' => 'First'], ['id' => 'Second', 'name' => 'Second']],
],
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Multi Select',
'type' => 'multi_select',
'hidden' => false,
'required' => false,
'allow_creation' => false,
'multi_select' => [
2024-02-23 10:54:12 +00:00
'options' => [['id' => 'One', 'name' => 'One'], ['id' => 'Two', 'name' => 'Two'], ['id' => 'Three', 'name' => 'Three']],
],
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Date',
'type' => 'date',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Checkbox',
'type' => 'checkbox',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'URL',
'type' => 'url',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Email',
'type' => 'email',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Phone Number',
'type' => 'phone_number',
'hidden' => false,
2024-02-23 10:54:12 +00:00
'required' => false,
2022-09-20 19:59:52 +00:00
],
[
'name' => 'Files',
'type' => 'files',
'hidden' => false,
'required' => false,
2024-02-23 10:54:12 +00:00
],
2022-09-20 19:59:52 +00:00
];
return Form::factory()
->withProperties(FormFactory::formatProperties($dbProperties))
->forWorkspace($workspace)
->createdBy($user)
2022-11-16 14:57:10 +00:00
->make($data);
2022-09-20 19:59:52 +00:00
}
public function createForm(User $user, Workspace $workspace, array $data = [])
{
$form = $this->makeForm($user, $workspace, $data);
$form->save();
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $form;
}
2022-11-16 14:57:10 +00:00
public function createUser(array $data = [])
2022-09-20 19:59:52 +00:00
{
2022-11-16 14:57:10 +00:00
return \App\Models\User::factory()->create($data);
2022-09-20 19:59:52 +00:00
}
public function createProUser()
{
$user = $this->createUser();
$user->subscriptions()->create([
'name' => 'default',
'stripe_id' => Str::random(),
'stripe_status' => 'active',
'stripe_price' => Str::random(),
'quantity' => 1,
]);
return $user;
}
2024-02-23 10:54:12 +00:00
public function actingAsUser(?User $user = null)
2022-09-20 19:59:52 +00:00
{
if ($user == null) {
$user = $this->createUser();
}
2024-01-14 19:52:14 +00:00
$this->postJson('/login', [
2022-09-20 19:59:52 +00:00
'email' => $user->email,
'password' => 'password',
])->assertSuccessful();
return $user;
}
/**
* Creates a user with a Pro subscription
*
* @return User|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
*/
2024-02-23 10:54:12 +00:00
public function actingAsProUser(?User $user = null)
2022-09-20 19:59:52 +00:00
{
if ($user == null) {
$user = $this->createProUser();
}
return $this->actingAsUser($user);
}
public function actingAsGuest()
{
if (Auth::check()) {
Auth::logout();
}
$this->assertGuest();
}
}