FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
framework/tests/Database/DatabaseEloquentMorphTest.php at master · JavaDevl/framework · GitHub
JavaDevl
/
framework
Public
forked from
laravel/framework
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
framework
/
tests
/
Database
/
DatabaseEloquentMorphTest.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
237 lines (199 loc) · 11.3 KB
Breadcrumbs
framework
/
tests
/
Database
/
DatabaseEloquentMorphTest.php
Copy path
File metadata and controls
executable file
·
237 lines (199 loc) · 11.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
use
Mockery
as
m
;
use
Illuminate
\
Database
\
Eloquent
\
Model
;
use
Illuminate
\
Database
\
Eloquent
\
Relations
\
MorphOne
;
use
Illuminate
\
Database
\
Eloquent
\
Relations
\
Relation
;
use
Illuminate
\
Database
\
Eloquent
\
Relations
\
MorphMany
;
class
DatabaseEloquentMorphTest
extends
PHPUnit_Framework_TestCase
{
public
function
tearDown
()
{
Relation::
morphMap
([],
false
);
m::
close
();
}
public
function
testMorphOneSetsProperConstraints
()
{
$
relation
=
$
this
->
getOneRelation
();
}
public
function
testMorphOneEagerConstraintsAreProperlyAdded
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
whereIn
'
)->
once
()->
with
(
'
table.morph_id
'
, [
1
,
2
]);
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
model1
=
new
EloquentMorphResetModelStub
;
$
model1
->
id
=
1
;
$
model2
=
new
EloquentMorphResetModelStub
;
$
model2
->
id
=
2
;
$
relation
->
addEagerConstraints
([
$
model1
,
$
model2
]);
}
/**
* Note that the tests are the exact same for morph many because the classes share this code...
* Will still test to be safe.
*/
public
function
testMorphManySetsProperConstraints
()
{
$
relation
=
$
this
->
getManyRelation
();
}
public
function
testMorphManyEagerConstraintsAreProperlyAdded
()
{
$
relation
=
$
this
->
getManyRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
whereIn
'
)->
once
()->
with
(
'
table.morph_id
'
, [
1
,
2
]);
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
model1
=
new
EloquentMorphResetModelStub
;
$
model1
->
id
=
1
;
$
model2
=
new
EloquentMorphResetModelStub
;
$
model2
->
id
=
2
;
$
relation
->
addEagerConstraints
([
$
model1
,
$
model2
]);
}
public
function
testCreateFunctionOnMorph
()
{
// Doesn't matter which relation type we use since they share the code...
$
relation
=
$
this
->
getOneRelation
();
$
created
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
created
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_id
'
,
1
);
$
created
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
once
()->
with
([
'
name
'
=>
'
taylor
'
])->
andReturn
(
$
created
);
$
created
->
shouldReceive
(
'
save
'
)->
once
()->
andReturn
(
true
);
$
this
->
assertEquals
(
$
created
,
$
relation
->
create
([
'
name
'
=>
'
taylor
'
]));
}
public
function
testFindOrNewMethodFindsModel
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
find
'
)->
once
()->
with
(
'
foo
'
, [
'
*
'
])->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
never
();
$
model
->
shouldReceive
(
'
setAttribute
'
)->
never
();
$
model
->
shouldReceive
(
'
save
'
)->
never
();
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
findOrNew
(
'
foo
'
));
}
public
function
testFindOrNewMethodReturnsNewModelWithMorphKeysSet
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
find
'
)->
once
()->
with
(
'
foo
'
, [
'
*
'
])->
andReturn
(
null
);
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
once
()->
with
()->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_id
'
,
1
);
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
model
->
shouldReceive
(
'
save
'
)->
never
();
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
findOrNew
(
'
foo
'
));
}
public
function
testFirstOrNewMethodFindsFirstModel
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
relation
->
getQuery
());
$
relation
->
getQuery
()->
shouldReceive
(
'
first
'
)->
once
()->
with
()->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
never
();
$
model
->
shouldReceive
(
'
setAttribute
'
)->
never
();
$
model
->
shouldReceive
(
'
save
'
)->
never
();
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
firstOrNew
([
'
foo
'
]));
}
public
function
testFirstOrNewMethodReturnsNewModelWithMorphKeysSet
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
relation
->
getQuery
());
$
relation
->
getQuery
()->
shouldReceive
(
'
first
'
)->
once
()->
with
()->
andReturn
(
null
);
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_id
'
,
1
);
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
model
->
shouldReceive
(
'
save
'
)->
never
();
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
firstOrNew
([
'
foo
'
]));
}
public
function
testFirstOrCreateMethodFindsFirstModel
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
relation
->
getQuery
());
$
relation
->
getQuery
()->
shouldReceive
(
'
first
'
)->
once
()->
with
()->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
never
();
$
model
->
shouldReceive
(
'
setAttribute
'
)->
never
();
$
model
->
shouldReceive
(
'
save
'
)->
never
();
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
firstOrCreate
([
'
foo
'
]));
}
public
function
testFirstOrCreateMethodCreatesNewMorphModel
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
relation
->
getQuery
());
$
relation
->
getQuery
()->
shouldReceive
(
'
first
'
)->
once
()->
with
()->
andReturn
(
null
);
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_id
'
,
1
);
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
model
->
shouldReceive
(
'
save
'
)->
once
()->
andReturn
(
true
);
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
firstOrCreate
([
'
foo
'
]));
}
public
function
testUpdateOrCreateMethodFindsFirstModelAndUpdates
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
relation
->
getQuery
());
$
relation
->
getQuery
()->
shouldReceive
(
'
first
'
)->
once
()->
with
()->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
never
();
$
model
->
shouldReceive
(
'
setAttribute
'
)->
never
();
$
model
->
shouldReceive
(
'
fill
'
)->
once
()->
with
([
'
bar
'
]);
$
model
->
shouldReceive
(
'
save
'
)->
once
();
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
updateOrCreate
([
'
foo
'
], [
'
bar
'
]));
}
public
function
testUpdateOrCreateMethodCreatesNewMorphModel
()
{
$
relation
=
$
this
->
getOneRelation
();
$
relation
->
getQuery
()->
shouldReceive
(
'
where
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
relation
->
getQuery
());
$
relation
->
getQuery
()->
shouldReceive
(
'
first
'
)->
once
()->
with
()->
andReturn
(
null
);
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
once
()->
with
([
'
foo
'
])->
andReturn
(
$
model
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
));
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_id
'
,
1
);
$
model
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_type
'
,
get_class
(
$
relation
->
getParent
()));
$
model
->
shouldReceive
(
'
save
'
)->
once
()->
andReturn
(
true
);
$
model
->
shouldReceive
(
'
fill
'
)->
once
()->
with
([
'
bar
'
]);
$
this
->
assertInstanceOf
(Model::class,
$
relation
->
updateOrCreate
([
'
foo
'
], [
'
bar
'
]));
}
public
function
testCreateFunctionOnNamespacedMorph
()
{
$
relation
=
$
this
->
getNamespacedRelation
(
'
namespace
'
);
$
created
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
created
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_id
'
,
1
);
$
created
->
shouldReceive
(
'
setAttribute
'
)->
once
()->
with
(
'
morph_type
'
,
'
namespace
'
);
$
relation
->
getRelated
()->
shouldReceive
(
'
newInstance
'
)->
once
()->
with
([
'
name
'
=>
'
taylor
'
])->
andReturn
(
$
created
);
$
created
->
shouldReceive
(
'
save
'
)->
once
()->
andReturn
(
true
);
$
this
->
assertEquals
(
$
created
,
$
relation
->
create
([
'
name
'
=>
'
taylor
'
]));
}
protected
function
getOneRelation
()
{
$
builder
= m::
mock
(
'
Illuminate\Database\Eloquent\Builder
'
);
$
builder
->
shouldReceive
(
'
whereNotNull
'
)->
once
()->
with
(
'
table.morph_id
'
);
$
builder
->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_id
'
,
'
=
'
,
1
);
$
related
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
builder
->
shouldReceive
(
'
getModel
'
)->
andReturn
(
$
related
);
$
parent
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
parent
->
shouldReceive
(
'
getAttribute
'
)->
with
(
'
id
'
)->
andReturn
(
1
);
$
parent
->
shouldReceive
(
'
getMorphClass
'
)->
andReturn
(
get_class
(
$
parent
));
$
builder
->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_type
'
,
get_class
(
$
parent
));
return
new
MorphOne
(
$
builder
,
$
parent
,
'
table.morph_type
'
,
'
table.morph_id
'
,
'
id
'
);
}
protected
function
getManyRelation
()
{
$
builder
= m::
mock
(
'
Illuminate\Database\Eloquent\Builder
'
);
$
builder
->
shouldReceive
(
'
whereNotNull
'
)->
once
()->
with
(
'
table.morph_id
'
);
$
builder
->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_id
'
,
'
=
'
,
1
);
$
related
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
builder
->
shouldReceive
(
'
getModel
'
)->
andReturn
(
$
related
);
$
parent
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
parent
->
shouldReceive
(
'
getAttribute
'
)->
with
(
'
id
'
)->
andReturn
(
1
);
$
parent
->
shouldReceive
(
'
getMorphClass
'
)->
andReturn
(
get_class
(
$
parent
));
$
builder
->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_type
'
,
get_class
(
$
parent
));
return
new
MorphMany
(
$
builder
,
$
parent
,
'
table.morph_type
'
,
'
table.morph_id
'
,
'
id
'
);
}
protected
function
getNamespacedRelation
(
$
alias
)
{
require_once
__DIR__
.
'
/stubs/EloquentModelNamespacedStub.php
'
;
Relation::
morphMap
([
$
alias
=>
'
Foo\Bar\EloquentModelNamespacedStub
'
,
]);
$
builder
= m::
mock
(
'
Illuminate\Database\Eloquent\Builder
'
);
$
builder
->
shouldReceive
(
'
whereNotNull
'
)->
once
()->
with
(
'
table.morph_id
'
);
$
builder
->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_id
'
,
'
=
'
,
1
);
$
related
= m::
mock
(
'
Illuminate\Database\Eloquent\Model
'
);
$
builder
->
shouldReceive
(
'
getModel
'
)->
andReturn
(
$
related
);
$
parent
= m::
mock
(
'
Foo\Bar\EloquentModelNamespacedStub
'
);
$
parent
->
shouldReceive
(
'
getAttribute
'
)->
with
(
'
id
'
)->
andReturn
(
1
);
$
parent
->
shouldReceive
(
'
getMorphClass
'
)->
andReturn
(
$
alias
);
$
builder
->
shouldReceive
(
'
where
'
)->
once
()->
with
(
'
table.morph_type
'
,
$
alias
);
return
new
MorphOne
(
$
builder
,
$
parent
,
'
table.morph_type
'
,
'
table.morph_id
'
,
'
id
'
);
}
}
class
EloquentMorphResetModelStub
extends
Illuminate
\
Database
\
Eloquent
\Model
{
}
Back
|
FazBrowse Home
|
New Git URL