Skip to content

Commit 739b0c0

Browse files
Improve tests and type of insertions
1 parent b5fa3f2 commit 739b0c0

File tree

3 files changed

+120
-9
lines changed

3 files changed

+120
-9
lines changed

src/interactive-model/InteractiveModelCommand.php

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace LaravelLegends\InteractiveModel;
44

5+
use Carbon\Carbon;
6+
use Carbon\Exceptions\InvalidFormatException;
57
use Illuminate\Console\Command;
68
use Illuminate\Database\Eloquent\Model;
79

@@ -13,7 +15,7 @@
1315

1416
class InteractiveModelCommand extends Command
1517
{
16-
protected $signature = 'll:interactive-model {model}';
18+
protected $signature = 'model:interactive {model}';
1719

1820
protected $description = 'Insert data in your model interactively';
1921

@@ -23,11 +25,9 @@ class InteractiveModelCommand extends Command
2325
*/
2426
public function handle()
2527
{
26-
2728
$instance = $this->getModelInstance();
2829

2930
foreach ($instance->getFillable() as $field) {
30-
3131
$value = $this->getFieldValue($instance, $field);
3232

3333
$instance->setAttribute($field, $value);
@@ -78,15 +78,15 @@ protected function getModelInstance(): Model
7878
*
7979
* @param Model $model
8080
* @param string $field
81-
* @return string
81+
* @return mixed
8282
*/
83-
protected function getFieldValue(Model $model, string $field): ?string
83+
protected function getFieldValue(Model $model, string $field)
8484
{
85-
$method = in_array($field, $model->getHidden()) ? 'secret' : 'ask';
85+
$castType = $model->getCasts()[$field] ?? 'string';
8686

87-
$value = $this->$method("Type the \"$field\" value", false);
87+
$value = $this->getByCastType($model, $castType, $field);
8888

89-
return $value === false ? null : $value;
89+
return $value;
9090
}
9191

9292
/**
@@ -105,4 +105,59 @@ protected function showInsertedModel(Model $model): void
105105

106106
$this->table(array_keys($attributes), [$attributes]);
107107
}
108-
}
108+
109+
110+
protected function getByCastType(Model $model, string $castType, string $field)
111+
{
112+
if (in_array($castType, ['datetime', 'date', 'custom_datetime'])) {
113+
114+
$value = $this->ask($this->getAskForField($field));
115+
116+
try {
117+
$value = Carbon::parse($value);
118+
} catch (InvalidFormatException $e) {
119+
$value = $this->getByCastType($model, $castType, $field);
120+
}
121+
122+
return $value;
123+
124+
} elseif (in_array($castType, ['json', 'array'])) {
125+
$value = $this->ask($this->getAskForField($field));
126+
127+
$value = json_decode($value, true);
128+
129+
if (json_last_error() > 0) {
130+
$value = $this->getByCastType($model, $castType, $field);
131+
}
132+
133+
return $value;
134+
135+
} elseif (in_array($castType, ['bool', 'boolean'])) {
136+
137+
$value = strtolower(
138+
$this->ask($this->getAskForField($field))
139+
);
140+
141+
if (! in_array($value, ['true', 'false'])) {
142+
$value = strtolower(
143+
$this->ask($this->getAskForField($field))
144+
);
145+
}
146+
147+
return $value === 'true';
148+
149+
}
150+
151+
$method = in_array($field, $model->getHidden()) ? 'secret' : 'ask';
152+
153+
$value = $this->$method($this->getAskForField($field));
154+
155+
return $value === 'NULL' ? null : $value;
156+
157+
}
158+
159+
protected function getAskForField(string $field)
160+
{
161+
return "Type the \"$field\" value";
162+
}
163+
}

tests/IteractiveModelCommandTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,38 @@
22

33
use Illuminate\Console\Command;
44
use LaravelLegends\InteractiveModel\InteractiveModelCommand;
5+
use LaravelLegends\InteractiveModel\InteractiveModelServiceProvider;
6+
use Models\User;
57

68
class IteractiveModelCommandTest extends Orchestra\Testbench\TestCase
79
{
10+
811
public function testInstance()
912
{
1013
$cmd = new InteractiveModelCommand();
1114

1215
$this->assertInstanceOf(Command::class, $cmd);
1316
}
17+
18+
public function testConsoleCommand()
19+
{
20+
$command = $this->artisan(
21+
'model:interactive',
22+
['model' => User::class]
23+
)
24+
->expectsQuestion('Type the "name" value', 'Wallace')
25+
->expectsQuestion('Type the "email" value', '[email protected]')
26+
->expectsQuestion('Type the "password" value', 'NULL')
27+
->expectsQuestion('Type the "last_login" value', 'invalid_date') // erro proposital pra repetir
28+
->expectsQuestion('Type the "last_login" value', '2021-11-12')
29+
->expectsQuestion('Type the "metadata" value', '[') // erro proposital
30+
->expectsQuestion('Type the "metadata" value', '{"open_menu" : true}')
31+
->expectsQuestion('Type the "active" value', '123')
32+
->expectsQuestion('Type the "active" value', 'True');
33+
}
34+
35+
protected function getPackageProviders($app)
36+
{
37+
return [InteractiveModelServiceProvider::class];
38+
}
1439
}

tests/Models/User.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class User extends Model
8+
{
9+
protected $fillable = [
10+
'name',
11+
'email',
12+
'password',
13+
'last_login',
14+
'metadata',
15+
'active'
16+
];
17+
18+
protected $hidden = ['password'];
19+
20+
protected $casts = [
21+
'metadata' => 'json',
22+
'name' => 'string',
23+
'last_login' => 'datetime',
24+
'active' => 'boolean',
25+
];
26+
27+
public function save(array $options = [])
28+
{
29+
return true;
30+
}
31+
}

0 commit comments

Comments
 (0)