Skip to content

Commit b70b4ff

Browse files
author
vagrant
committed
Add regression test for bugfix
1 parent 39aa347 commit b70b4ff

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

tests/HasNestedAttributesTraitTest.php

+50-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ public function setUp()
1313
// Mock the Model that uses the custom traits
1414
$this->model = Mockery::mock('ModelEloquentStub');
1515
$this->model->makePartial();
16+
17+
// Mock the Model without fillable array set
18+
$this->modelWithoutFillable = Mockery::mock('ModelEloquentStubWithoutFillable');
19+
$this->modelWithoutFillable->makePartial();
20+
21+
// Default payload for Model
22+
$this->payload = [
23+
'model_bar' => ['text' => 'bar'],
24+
'model_foos' => [
25+
['text' => 'foo1'],
26+
['text' => 'foo2'],
27+
]
28+
];
1629
}
1730

1831
/**
@@ -28,55 +41,72 @@ public function tearDown()
2841
*/
2942
public function testFillable()
3043
{
31-
$payload = [
32-
'title' => 'foo',
33-
'not_exists' => [],
44+
$this->model->fill(array_merge($this->payload, ['title' => 'foo', 'not_exists' => []]));
45+
46+
$this->assertEquals(['title' => 'foo'], $this->model->getAttributes());
47+
$this->assertEquals([
3448
'model_bar' => ['text' => 'bar'],
3549
'model_foos' => [
3650
['text' => 'foo1'],
37-
['text' => 'foo2'],
51+
['text' => 'foo2']
3852
]
39-
];
53+
], $this->model->getAcceptNestedAttributesFor());
54+
}
4055

41-
$this->model->fill($payload);
56+
/**
57+
* Test that a model with nested attributes can still save without fillable array.
58+
*/
59+
public function testModelWithNestedAttributesCanSaveWithoutFillableArraySet()
60+
{
61+
$this->modelWithoutFillable->fill($this->payload);
4262

43-
$this->assertEquals(['title' => 'foo'], $this->model->getAttributes());
4463
$this->assertEquals([
4564
'model_bar' => ['text' => 'bar'],
4665
'model_foos' => [
47-
['text' => 'foo1'],
66+
['text' => 'foo1'],
4867
['text' => 'foo2']
4968
]
50-
], $this->model->getAcceptNestedAttributesFor());
69+
], $this->modelWithoutFillable->getAcceptNestedAttributesFor());
5170
}
5271
}
5372

54-
class ModelEloquentStub extends Model {
73+
class ModelEloquentStubWithoutFillable extends Model
74+
{
5575
protected $table = 'stubs';
56-
protected $fillable = ['title'];
5776
protected $nested = ['model_bar', 'model_foos' ];
58-
59-
public function modelBar() {
77+
78+
public function modelBar()
79+
{
6080
return $this->hasOne(ModelBarStub::class);
6181
}
6282

63-
public function modelFoos() {
83+
public function modelFoos()
84+
{
6485
return $this->hasOne(ModelFooStub::class);
6586
}
6687
}
6788

68-
class ModelBarStub extends Model {
89+
class ModelEloquentStub extends ModelEloquentStubWithoutFillable
90+
{
91+
protected $fillable = ['title'];
92+
}
93+
94+
class ModelBarStub extends Model
95+
{
6996
protected $fillable = ['text'];
70-
71-
public function parent() {
97+
98+
public function parent()
99+
{
72100
return $this->belongsTo(ModelEloquentStub::class);
73101
}
74102
}
75103

76-
class ModelFooStub extends Model {
104+
class ModelFooStub extends Model
105+
{
77106
protected $fillable = ['text'];
78107

79-
public function parent() {
108+
public function parent()
109+
{
80110
return $this->belongsTo(ModelEloquentStub::class);
81111
}
82-
}
112+
}

0 commit comments

Comments
 (0)