|
1
|
|
|
<?php namespace App\Console\Commands; |
|
2
|
|
|
|
|
3
|
|
|
use App\Models\Categories; |
|
4
|
|
|
use App\Models\Groups; |
|
5
|
|
|
use App\Models\Products; |
|
6
|
|
|
use App\User; |
|
7
|
|
|
use Artisan; |
|
8
|
|
|
use DB; |
|
9
|
|
|
use Faker\Factory; |
|
10
|
|
|
use Hash; |
|
11
|
|
|
use Illuminate\Console\Command; |
|
12
|
|
|
|
|
13
|
|
|
class DatabaseFaker extends Command { |
|
14
|
|
|
|
|
15
|
|
|
const NB_USERS = 20; |
|
16
|
|
|
const MAX_NB_PRODUCTS = 9; |
|
17
|
|
|
|
|
18
|
|
|
protected $name = 'db:faker'; |
|
19
|
|
|
protected $description = 'Seed the database with fake data for testing purposes'; |
|
20
|
|
|
|
|
21
|
|
|
private $faker; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct(); |
|
26
|
|
|
|
|
27
|
|
|
$this->faker = Factory::create(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function fire() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->error("\nWatch out!"); |
|
33
|
|
|
$this->info('This command will truncate all your Barmate data.'); |
|
34
|
|
|
$this->info("Use this command only for testing purposes.\n"); |
|
35
|
|
|
|
|
36
|
|
|
if( $this->confirm('Are you sure you want to continue? ', false)==false ) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$this->info('Command aborted'); |
|
39
|
|
|
return 1; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
Artisan::call('db:seed', ['--force'=>true]); |
|
43
|
|
|
$this->info('Created administrator account \'[email protected]\' with password \'password\'.'); |
|
44
|
|
|
|
|
45
|
|
|
$groupId = Groups::firstOrFail()->group_id; |
|
46
|
|
|
|
|
47
|
|
|
$this->seedUsers($groupId); |
|
48
|
|
|
$this->seedCategories($groupId); |
|
49
|
|
|
|
|
50
|
|
|
$this->info("\nFake data successfully created. Happy testing!"); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function getArguments() |
|
54
|
|
|
{ |
|
55
|
|
|
return []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function getOptions() |
|
59
|
|
|
{ |
|
60
|
|
|
return []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function seedUsers($groupId) |
|
64
|
|
|
{ |
|
65
|
|
|
for($i=0; $i<self::NB_USERS; $i++) |
|
66
|
|
|
{ |
|
67
|
|
|
$firstName = $this->faker->firstName; |
|
68
|
|
|
$lastName = $this->faker->lastName; |
|
69
|
|
|
$email = strtolower($firstName.'.'.$lastName.'@'.$this->faker->domainName); |
|
70
|
|
|
|
|
71
|
|
|
User::create([ 'firstname' => $firstName, |
|
72
|
|
|
'lastname' => $lastName, |
|
73
|
|
|
'group_id' => $groupId, |
|
74
|
|
|
'email' => $email, |
|
75
|
|
|
'password_hash' => Hash::make('password'), |
|
76
|
|
|
'role' => ( $i%2 == 0 ) ? 'USER' : 'MNGR', |
|
77
|
|
|
'inscription_date' => $this->faker->dateTime, |
|
78
|
|
|
'is_active' => ( $i%5 == 0 ) ? false : true ]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->info('Added '.self::NB_USERS.' users to the application with password \'password\'.'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function seedCategories($groupId) |
|
85
|
|
|
{ |
|
86
|
|
|
DB::table('categories')->delete(); |
|
87
|
|
|
|
|
88
|
|
|
$categories = ['Saison', 'Gueuze', 'Tripel', 'Witbier', 'Dubbel']; |
|
89
|
|
|
shuffle($categories); |
|
90
|
|
|
|
|
91
|
|
|
foreach($categories as $categoryName) |
|
92
|
|
|
{ |
|
93
|
|
|
$category = Categories::create(['group_id' => $groupId, |
|
94
|
|
|
'category_title' => $categoryName]); |
|
95
|
|
|
|
|
96
|
|
|
$nbProducts = rand(1, self::MAX_NB_PRODUCTS); |
|
97
|
|
|
|
|
98
|
|
|
for($i=1; $i<=$nbProducts; $i++) |
|
99
|
|
|
{ |
|
100
|
|
|
Products::create(['category_id' => $category->category_id, |
|
|
|
|
|
|
101
|
|
|
'product_name' => $categoryName.''.$i, |
|
102
|
|
|
'description' => '', |
|
103
|
|
|
'price' => rand(1, 4), |
|
104
|
|
|
'quantity' => rand(24,48)]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->info('Created '.count($categories).' categories with products in the application.'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|