|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use DotEnvWriter\DotEnvWriter; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Mockery\Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Setup the development environment for MyIO. The command |
|
11
|
|
|
* will auto generate the .env.testing file for you. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CreateDev extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The name and signature of the console command. |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $signature = 'myio:testenv'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command description. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $description = 'Setup the testing environment'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This testing environment file to write. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $envFile = '.env.testing'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* We will write our tests into this |
|
37
|
|
|
* database file. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $dbFile = './database/database.sqlite'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $defaults = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create a new command instance. |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function __construct() |
|
52
|
|
|
{ |
|
53
|
12 |
|
parent::__construct(); |
|
54
|
12 |
|
$this->defaults = [ |
|
55
|
12 |
|
'APP_ENV' => 'local', |
|
56
|
12 |
|
'APP_KEY' => $this->createAppKey(), |
|
57
|
12 |
|
'APP_DEBUG' => 'true', |
|
58
|
12 |
|
'APP_LOG_LEVEL' => 'debug', |
|
59
|
12 |
|
'APP_URL' => config('app.url'), |
|
60
|
12 |
|
'DB_CONNECTION' => 'sqlite', |
|
61
|
12 |
|
'BROADCAST_DRIVER' => 'log', |
|
62
|
12 |
|
'CACHE_DRIVER' => 'file', |
|
63
|
12 |
|
'SESSION_DRIVER' => 'file', |
|
64
|
12 |
|
'QUEUE_DRIVER' => 'sync', |
|
65
|
|
|
]; |
|
66
|
12 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Generate a unique APP key, This is |
|
70
|
|
|
* code i stole and modified from the |
|
71
|
|
|
* core. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
12 |
|
private function createAppKey() |
|
76
|
|
|
{ |
|
77
|
12 |
|
return 'base64:'.base64_encode(random_bytes(config('app.cipher') == 'AES-128-CBC' ? 16 : 32)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create the sqlite file that we |
|
82
|
|
|
* need for test database |
|
83
|
|
|
* transactions. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
private function createDatabase() |
|
88
|
|
|
{ |
|
89
|
|
|
return touch($this->dbFile); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Fil in the fields so we can write them |
|
94
|
|
|
* to the .env.testing file. |
|
95
|
|
|
*/ |
|
96
|
|
|
private function createEnvironment() |
|
97
|
|
|
{ |
|
98
|
|
|
try { |
|
99
|
|
|
$writer = new DotEnvWriter($this->envFile); |
|
100
|
|
|
foreach ($this->defaults as $key => $val) { |
|
101
|
|
|
$writer->set($key, $val); |
|
102
|
|
|
} |
|
103
|
|
|
$writer->save(); |
|
104
|
|
|
$status = true; |
|
105
|
|
|
} catch (Exception $e) { |
|
106
|
|
|
$status = false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $status; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Handle the execution of the console |
|
114
|
|
|
* command. |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function handle() |
|
119
|
|
|
{ |
|
120
|
|
|
$steps = [ |
|
121
|
|
|
'database' => [ |
|
122
|
|
|
'callback' => [$this, 'createDatabase'], |
|
123
|
|
|
'messages' => [ |
|
124
|
|
|
'success' => 'Created '.$this->dbFile, |
|
125
|
|
|
'error' => 'Failed to create '.$this->dbFile, |
|
126
|
|
|
], |
|
127
|
|
|
], |
|
128
|
|
|
'environment' => [ |
|
129
|
|
|
'callback' => [$this, 'createEnvironment'], |
|
130
|
|
|
'messages' => [ |
|
131
|
|
|
'success' => 'Created '.$this->envFile, |
|
132
|
|
|
'error' => 'Failed to create '.$this->envFile, |
|
133
|
|
|
], |
|
134
|
|
|
], |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
$this->info('Welcome to the '.config('app.name')." testing environment. \r\n"); |
|
138
|
|
|
|
|
139
|
|
|
foreach ($steps as $step) { |
|
140
|
|
|
$result = call_user_func($step['callback']); |
|
141
|
|
|
if ($result == true) { |
|
142
|
|
|
$this->info($step['messages']['success']); |
|
143
|
|
|
} else { |
|
144
|
|
|
$this->info($step['messages']['error']); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$this->info("\r\nHappy testing ... "); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|