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
a0acb263
Commit
a0acb263
authored
Aug 09, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code style tweaks and increasing test coverage
parent
f0b91050
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
125 additions
and
67 deletions
+125
-67
DatabaseReminderRepository.php
src/Jenssegers/Mongodb/Auth/DatabaseReminderRepository.php
+3
-8
Collection.php
src/Jenssegers/Mongodb/Collection.php
+15
-9
Builder.php
src/Jenssegers/Mongodb/Eloquent/Builder.php
+2
-0
Collection.php
src/Jenssegers/Mongodb/Eloquent/Collection.php
+6
-2
Model.php
src/Jenssegers/Mongodb/Model.php
+8
-14
Builder.php
src/Jenssegers/Mongodb/Query/Builder.php
+13
-8
EmbedsOne.php
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
+0
-17
Blueprint.php
src/Jenssegers/Mongodb/Schema/Blueprint.php
+14
-5
AuthTest.php
tests/AuthTest.php
+35
-0
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+29
-4
No files found.
src/Jenssegers/Mongodb/Auth/DatabaseReminderRepository.php
View file @
a0acb263
...
...
@@ -25,22 +25,17 @@ class DatabaseReminderRepository extends \Illuminate\Auth\Reminders\DatabaseRemi
*/
protected
function
reminderExpired
(
$reminder
)
{
// Convert to array so that we can pass it to the parent method
if
(
is_object
(
$reminder
))
{
$reminder
=
(
array
)
$reminder
;
}
// Convert MongoDate to a date string.
if
(
$reminder
[
'created_at'
]
instanceof
MongoDate
)
{
$date
=
new
DateTime
();
$date
=
new
DateTime
;
$date
->
setTimestamp
(
$reminder
[
'created_at'
]
->
sec
);
$reminder
[
'created_at'
]
=
$date
->
format
(
'Y-m-d H:i:s'
);
}
//
DEPRECATED: Convert DateTime to a date string
.
//
Convert DateTime to a date string (backwards compatibility)
.
elseif
(
is_array
(
$reminder
[
'created_at'
]))
{
$date
=
DateTime
::
__set_state
(
$reminder
[
'created_at'
]);
...
...
src/Jenssegers/Mongodb/Collection.php
View file @
a0acb263
...
...
@@ -26,6 +26,7 @@ class Collection {
public
function
__construct
(
Connection
$connection
,
MongoCollection
$collection
)
{
$this
->
connection
=
$connection
;
$this
->
collection
=
$collection
;
}
...
...
@@ -38,29 +39,34 @@ class Collection {
*/
public
function
__call
(
$method
,
$parameters
)
{
$query
=
array
();
// Build the query string.
$query
=
$parameters
;
foreach
(
$query
as
&
$param
)
foreach
(
$parameters
as
$parameter
)
{
try
{
$
param
=
json_encode
(
$param
);
$
query
[]
=
json_encode
(
$parameter
);
}
catch
(
Exception
$e
)
{
$
param
=
'{...}'
;
$
query
[]
=
'{...}'
;
}
}
$start
=
microtime
(
true
);
// Execute the query.
$result
=
call_user_func_array
(
array
(
$this
->
collection
,
$method
),
$parameters
);
// Log the query.
$this
->
connection
->
logQuery
(
$this
->
collection
->
getName
()
.
'.'
.
$method
.
'('
.
join
(
','
,
$query
)
.
')'
,
array
(),
$this
->
connection
->
getElapsedTime
(
$start
));
// Once we have run the query we will calculate the time that it took to run and
// then log the query, bindings, and execution time so we will report them on
// the event that the developer needs them. We'll log time in milliseconds.
$time
=
$this
->
connection
->
getElapsedTime
(
$start
);
// Convert the query to a readable string.
$queryString
=
$this
->
collection
->
getName
()
.
'.'
.
$method
.
'('
.
join
(
','
,
$query
)
.
')'
;
$this
->
connection
->
logQuery
(
$queryString
,
array
(),
$time
);
return
$result
;
}
...
...
src/Jenssegers/Mongodb/Eloquent/Builder.php
View file @
a0acb263
...
...
@@ -116,6 +116,7 @@ class Builder extends EloquentBuilder {
// sync the original attributes. We need to change the attribute
// temporary in order to trigger an update query.
$this
->
model
->
{
$column
}
=
null
;
$this
->
model
->
syncOriginalAttribute
(
$column
);
$result
=
$this
->
model
->
update
(
array
(
$column
=>
$value
));
...
...
@@ -146,6 +147,7 @@ class Builder extends EloquentBuilder {
// sync the original attributes. We need to change the attribute
// temporary in order to trigger an update query.
$this
->
model
->
{
$column
}
=
null
;
$this
->
model
->
syncOriginalAttribute
(
$column
);
return
$this
->
model
->
update
(
array
(
$column
=>
$value
));
...
...
src/Jenssegers/Mongodb/Eloquent/Collection.php
View file @
a0acb263
<?php
namespace
Jenssegers\Mongodb\Eloquent
;
use
Illuminate\Database\Eloquent\Collection
as
Base
Collection
;
use
Illuminate\Database\Eloquent\Collection
as
Eloquent
Collection
;
class
Collection
extends
Base
Collection
{
class
Collection
extends
Eloquent
Collection
{
/**
* Simulate a get clause on the collection.
...
...
@@ -63,6 +63,10 @@ class Collection extends BaseCollection {
return
$actual
>=
$value
;
break
;
case
'<='
:
return
$actual
<=
$value
;
break
;
case
'between'
:
return
$actual
>=
$value
[
0
]
and
$actual
<=
$value
[
1
];
break
;
...
...
src/Jenssegers/Mongodb/Model.php
View file @
a0acb263
...
...
@@ -269,12 +269,8 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
protected
function
getAttributeFromArray
(
$key
)
{
if
(
array_key_exists
(
$key
,
$this
->
attributes
))
{
return
$this
->
attributes
[
$key
];
}
elseif
(
str_contains
(
$key
,
'.'
))
// Support keys in dot notation.
if
(
str_contains
(
$key
,
'.'
))
{
$attributes
=
array_dot
(
$this
->
attributes
);
...
...
@@ -283,6 +279,8 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return
$attributes
[
$key
];
}
}
return
parent
::
getAttributeFromArray
(
$key
);
}
/**
...
...
@@ -380,10 +378,8 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
list
(
$column
,
$values
)
=
$parameters
;
}
if
(
!
is_array
(
$values
))
{
$values
=
array
(
$values
);
}
// Do batch push by default.
if
(
!
is_array
(
$values
))
$values
=
array
(
$values
);
$query
=
$this
->
setKeysForSaveQuery
(
$this
->
newQuery
());
...
...
@@ -402,10 +398,8 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
public
function
pull
(
$column
,
$values
)
{
if
(
!
is_array
(
$values
))
{
$values
=
array
(
$values
);
}
// Do batch pull by default.
if
(
!
is_array
(
$values
))
$values
=
array
(
$values
);
$query
=
$this
->
setKeysForSaveQuery
(
$this
->
newQuery
());
...
...
src/Jenssegers/Mongodb/Query/Builder.php
View file @
a0acb263
...
...
@@ -6,10 +6,11 @@ use MongoDate;
use
DateTime
;
use
Closure
;
use
Illuminate\Database\Query\Builder
as
QueryBuilder
;
use
Illuminate\Database\Query\Expression
;
use
Jenssegers\Mongodb\Connection
;
class
Builder
extends
\Illuminate\Database\Query\
Builder
{
class
Builder
extends
Query
Builder
{
/**
* The database collection
...
...
@@ -193,6 +194,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
foreach
(
$this
->
columns
as
$column
)
{
$key
=
str_replace
(
'.'
,
'_'
,
$column
);
$group
[
$key
]
=
array
(
'$last'
=>
'$'
.
$column
);
}
}
...
...
@@ -374,17 +376,18 @@ class Builder extends \Illuminate\Database\Query\Builder {
// Since every insert gets treated like a batch insert, we will have to detect
// if the user is inserting a single document or an array of documents.
$batch
=
true
;
foreach
(
$values
as
$value
)
{
// As soon as we find a value that is not an array we assume the user is
// inserting a single document.
if
(
!
is_array
(
$value
))
if
(
!
is_array
(
$value
))
{
$batch
=
false
;
break
;
}
}
if
(
!
$batch
)
$values
=
array
(
$values
);
if
(
!
$batch
)
$values
=
array
(
$values
);
// Batch insert
$result
=
$this
->
collection
->
batchInsert
(
$values
);
...
...
@@ -405,7 +408,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
if
(
1
==
(
int
)
$result
[
'ok'
])
{
if
(
!
$sequence
)
if
(
is_null
(
$sequence
)
)
{
$sequence
=
'_id'
;
}
...
...
@@ -454,6 +457,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
$this
->
where
(
function
(
$query
)
use
(
$column
)
{
$query
->
where
(
$column
,
'exists'
,
false
);
$query
->
orWhereNotNull
(
$column
);
});
...
...
@@ -502,6 +506,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
public
function
delete
(
$id
=
null
)
{
$wheres
=
$this
->
compileWheres
();
$result
=
$this
->
collection
->
remove
(
$wheres
);
if
(
1
==
(
int
)
$result
[
'ok'
])
...
...
@@ -577,13 +582,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
$operator
=
$unique
?
'$addToSet'
:
'$push'
;
// Check if we are pushing multiple values.
$
multipleValues
=
(
is_array
(
$value
)
and
array_keys
(
$value
)
===
range
(
0
,
count
(
$value
)
-
1
));
$
batch
=
(
is_array
(
$value
)
and
array_keys
(
$value
)
===
range
(
0
,
count
(
$value
)
-
1
));
if
(
is_array
(
$column
))
{
$query
=
array
(
$operator
=>
$column
);
}
else
if
(
$
multipleValues
)
else
if
(
$
batch
)
{
$query
=
array
(
$operator
=>
array
(
$column
=>
array
(
'$each'
=>
$value
)));
}
...
...
@@ -605,10 +610,10 @@ class Builder extends \Illuminate\Database\Query\Builder {
public
function
pull
(
$column
,
$value
=
null
)
{
// Check if we passed an associative array.
$
multipleValues
=
(
is_array
(
$value
)
and
array_keys
(
$value
)
===
range
(
0
,
count
(
$value
)
-
1
));
$
batch
=
(
is_array
(
$value
)
and
array_keys
(
$value
)
===
range
(
0
,
count
(
$value
)
-
1
));
// If we are pulling multiple values, we need to use $pullAll.
$operator
=
$
multipleValues
?
'$pullAll'
:
'$pull'
;
$operator
=
$
batch
?
'$pullAll'
:
'$pull'
;
if
(
is_array
(
$column
))
{
...
...
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
View file @
a0acb263
...
...
@@ -131,21 +131,4 @@ class EmbedsOne extends EmbedsOneOrMany {
return
$this
->
performDelete
(
$model
);
}
/**
* Check if a model is already embedded.
*
* @param mixed $key
* @return bool
*/
public
function
contains
(
$key
)
{
if
(
$key
instanceof
Model
)
$key
=
$key
->
getKey
();
$embedded
=
$this
->
getEmbedded
();
$primaryKey
=
$this
->
related
->
getKeyName
();
return
(
$embedded
and
$embedded
[
$primaryKey
]
==
$key
);
}
}
src/Jenssegers/Mongodb/Schema/Blueprint.php
View file @
a0acb263
...
...
@@ -36,6 +36,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
public
function
__construct
(
Connection
$connection
,
$collection
)
{
$this
->
connection
=
$connection
;
$this
->
collection
=
$connection
->
getCollection
(
$collection
);
}
...
...
@@ -50,11 +51,12 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
{
$columns
=
$this
->
fluent
(
$columns
);
// Columns are passed as a default array
// Columns are passed as a default array
.
if
(
is_array
(
$columns
)
&&
is_int
(
key
(
$columns
)))
{
// Transform the columns to the required array format
// Transform the columns to the required array format
.
$transform
=
array
();
foreach
(
$columns
as
$column
)
{
$transform
[
$column
]
=
1
;
...
...
@@ -78,11 +80,12 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
{
$columns
=
$this
->
fluent
(
$columns
);
// Columns are passed as a default array
// Columns are passed as a default array
.
if
(
is_array
(
$columns
)
&&
is_int
(
key
(
$columns
)))
{
// Transform the columns to the required array format
// Transform the columns to the required array format
.
$transform
=
array
();
foreach
(
$columns
as
$column
)
{
$transform
[
$column
]
=
1
;
...
...
@@ -105,6 +108,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
public
function
unique
(
$columns
=
null
,
$name
=
null
)
{
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'unique'
=>
true
));
return
$this
;
...
...
@@ -119,6 +123,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
public
function
background
(
$columns
=
null
)
{
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'background'
=>
true
));
return
$this
;
...
...
@@ -149,6 +154,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
public
function
expire
(
$columns
,
$seconds
)
{
$columns
=
$this
->
fluent
(
$columns
);
$this
->
index
(
$columns
,
array
(
'expireAfterSeconds'
=>
$seconds
));
return
$this
;
...
...
@@ -163,8 +169,9 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
{
$collection
=
$this
->
collection
->
getName
();
// Ensure the collection is created
$db
=
$this
->
connection
->
getMongoDB
();
// Ensure the collection is created.
$db
->
createCollection
(
$collection
);
}
...
...
@@ -189,6 +196,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
protected
function
addColumn
(
$type
,
$name
,
array
$parameters
=
array
())
{
$this
->
fluent
(
$name
);
return
$this
;
}
...
...
@@ -221,6 +229,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
*/
public
function
__call
(
$method
,
$args
)
{
// Dummy.
return
$this
;
}
...
...
tests/AuthTest.php
View file @
a0acb263
...
...
@@ -57,4 +57,39 @@ class AuthTest extends TestCase {
$this
->
assertEquals
(
0
,
DB
::
collection
(
'password_reminders'
)
->
count
());
}
public
function
testDeprecatedRemind
()
{
$mailer
=
Mockery
::
mock
(
'Illuminate\Mail\Mailer'
);
$this
->
app
->
instance
(
'mailer'
,
$mailer
);
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'email'
=>
'john@doe.com'
,
'password'
=>
Hash
::
make
(
'foobar'
)
));
$mailer
->
shouldReceive
(
'send'
)
->
once
();
Password
::
remind
(
array
(
'email'
=>
'john@doe.com'
));
DB
::
collection
(
'password_reminders'
)
->
update
(
array
(
'created_at'
=>
new
DateTime
));
$reminder
=
DB
::
collection
(
'password_reminders'
)
->
first
();
$this
->
assertTrue
(
is_array
(
$reminder
[
'created_at'
]));
$credentials
=
array
(
'email'
=>
'john@doe.com'
,
'password'
=>
'foobar'
,
'password_confirmation'
=>
'foobar'
,
'token'
=>
$reminder
[
'token'
]
);
$response
=
Password
::
reset
(
$credentials
,
function
(
$user
,
$password
)
{
$user
->
password
=
Hash
::
make
(
$password
);
$user
->
save
();
});
$this
->
assertEquals
(
'reminders.reset'
,
$response
);
$this
->
assertEquals
(
0
,
DB
::
collection
(
'password_reminders'
)
->
count
());
}
}
tests/EmbeddedRelationsTest.php
View file @
a0acb263
...
...
@@ -416,10 +416,10 @@ class EmbeddedRelationsTest extends TestCase {
public
function
testEmbedsManyCollectionMethods
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
,
'country'
=>
'France'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Bruges'
,
'country'
=>
'Belgium'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Brussels'
,
'country'
=>
'Belgium'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Ghent'
,
'country'
=>
'Belgium'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
,
'country'
=>
'France'
,
'visited'
=>
4
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Bruges'
,
'country'
=>
'Belgium'
,
'visited'
=>
7
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Brussels'
,
'country'
=>
'Belgium'
,
'visited'
=>
2
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Ghent'
,
'country'
=>
'Belgium'
,
'visited'
=>
13
)));
$this
->
assertEquals
(
array
(
'Paris'
,
'Bruges'
,
'Brussels'
,
'Ghent'
),
$user
->
addresses
()
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruges'
,
'Brussels'
,
'Ghent'
,
'Paris'
),
$user
->
addresses
()
->
sortBy
(
'city'
)
->
lists
(
'city'
));
...
...
@@ -433,6 +433,24 @@ class EmbeddedRelationsTest extends TestCase {
$results
=
$user
->
addresses
()
->
where
(
'country'
,
'Belgium'
)
->
get
();
$this
->
assertInstanceOf
(
'Jenssegers\Mongodb\Eloquent\Collection'
,
$results
);
$this
->
assertEquals
(
3
,
$results
->
count
());
$results
=
$user
->
addresses
()
->
where
(
'country'
,
'!='
,
'Belgium'
)
->
get
();
$this
->
assertEquals
(
1
,
$results
->
count
());
$results
=
$user
->
addresses
()
->
where
(
'visited'
,
'>'
,
4
)
->
get
();
$this
->
assertEquals
(
2
,
$results
->
count
());
$results
=
$user
->
addresses
()
->
where
(
'visited'
,
'<'
,
7
)
->
get
();
$this
->
assertEquals
(
2
,
$results
->
count
());
$results
=
$user
->
addresses
()
->
where
(
'visited'
,
'<='
,
7
)
->
get
();
$this
->
assertEquals
(
3
,
$results
->
count
());
$results
=
$user
->
addresses
()
->
where
(
'visited'
,
'>='
,
7
)
->
get
();
$this
->
assertEquals
(
2
,
$results
->
count
());
$results
=
$user
->
addresses
()
->
where
(
'visited'
,
'between'
,
array
(
4
,
7
))
->
get
();
$this
->
assertEquals
(
2
,
$results
->
count
());
}
public
function
testEmbedsOne
()
...
...
@@ -671,6 +689,13 @@ class EmbeddedRelationsTest extends TestCase {
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
6
,
$user
->
addresses
()
->
first
()
->
visited
);
$address
->
decrement
(
'visited'
);
$this
->
assertEquals
(
5
,
$address
->
visited
);
//$this->assertEquals(5, $user->addresses()->first()->visited); TODO
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
5
,
$user
->
addresses
()
->
first
()
->
visited
);
}
public
function
testPaginateEmbedsMany
()
...
...
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