Skip to content

Commit 344cc86

Browse files
authored
Add user seeder (#468)
* Fix for filters not working with pagination Targets bug #437 Easy testing with the concurrent userSeeder * Add users seeder (helps testing filters and pagination)
1 parent cddf698 commit 344cc86

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

docs/intro/installation.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,11 @@ php spark serve
7575
That's all that's needed to get started. You can now visit [http://localhost:8080/admin](http://localhost:8080/admin) and login with your new user.
7676

7777
If you'd rather use a different server, like Apache or Nginx, you can follow the [CodeIgniter 4 guide](https://codeigniter.com/user_guide/installation/running.html) suggestions for a number of different server setups.
78+
79+
## Generate some data for testing
80+
81+
You can insert some records of users into your database by running this migration (each run will add 100 users randomly distributed through Users, Developers and Beta groups, randomly activated):
82+
83+
```bash
84+
php spark db:seed Bonfire\\Users\\Database\\Seeds\\Seed100Users
85+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Bonfire\Users\Database\Seeds;
4+
5+
use CodeIgniter\Database\Seeder;
6+
use Faker\Factory;
7+
8+
class Seed100Users extends Seeder
9+
{
10+
public function run()
11+
{
12+
$faker = Factory::create();
13+
$groups = ['user', 'beta', 'developer'];
14+
15+
for ($i = 0; $i < 100; $i++) {
16+
$timestamp = $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d H:i:s');
17+
$firstName = $faker->firstName;
18+
$lastName = $faker->lastName;
19+
$username = substr($this->toAscii($firstName), 0, 5) . substr($this->toAscii($lastName), 0, 5);
20+
$active = $faker->boolean;
21+
$email = strtolower($this->toAscii($firstName) . '.' . $this->toAscii($lastName) . '@example.com');
22+
$secret2 = $faker->sha256;
23+
24+
// Insert into users table
25+
$this->db->table('users')->insert([
26+
'first_name' => $firstName,
27+
'last_name' => $lastName,
28+
'username' => $username,
29+
'active' => $active,
30+
'created_at' => $timestamp,
31+
'updated_at' => $timestamp,
32+
'last_active' => $timestamp,
33+
]);
34+
35+
// Get the last inserted ID
36+
$userId = $this->db->insertID();
37+
38+
// Insert into auth_identities table
39+
$this->db->table('auth_identities')->insert([
40+
'user_id' => $userId,
41+
'type' => 'email_password',
42+
'secret' => $email,
43+
'secret2' => $secret2,
44+
]);
45+
46+
// Insert into auth_groups_users table
47+
$this->db->table('auth_groups_users')->insert([
48+
'user_id' => $userId,
49+
'group' => $faker->randomElement($groups),
50+
'created_at' => $timestamp,
51+
]);
52+
}
53+
}
54+
55+
private function toAscii($str)
56+
{
57+
return iconv('UTF-8', 'ASCII//TRANSLIT', $str);
58+
}
59+
}

0 commit comments

Comments
 (0)