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
34ea8120
Commit
34ea8120
authored
Nov 29, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding tests for custom belongsToMany keys
parent
92debf56
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
11 deletions
+59
-11
RelationsTest.php
tests/RelationsTest.php
+34
-5
Client.php
tests/models/Client.php
+2
-2
Group.php
tests/models/Group.php
+14
-0
User.php
tests/models/User.php
+9
-4
No files found.
tests/RelationsTest.php
View file @
34ea8120
...
...
@@ -12,6 +12,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Item
::
truncate
();
Role
::
truncate
();
Client
::
truncate
();
Group
::
truncate
();
}
public
function
testHasMany
()
...
...
@@ -126,16 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'admin'
,
$role
->
type
);
}
public
function
test
HasManyAndBelongsTo
()
public
function
test
BelongsToMany
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
// Add 2 clients
$user
->
clients
()
->
save
(
new
Client
(
array
(
'name'
=>
'Pork Pies Ltd.'
)));
$user
->
clients
()
->
create
(
array
(
'name'
=>
'Buffet Bar Inc.'
));
// Refetch
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
$client
=
Client
::
with
(
'users'
)
->
first
();
// Check for relation attributes
$this
->
assertTrue
(
array_key_exists
(
'user_ids'
,
$client
->
getAttributes
()));
$this
->
assertTrue
(
array_key_exists
(
'client_ids'
,
$user
->
getAttributes
()));
$clients
=
$client
->
getRelation
(
'users'
);
$users
=
$user
->
getRelation
(
'clients'
);
...
...
@@ -194,7 +201,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this
->
assertCount
(
1
,
$client
->
users
);
}
public
function
test
HasManyAndBelongsTo
AttachesExistingModels
()
public
function
test
BelongsToMany
AttachesExistingModels
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'client_ids'
=>
array
(
'1234523'
)));
...
...
@@ -222,12 +229,34 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Add more clients
$user
->
clients
()
->
sync
(
$moreClients
);
// Refetch
$user
=
User
::
with
(
'clients'
)
->
find
(
$user
->
_id
);
// Assert there are now still 2 client objects in the relationship
$this
->
assertCount
(
2
,
$user
->
clients
);
// Assert that the new relationships name start with synced
$this
->
assertStringStartsWith
(
'synced'
,
$user
->
clients
[
0
]
->
name
);
$this
->
assertStringStartsWith
(
'synced'
,
$user
->
clients
[
1
]
->
name
);
// Assert that the new relationships name start with synced
$this
->
assertStringStartsWith
(
'synced'
,
$user
->
clients
[
0
]
->
name
);
$this
->
assertStringStartsWith
(
'synced'
,
$user
->
clients
[
1
]
->
name
);
}
public
function
testBelongsToManyCustom
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$group
=
$user
->
groups
()
->
create
(
array
(
'name'
=>
'Admins'
));
// Refetch
$user
=
User
::
find
(
$user
->
_id
);
$group
=
Group
::
find
(
$group
->
_id
);
// Check for custom relation attributes
$this
->
assertTrue
(
array_key_exists
(
'users'
,
$group
->
getAttributes
()));
$this
->
assertTrue
(
array_key_exists
(
'groups'
,
$user
->
getAttributes
()));
// Assert they are attached
$this
->
assertTrue
(
in_array
(
$group
->
_id
,
$user
->
groups
));
$this
->
assertTrue
(
in_array
(
$user
->
_id
,
$group
->
users
));
$this
->
assertEquals
(
$group
->
_id
,
$user
->
groups
()
->
first
()
->
_id
);
$this
->
assertEquals
(
$user
->
_id
,
$group
->
users
()
->
first
()
->
_id
);
}
}
tests/models/Client.php
View file @
34ea8120
...
...
@@ -6,9 +6,9 @@ class Client extends Eloquent {
protected
$collection
=
'clients'
;
protected
static
$unguarded
=
true
;
public
function
users
()
{
return
$this
->
belongsToMany
(
'User'
);
}
}
\ No newline at end of file
}
tests/models/Group.php
0 → 100644
View file @
34ea8120
<?php
use
Jenssegers\Mongodb\Model
as
Eloquent
;
class
Group
extends
Eloquent
{
protected
$collection
=
'groups'
;
protected
static
$unguarded
=
true
;
public
function
users
()
{
return
$this
->
belongsToMany
(
'User'
,
'test_collection'
,
'groups'
,
'users'
);
}
}
tests/models/User.php
View file @
34ea8120
...
...
@@ -8,9 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class
User
extends
Eloquent
implements
UserInterface
,
RemindableInterface
{
protected
$collection
=
'users'
;
protected
$dates
=
array
(
'birthday'
);
protected
static
$unguarded
=
true
;
public
function
books
()
...
...
@@ -27,12 +27,17 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return
$this
->
hasOne
(
'Role'
);
}
public
function
clients
()
{
return
$this
->
belongsToMany
(
'Client'
);
}
public
function
groups
()
{
return
$this
->
belongsToMany
(
'Group'
,
'test_collection'
,
'users'
,
'groups'
);
}
/**
* Get the unique identifier for the user.
*
...
...
@@ -62,4 +67,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return
$this
->
email
;
}
}
\ No newline at end of file
}
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