/
ModelCollectionAttributesCanBeBoundDirectlyUnitTest.php
203 lines (169 loc) · 6.24 KB
/
ModelCollectionAttributesCanBeBoundDirectlyUnitTest.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Livewire\Features\SupportLegacyModels\Tests;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Livewire\Component;
use Livewire\Features\SupportLegacyModels\CannotBindToModelDataWithoutValidationRuleException;
use Livewire\Livewire;
use Livewire\Mechanisms\HandleComponents\CorruptComponentPayloadException;
use Sushi\Sushi;
use function Livewire\invade;
class ModelCollectionAttributesCanBeBoundDirectlyUnitTest extends \Tests\TestCase
{
use Concerns\EnableLegacyModels;
public function test_can_set_a_model_attribute_inside_a_models_collection_and_save()
{
// Reset Sushi model.
(new ModelForBinding)->resolveConnection()->getSchemaBuilder()->drop((new ModelForBinding)->getTable());
(new ModelForBinding)->migrate();
Livewire::test(ComponentWithModelCollectionProperty::class)
->assertSet('models.0.title', 'foo')
->assertSnapshotSet('models.0.title', 'foo')
->set('models.0.title', 'bo')
->assertSet('models.0.title', 'bo')
->call('refreshModels')
->assertSet('models.0.title', 'foo')
->set('models.0.title', 'bo')
->call('save')
->assertHasErrors('models.0.title')
->set('models.0.title', 'boo')
->call('save')
->call('refreshModels')
->assertSet('models.0.title', 'boo');
}
public function test_can_set_non_persisted_models_in_model_collection()
{
// Reset Sushi model.
(new ModelForBinding)->resolveConnection()->getSchemaBuilder()->drop((new ModelForBinding)->getTable());
(new ModelForBinding)->migrate();
Livewire::test(ComponentWithModelCollectionProperty::class)
->assertSet('models.2.title', 'baz')
->assertSet('models.3', null)
->assertSnapshotSet('models.3', null)
->call('addModel')
->assertNotSet('models.3', null)
->assertSnapshotNotSet('models.3', null)
->set('models.3.title', 'bob')
->assertSet('models.3.title', 'bob')
->assertSnapshotSet('models.3.title', 'bob')
->set('models.3.title', 'bo')
->call('refreshModels')
->assertSet('models.3', null)
->assertSnapshotSet('models.3', null)
->call('addModel')
->set('models.3.title', 'bo')
->call('save')
->assertHasErrors('models.3.title')
->set('models.3.title', 'boo')
->call('save')
->call('refreshModels')
->assertSet('models.3.title', 'boo');
;
}
public function test_can_use_a_custom_model_collection_and_bind_to_values()
{
// Reset Sushi model.
(new ModelWithCustomCollectionForBinding)->resolveConnection()->getSchemaBuilder()->drop((new ModelWithCustomCollectionForBinding)->getTable());
(new ModelWithCustomCollectionForBinding)->migrate();
Livewire::test(ComponentWithModelCollectionProperty::class)
->call('setModelsToCustomCollection')
->assertSet('models.0.title', 'foo')
->assertSnapshotSet('models.0.title', 'foo')
->set('models.0.title', 'bo')
->assertSet('models.0.title', 'bo')
->call('refreshModels')
->assertSet('models.0.title', 'foo')
->set('models.0.title', 'bo')
->call('save')
->assertHasErrors('models.0.title')
->set('models.0.title', 'boo')
->call('save')
->call('refreshModels')
->assertSet('models.0.title', 'boo')
->call('getTypeOfModels')
->assertSet('modelsType', CustomCollection::class);
}
public function test_cant_set_a_model_attribute_that_isnt_present_in_rules_array()
{
// Reset Sushi model.
(new ModelForBinding)->resolveConnection()->getSchemaBuilder()->drop((new ModelForBinding)->getTable());
(new ModelForBinding)->migrate();
$this->expectException(CannotBindToModelDataWithoutValidationRuleException::class);
Livewire::test(ComponentWithModelCollectionProperty::class)
->set('models.1.restricted', 'bar')
->assertSet('models.1.restricted', null);
}
public function test_an_eloquent_models_meta_cannot_be_hijacked_by_tampering_with_data()
{
// Reset Sushi model.
(new ModelForBinding)->resolveConnection()->getSchemaBuilder()->drop((new ModelForBinding)->getTable());
(new ModelForBinding)->migrate();
$this->expectException(CorruptComponentPayloadException::class);
$component = Livewire::test(ComponentWithModelCollectionProperty::class);
invade(invade($component)->lastState)->snapshot['data']['models'][0]['id'] = 2;
$component->call('$refresh');
}
}
class ModelForBinding extends Model
{
use Sushi;
protected $rows = [
['title' => 'foo'],
['title' => 'bar'],
['title' => 'baz'],
];
}
class CustomCollection extends EloquentCollection
{
//
}
class ModelWithCustomCollectionForBinding extends Model
{
use Sushi;
protected $rows = [
['title' => 'foo'],
['title' => 'bar'],
['title' => 'baz'],
];
public function newCollection(array $models = [])
{
return new CustomCollection($models);
}
}
class ComponentWithModelCollectionProperty extends Component
{
public $models;
public $modelsType;
protected $rules = [
'models.*.title' => 'required|min:3',
];
public function mount()
{
$this->models = ModelForBinding::all();
}
public function addModel()
{
$this->models->push(new ModelForBinding);
}
public function setModelsToCustomCollection()
{
$this->models = ModelWithCustomCollectionForBinding::all();
}
public function getTypeOfModels()
{
$this->modelsType = get_class($this->models);
}
public function save()
{
$this->validate();
$this->models->each->save();
}
public function refreshModels()
{
$this->models = $this->models->filter->exists->fresh();
}
public function render()
{
return view('null-view');
}
}