-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPest.php
150 lines (129 loc) · 3.89 KB
/
Pest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Contracts\Auth\Authenticatable;
use App\Models\User;
use Illuminate\Support\Facades\Config;
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
uses(TestCase::class, RefreshDatabase::class)->in('Feature');
uses()->group('admin')->in('Feature/Admin');
uses()->group('user')->in('Feature/User');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
/**
* Set the currently logged in user for the application.
*
* @return TestCase
*/
function actingAs(Authenticatable $user, string $driver = null)
{
return test()->actingAs($user, $driver);
}
/**
* Sign in the given user or create new one if not provided.
*
* @param $user \App\User
*
* @return \App\User
*/
function actingAsSanctum($user = null) : User
{
$user = $user ?: User::factory()->create();
Sanctum::actingAs($user, ['*']);
return $user;
}
function seedConfigurations() : void
{
$configs = collect(config('system_configurations'))->map(function($item, $key) {
return [
'name' => $key,
'type' => $item['type'],
'value' => $item['default'] ?? null
];
})->values();
redis()->set('configurations', $configs);
}
function updateConfigurationValue(string $name, $value) : void
{
$configs = redis()->get('configurations');
$updated = $configs->map(function($item) use ($name, $value) {
$item = (array) $item;
if($item['name'] == $name) {
$item['value'] = $value;
}
return $item;
});
redis()->set('configurations', $updated);
}
/**
* provide the inertia headers to pass in requests
*/
function inertiaHeaders() : array
{
return [
'X-Inertia' => true
];
}
/**
* create and return a user by a certain role
* @param string $role
* @return User
*/
function getUserByRole($role = User::ROLE_USER, $attributes = []) : User
{
$user = User::factory()->create(array_merge([
'role' => $role
], $attributes));
return $user;
}
/**
* assert sucess in browser post/put response
*/
function assertBrowserSuccess($response) : void
{
$response->assertRedirect()
->assertSessionHasNoErrors()
->assertValid()
->assertSessionHas('success')
->assertSessionMissing('error');
}
/**
* increase throttle for testing purposes
*/
function increaseThrottles() : void {
Config::set('auth.limiters.registration', '50,1');
Config::set('auth.limiters.login', '50,1');
Config::set('auth.limiters.verification', '50,1');
Config::set('auth.limiters.contact', '50,1');
}