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
ac7174ae
Commit
ac7174ae
authored
Dec 23, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding some more tests for mysql relations
parent
9559b298
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
130 additions
and
48 deletions
+130
-48
phpunit.xml
phpunit.xml
+3
-0
RelationsTest.php
tests/RelationsTest.php
+36
-8
Book.php
tests/models/Book.php
+0
-2
Client.php
tests/models/Client.php
+1
-1
Group.php
tests/models/Group.php
+1
-1
Item.php
tests/models/Item.php
+1
-2
MysqlBook.php
tests/models/MysqlBook.php
+37
-0
MysqlRole.php
tests/models/MysqlRole.php
+41
-0
MysqlUser.php
tests/models/MysqlUser.php
+0
-31
Role.php
tests/models/Role.php
+0
-1
User.php
tests/models/User.php
+10
-2
No files found.
phpunit.xml
View file @
ac7174ae
...
@@ -31,5 +31,8 @@
...
@@ -31,5 +31,8 @@
<directory>
tests/ModelTest.php
</directory>
<directory>
tests/ModelTest.php
</directory>
<directory>
tests/RelationsTest.php
</directory>
<directory>
tests/RelationsTest.php
</directory>
</testsuite>
</testsuite>
<testsuite
name=
"relations"
>
<directory>
tests/RelationsTest.php
</directory>
</testsuite>
</testsuites>
</testsuites>
</phpunit>
</phpunit>
tests/RelationsTest.php
View file @
ac7174ae
...
@@ -32,7 +32,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -32,7 +32,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$items
=
$user
->
items
;
$items
=
$user
->
items
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertEquals
(
3
,
count
(
$items
));
}
}
public
function
testBelongsTo
()
public
function
testBelongsTo
()
{
{
...
@@ -264,38 +264,66 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
...
@@ -264,38 +264,66 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
{
{
// A bit dirty
// A bit dirty
MysqlUser
::
executeSchema
();
MysqlUser
::
executeSchema
();
MysqlBook
::
executeSchema
();
MysqlRole
::
executeSchema
();
$user
=
new
MysqlUser
;
$user
=
new
MysqlUser
;
$this
->
assertInstanceOf
(
'MysqlUser'
,
$user
);
$this
->
assertInstanceOf
(
'MysqlUser'
,
$user
);
$this
->
assertInstanceOf
(
'Illuminate\Database\MySqlConnection'
,
$user
->
getConnection
());
$this
->
assertInstanceOf
(
'Illuminate\Database\MySqlConnection'
,
$user
->
getConnection
());
// Mysql User
$user
->
name
=
"John Doe"
;
$user
->
name
=
"John Doe"
;
$user
->
save
();
$user
->
save
();
$this
->
assertTrue
(
is_int
(
$user
->
id
));
$this
->
assertTrue
(
is_int
(
$user
->
id
));
//
H
as many
//
SQL h
as many
$book
=
new
Book
(
array
(
'title'
=>
'Game of Thrones'
));
$book
=
new
Book
(
array
(
'title'
=>
'Game of Thrones'
));
$user
->
books
()
->
save
(
$book
);
$user
->
books
()
->
save
(
$book
);
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$this
->
assertEquals
(
1
,
count
(
$user
->
books
));
$this
->
assertEquals
(
1
,
count
(
$user
->
books
));
// MongoDB belongs to
$book
=
$user
->
books
()
->
first
();
// refetch
$book
=
$user
->
books
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$book
->
mysqlAuthor
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$book
->
mysqlAuthor
->
name
);
//
H
as one
//
SQL h
as one
$role
=
new
Role
(
array
(
'type'
=>
'admin'
));
$role
=
new
Role
(
array
(
'type'
=>
'admin'
));
$user
->
role
()
->
save
(
$role
);
$user
->
role
()
->
save
(
$role
);
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$user
=
MysqlUser
::
find
(
$user
->
id
);
// refetch
$this
->
assertEquals
(
'admin'
,
$user
->
role
->
type
);
$this
->
assertEquals
(
'admin'
,
$user
->
role
->
type
);
// MongoDB beelongs to
$role
=
$user
->
role
()
->
first
();
// refetch
$role
=
$user
->
role
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$role
->
mysqlUser
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$role
->
mysqlUser
->
name
);
// belongsToMany DOES NOT WORK YET
// MongoDB User
/*$client = new Client(array('name' => 'Pork Pies Ltd.'));
$user
=
new
User
;
$user->clients()->save($client);
$user
->
name
=
"John Doe"
;
$user = MysqlUser::find($user->id); // refetch
$user
->
save
();
$this->assertEquals(1, count($user->clients));*/
// MongoDB has many
$book
=
new
MysqlBook
(
array
(
'title'
=>
'Game of Thrones'
));
$user
->
mysqlBooks
()
->
save
(
$book
);
$user
=
User
::
find
(
$user
->
_id
);
// refetch
$this
->
assertEquals
(
1
,
count
(
$user
->
mysqlBooks
));
// SQL belongs to
$book
=
$user
->
mysqlBooks
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$book
->
author
->
name
);
// MongoDB has one
$role
=
new
MysqlRole
(
array
(
'type'
=>
'admin'
));
$user
->
mysqlRole
()
->
save
(
$role
);
$user
=
User
::
find
(
$user
->
_id
);
// refetch
$this
->
assertEquals
(
'admin'
,
$user
->
mysqlRole
->
type
);
// SQL belongs to
$role
=
$user
->
mysqlRole
()
->
first
();
// refetch
$this
->
assertEquals
(
'John Doe'
,
$role
->
user
->
name
);
// Dirty again :)
// Dirty again :)
MysqlUser
::
truncate
();
MysqlUser
::
truncate
();
MysqlBook
::
truncate
();
MysqlRole
::
truncate
();
}
}
}
}
tests/models/Book.php
View file @
ac7174ae
...
@@ -5,9 +5,7 @@ use Jenssegers\Mongodb\Model as Eloquent;
...
@@ -5,9 +5,7 @@ use Jenssegers\Mongodb\Model as Eloquent;
class
Book
extends
Eloquent
{
class
Book
extends
Eloquent
{
protected
$collection
=
'books'
;
protected
$collection
=
'books'
;
protected
static
$unguarded
=
true
;
protected
static
$unguarded
=
true
;
protected
$primaryKey
=
'title'
;
protected
$primaryKey
=
'title'
;
public
function
author
()
public
function
author
()
...
...
tests/models/Client.php
View file @
ac7174ae
<?php
<?php
use
Jenssegers\Mongodb\Model
as
Eloquent
;
use
Jenssegers\Mongodb\Model
as
Eloquent
;
class
Client
extends
Eloquent
{
class
Client
extends
Eloquent
{
protected
$collection
=
'clients'
;
protected
$collection
=
'clients'
;
protected
static
$unguarded
=
true
;
protected
static
$unguarded
=
true
;
public
function
users
()
public
function
users
()
...
...
tests/models/Group.php
View file @
ac7174ae
<?php
<?php
use
Jenssegers\Mongodb\Model
as
Eloquent
;
use
Jenssegers\Mongodb\Model
as
Eloquent
;
class
Group
extends
Eloquent
{
class
Group
extends
Eloquent
{
protected
$collection
=
'groups'
;
protected
$collection
=
'groups'
;
protected
static
$unguarded
=
true
;
protected
static
$unguarded
=
true
;
public
function
users
()
public
function
users
()
...
...
tests/models/Item.php
View file @
ac7174ae
...
@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent;
...
@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent;
class
Item
extends
Eloquent
{
class
Item
extends
Eloquent
{
protected
$collection
=
'roles'
;
protected
$collection
=
'roles'
;
protected
static
$unguarded
=
true
;
protected
static
$unguarded
=
true
;
public
function
user
()
public
function
user
()
...
...
tests/models/MysqlBook.php
0 → 100644
View file @
ac7174ae
<?php
use
\Illuminate\Support\Facades\Schema
;
use
Jenssegers\Eloquent\Model
as
Eloquent
;
class
MysqlBook
extends
Eloquent
{
protected
$connection
=
'mysql'
;
protected
$table
=
'books'
;
protected
static
$unguarded
=
true
;
protected
$primaryKey
=
'title'
;
public
function
author
()
{
return
$this
->
belongsTo
(
'User'
,
'author_id'
);
}
/**
* Check if we need to run the schema
* @return [type] [description]
*/
public
static
function
executeSchema
()
{
$schema
=
Schema
::
connection
(
'mysql'
);
if
(
!
$schema
->
hasTable
(
'books'
))
{
Schema
::
connection
(
'mysql'
)
->
create
(
'books'
,
function
(
$table
)
{
$table
->
string
(
'title'
);
$table
->
string
(
'author_id'
);
$table
->
timestamps
();
});
}
}
}
tests/models/MysqlRole.php
0 → 100644
View file @
ac7174ae
<?php
use
\Illuminate\Support\Facades\Schema
;
use
Jenssegers\Eloquent\Model
as
Eloquent
;
class
MysqlRole
extends
Eloquent
{
protected
$connection
=
'mysql'
;
protected
$table
=
'roles'
;
protected
static
$unguarded
=
true
;
public
function
user
()
{
return
$this
->
belongsTo
(
'User'
);
}
public
function
mysqlUser
()
{
return
$this
->
belongsTo
(
'MysqlUser'
,
'role_id'
);
}
/**
* Check if we need to run the schema
* @return [type] [description]
*/
public
static
function
executeSchema
()
{
$schema
=
Schema
::
connection
(
'mysql'
);
if
(
!
$schema
->
hasTable
(
'roles'
))
{
Schema
::
connection
(
'mysql'
)
->
create
(
'roles'
,
function
(
$table
)
{
$table
->
string
(
'type'
);
$table
->
string
(
'user_id'
);
$table
->
timestamps
();
});
}
}
}
tests/models/MysqlUser.php
View file @
ac7174ae
...
@@ -14,26 +14,11 @@ class MysqlUser extends Eloquent {
...
@@ -14,26 +14,11 @@ class MysqlUser extends Eloquent {
return
$this
->
hasMany
(
'Book'
,
'author_id'
);
return
$this
->
hasMany
(
'Book'
,
'author_id'
);
}
}
public
function
items
()
{
return
$this
->
hasMany
(
'Item'
);
}
public
function
role
()
public
function
role
()
{
{
return
$this
->
hasOne
(
'Role'
,
'role_id'
);
return
$this
->
hasOne
(
'Role'
,
'role_id'
);
}
}
public
function
clients
()
{
return
$this
->
belongsToMany
(
'Client'
);
}
public
function
groups
()
{
return
$this
->
belongsToMany
(
'Group'
,
null
,
'users'
,
'groups'
);
}
/**
/**
* Check if we need to run the schema
* Check if we need to run the schema
* @return [type] [description]
* @return [type] [description]
...
@@ -51,22 +36,6 @@ class MysqlUser extends Eloquent {
...
@@ -51,22 +36,6 @@ class MysqlUser extends Eloquent {
$table
->
timestamps
();
$table
->
timestamps
();
});
});
}
}
/*if (!$schema->hasColumn('users', 'id'))
{
Schema::connection('mysql')->table('users', function($table)
{
$table->increments('id');
});
}
if (!$schema->hasColumn('users', 'name'))
{
Schema::connection('mysql')->table('users', function($table)
{
$table->string('name');
});
}*/
}
}
}
}
tests/models/Role.php
View file @
ac7174ae
...
@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent;
...
@@ -5,7 +5,6 @@ use Jenssegers\Mongodb\Model as Eloquent;
class
Role
extends
Eloquent
{
class
Role
extends
Eloquent
{
protected
$collection
=
'roles'
;
protected
$collection
=
'roles'
;
protected
static
$unguarded
=
true
;
protected
static
$unguarded
=
true
;
public
function
user
()
public
function
user
()
...
...
tests/models/User.php
View file @
ac7174ae
...
@@ -8,9 +8,7 @@ use Illuminate\Auth\Reminders\RemindableInterface;
...
@@ -8,9 +8,7 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class
User
extends
Eloquent
implements
UserInterface
,
RemindableInterface
{
class
User
extends
Eloquent
implements
UserInterface
,
RemindableInterface
{
protected
$collection
=
'users'
;
protected
$collection
=
'users'
;
protected
$dates
=
array
(
'birthday'
);
protected
$dates
=
array
(
'birthday'
);
protected
static
$unguarded
=
true
;
protected
static
$unguarded
=
true
;
public
function
books
()
public
function
books
()
...
@@ -18,6 +16,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
...
@@ -18,6 +16,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return
$this
->
hasMany
(
'Book'
,
'author_id'
);
return
$this
->
hasMany
(
'Book'
,
'author_id'
);
}
}
public
function
mysqlBooks
()
{
return
$this
->
hasMany
(
'MysqlBook'
,
'author_id'
);
}
public
function
items
()
public
function
items
()
{
{
return
$this
->
hasMany
(
'Item'
);
return
$this
->
hasMany
(
'Item'
);
...
@@ -28,6 +31,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
...
@@ -28,6 +31,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return
$this
->
hasOne
(
'Role'
);
return
$this
->
hasOne
(
'Role'
);
}
}
public
function
mysqlRole
()
{
return
$this
->
hasOne
(
'MysqlRole'
);
}
public
function
clients
()
public
function
clients
()
{
{
return
$this
->
belongsToMany
(
'Client'
);
return
$this
->
belongsToMany
(
'Client'
);
...
...
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