-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFormMakerTest.php
108 lines (88 loc) · 3.85 KB
/
FormMakerTest.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
use Grafite\FormMaker\Fields\Text;
use Grafite\FormMaker\Fields\TextArea;
use Illuminate\Database\Eloquent\Model;
use Grafite\FormMaker\Services\FormMaker;
use Illuminate\Database\Eloquent\SoftDeletes;
class Entry extends Model
{
use SoftDeletes;
public $fillable = [
'name',
'details',
];
public function getMetaAttribute()
{
return (object) [
'user' => (object) [
'id' => 1,
],
'created_at' => \Carbon\Carbon::create(1999, 1, 1, 6, 15, 0),
'updated_at' => \Carbon\Carbon::create(1999, 1, 1, 6, 15, 0),
'deleted_at' => null
];
}
}
class FormMakerTest extends TestCase
{
protected $app;
protected $formMaker;
public function setUp(): void
{
parent::setUp();
$this->formMaker = app(FormMaker::class);
}
public function testSetConnection()
{
$test = $this->formMaker->setConnection('alternate');
$this->assertTrue(is_string($test->connection));
$this->assertEquals('alternate', $test->connection);
}
public function testFromTable()
{
$test = $this->formMaker->setConnection('testbench')->fromTable('entries');
$this->assertTrue(is_string($test));
$this->assertEquals('<div class="form-group"><label class="control-label" for="Name">Name</label><input class="form-control" id="Name" name="name" type="text" value=""></div><div class="form-group"><label class="control-label" for="Details">Details</label><textarea class="form-control" id="Details" name="details"></textarea></div>', $test);
}
public function testFromFields()
{
$test = $this->formMaker->fromFields([
Text::make('name'),
TextArea::make('details'),
]);
$this->assertTrue(is_string($test));
$this->assertEquals('<div class="form-group"><label class="control-label" for="Name">Name</label><input class="form-control" id="Name" name="name" type="text" value=""></div><div class="form-group"><label class="control-label" for="Details">Details</label><textarea class="form-control" id="Details" rows="5" name="details"></textarea></div>', $test);
}
public function testFromTableSimulated()
{
$entry = app(Entry::class)->create([
'name' => 'test entry',
'details' => 'this entry is written in [markdown](http://markdown.com)',
]);
$test = $this->formMaker
->setConnection('testbench')
->fromObject($entry, $this->formMaker->getTableAsFields('entries'));
$this->assertTrue(is_string($test));
$this->assertEquals('<div class="form-group"><label class="control-label" for="Name">Name</label><input class="form-control" id="Name" name="name" type="text" value="test entry"></div><div class="form-group"><label class="control-label" for="Details">Details</label><textarea class="form-control" id="Details" name="details">this entry is written in [markdown](http://markdown.com)</textarea></div>', $test);
}
public function testFromObject()
{
$testObject = [
'attributes' => [
'name' => 'Joe',
'age' => 18,
],
];
$columns = [
'name' => [
'type' => 'text',
],
'age' => [
'type' => 'number',
],
];
$test = $this->formMaker->fromObject((object) $testObject, $columns);
$this->assertTrue(is_string($test));
$this->assertEquals('<div class="form-group"><label class="control-label" for="Name">Name</label><input class="form-control" id="Name" name="name" type="text" value=""></div><div class="form-group"><label class="control-label" for="Age">Age</label><input class="form-control" id="Age" name="age" type="number" value=""></div>', $test);
}
}