Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
bc9b05ed
Commit
bc9b05ed
authored
Nov 20, 2013
by
unknown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Relations test updated
parent
e93b2625
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
21 deletions
+89
-21
RelationsTest.php
tests/RelationsTest.php
+89
-21
No files found.
tests/RelationsTest.php
View file @
bc9b05ed
...
@@ -3,6 +3,11 @@
...
@@ -3,6 +3,11 @@
class
RelationsTest
extends
PHPUnit_Framework_TestCase
{
class
RelationsTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{
public
function
setUp
()
{
User
::
truncate
();
Book
::
truncate
();
Item
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
}
}
public
function
tearDown
()
public
function
tearDown
()
...
@@ -11,6 +16,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -11,6 +16,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Book
::
truncate
();
Book
::
truncate
();
Item
::
truncate
();
Item
::
truncate
();
Role
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
}
}
public
function
testHasMany
()
public
function
testHasMany
()
...
@@ -102,27 +108,89 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -102,27 +108,89 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
}
public
function
test
EasyRelation
()
public
function
test
HasManyAndBelongsTo
()
{
{
// Has Many
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$item
=
Item
::
create
(
array
(
'type'
=>
'knife'
));
$user
->
items
()
->
save
(
$item
);
$user
=
User
::
find
(
$user
->
_id
);
$user
->
clients
()
->
save
(
new
Client
(
array
(
'name'
=>
'Pork Pies Ltd.'
)));
$items
=
$user
->
items
;
$user
->
clients
()
->
create
(
array
(
'name'
=>
'Buffet Bar Inc.'
));
$this
->
assertEquals
(
1
,
count
(
$items
));
$this
->
assertInstanceOf
(
'Item'
,
$items
[
0
]);
// Has one
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$role
=
Role
::
create
(
array
(
'type'
=>
'admin'
));
$user
->
role
()
->
save
(
$role
);
$user
=
User
::
find
(
$user
->
_id
);
$client
=
Client
::
with
(
'users'
)
->
first
();
$role
=
$user
->
role
;
$this
->
assertInstanceOf
(
'Role'
,
$role
);
$clients
=
$client
->
getRelation
(
'users'
);
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
$users
=
$user
->
getRelation
(
'clients'
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$users
);
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$clients
);
$this
->
assertInstanceOf
(
'Client'
,
$users
[
0
]);
$this
->
assertInstanceOf
(
'User'
,
$clients
[
0
]);
$this
->
assertCount
(
2
,
$user
->
clients
);
$this
->
assertCount
(
1
,
$client
->
users
);
// Now create a new user to an existing client
$client
->
users
()
->
create
(
array
(
'name'
=>
'Jane Doe'
));
$otherClient
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
()
->
clients
()
->
get
();
$this
->
assertInstanceOf
(
'Illuminate\Database\Eloquent\Collection'
,
$otherClient
);
$this
->
assertInstanceOf
(
'Client'
,
$otherClient
[
0
]);
$this
->
assertCount
(
1
,
$otherClient
);
// Now attach an existing client to an existing user
$user
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
();
$client
=
Client
::
Where
(
'name'
,
'='
,
'Buffet Bar Inc.'
)
->
first
();
// Check the models are what they should be
$this
->
assertInstanceOf
(
'Client'
,
$client
);
$this
->
assertInstanceOf
(
'User'
,
$user
);
// Assert they are not attached
$this
->
assertFalse
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertFalse
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
// Attach the client to the user
$user
->
clients
()
->
attach
(
$client
);
// Get the new user model
$user
=
User
::
where
(
'name'
,
'='
,
'Jane Doe'
)
->
first
();
$client
=
Client
::
Where
(
'name'
,
'='
,
'Buffet Bar Inc.'
)
->
first
();
// Assert they are attached
$this
->
assertTrue
(
in_array
(
$client
->
_id
,
$user
->
client_ids
));
$this
->
assertTrue
(
in_array
(
$user
->
_id
,
$client
->
user_ids
));
}
}
public
function
testHasManyAndBelongsToAttachesExistingModels
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'client_ids'
=>
array
(
'1234523'
)));
$clients
=
array
(
Client
::
create
(
array
(
'name'
=>
'Pork Pies Ltd.'
))
->
_id
,
Client
::
create
(
array
(
'name'
=>
'Buffet Bar Inc.'
))
->
_id
);
$moreClients
=
array
(
Client
::
create
(
array
(
'name'
=>
'Boloni Ltd.'
))
->
_id
,
Client
::
create
(
array
(
'name'
=>
'Meatballs Inc.'
))
->
_id
);
$user
->
clients
()
->
sync
(
$clients
);
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
// Assert non attached ID's are detached succesfully
$this
->
assertFalse
(
in_array
(
'1234523'
,
$user
->
client_ids
));
// Assert there are two client objects in the relationship
$this
->
assertCount
(
2
,
$user
->
clients
);
$user
->
clients
()
->
sync
(
$moreClients
);
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
// Assert there are now 4 client objects in the relationship
$this
->
assertCount
(
4
,
$user
->
clients
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment