-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclass-user-factory.php
More file actions
73 lines (64 loc) · 1.66 KB
/
class-user-factory.php
File metadata and controls
73 lines (64 loc) · 1.66 KB
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
<?php
/**
* User_Factory class file.
*
* @package Mantle
*/
namespace Mantle\Database\Factory;
use Mantle\Database\Model\User;
use function Mantle\Support\Helpers\get_user_object;
use function Mantle\Support\Helpers\stringable;
/**
* User Factory
*
* @template TModel of \Mantle\Database\Model\User
* @template TObject of \WP_User
* @template TReturnValue
*
* @extends Factory<TModel, TObject, TReturnValue>
*/
class User_Factory extends Factory {
use Concerns\With_Meta;
/**
* Model to use when creating objects.
*
* @var class-string<TModel>
*/
protected string $model = User::class;
/**
* Definition of the factory.
*
* @return array<string, mixed>
*/
public function definition(): array {
$first_name = $this->faker->firstName();
$last_name = $this->faker->lastName();
return [
'description' => $this->faker->sentence(),
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'subscriber',
'user_email' => $this->faker->email(),
'user_login' => stringable( "{$first_name} {$last_name}" )->slugify(),
'user_pass' => 'password',
'user_url' => substr( $this->faker->url(), 0, 100 ),
];
}
/**
* Retrieves an object by ID.
*
* @param int $object_id The object ID.
* @return \WP_User|\Mantle\Database\Model\User|null
*/
public function get_object_by_id( int $object_id ) {
return $this->as_models ? $this->model::find( $object_id ) : get_user_object( $object_id );
}
/**
* Create a user with a specific role.
*
* @param string $role The role to assign to the user.
*/
public function with_role( string $role ): static {
return $this->state( [ 'role' => $role ] );
}
}