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
209b3013
Commit
209b3013
authored
Aug 22, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweaked tests and fixed pluck bug
parent
03cee24c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
535 additions
and
497 deletions
+535
-497
Builder.php
src/Jenssegers/Mongodb/Builder.php
+20
-0
CacheTest.php
tests/CacheTest.php
+8
-7
ModelQueryTest.php
tests/ModelQueryTest.php
+0
-328
QueryBuilderTest.php
tests/QueryBuilderTest.php
+320
-0
QueryTest.php
tests/QueryTest.php
+187
-162
No files found.
src/Jenssegers/Mongodb/Builder.php
View file @
209b3013
...
@@ -359,6 +359,26 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -359,6 +359,26 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
$this
->
increment
(
$column
,
-
1
*
$amount
,
$extra
);
return
$this
->
increment
(
$column
,
-
1
*
$amount
,
$extra
);
}
}
/**
* Pluck a single column from the database.
*
* @param string $column
* @return mixed
*/
public
function
pluck
(
$column
)
{
$result
=
(
array
)
$this
->
first
(
array
(
$column
));
// MongoDB returns the _id field even if you did not ask for it, so we need to
// remove this from the result.
if
(
array_key_exists
(
'_id'
,
$result
))
{
unset
(
$result
[
'_id'
]);
}
return
count
(
$result
)
>
0
?
reset
(
$result
)
:
null
;
}
/**
/**
* Delete a record from the database.
* Delete a record from the database.
*
*
...
...
tests/CacheTest.php
View file @
209b3013
...
@@ -5,9 +5,14 @@ use Illuminate\Support\Facades\DB;
...
@@ -5,9 +5,14 @@ use Illuminate\Support\Facades\DB;
class
CacheTest
extends
PHPUnit_Framework_TestCase
{
class
CacheTest
extends
PHPUnit_Framework_TestCase
{
protected
$cache
;
public
function
setUp
()
public
function
setUp
()
{
{
// test data
# clear cache
global
$app
;
$this
->
cache
=
$app
[
'cache'
];
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'age'
=>
35
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'age'
=>
35
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
33
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
33
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Harry Hoe'
,
'age'
=>
13
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Harry Hoe'
,
'age'
=>
13
,
'title'
=>
'user'
));
...
@@ -16,15 +21,11 @@ class CacheTest extends PHPUnit_Framework_TestCase {
...
@@ -16,15 +21,11 @@ class CacheTest extends PHPUnit_Framework_TestCase {
public
function
tearDown
()
public
function
tearDown
()
{
{
User
::
truncate
();
User
::
truncate
();
$this
->
cache
->
forget
(
'db.users'
);
}
}
public
function
testCache
()
public
function
testCache
()
{
{
# get from cache driver
global
$app
;
$cache
=
$app
[
'cache'
];
$cache
->
forget
(
'db.users'
);
# auto generate cache key
# auto generate cache key
$users
=
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'>'
,
10
)
->
remember
(
10
)
->
get
();
$users
=
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'>'
,
10
)
->
remember
(
10
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
$this
->
assertEquals
(
3
,
count
(
$users
));
...
@@ -36,7 +37,7 @@ class CacheTest extends PHPUnit_Framework_TestCase {
...
@@ -36,7 +37,7 @@ class CacheTest extends PHPUnit_Framework_TestCase {
$users
=
User
::
where
(
'age'
,
'>'
,
10
)
->
remember
(
10
,
'db.users'
)
->
get
();
$users
=
User
::
where
(
'age'
,
'>'
,
10
)
->
remember
(
10
,
'db.users'
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
$this
->
assertEquals
(
3
,
count
(
$users
));
$users
=
$cache
->
get
(
'db.users'
);
$users
=
$
this
->
cache
->
get
(
'db.users'
);
$this
->
assertEquals
(
3
,
count
(
$users
));
$this
->
assertEquals
(
3
,
count
(
$users
));
}
}
...
...
tests/ModelQueryTest.php
deleted
100644 → 0
View file @
03cee24c
<?php
require_once
(
'tests/app.php'
);
class
ModelQueryTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{
// test data
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'age'
=>
35
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
33
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Harry Hoe'
,
'age'
=>
13
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Robert Roe'
,
'age'
=>
37
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Mark Moe'
,
'age'
=>
23
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Brett Boe'
,
'age'
=>
35
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Tommy Toe'
,
'age'
=>
33
,
'title'
=>
'user'
));
User
::
create
(
array
(
'name'
=>
'Yvonne Yoe'
,
'age'
=>
35
,
'title'
=>
'admin'
));
User
::
create
(
array
(
'name'
=>
'Error'
,
'age'
=>
null
,
'title'
=>
null
));
}
public
function
tearDown
()
{
User
::
truncate
();
}
public
function
testGet
()
{
$users
=
User
::
get
();
$this
->
assertEquals
(
9
,
count
(
$users
));
$this
->
assertInstanceOf
(
'Jenssegers\Mongodb\Model'
,
$users
[
0
]);
}
public
function
testFirst
()
{
$user
=
User
::
get
()
->
first
();
$this
->
assertInstanceOf
(
'Jenssegers\Mongodb\Model'
,
$user
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
}
public
function
testWhere
()
{
$users
=
User
::
where
(
'age'
,
35
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
'='
,
35
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
'>='
,
35
)
->
get
();
$this
->
assertEquals
(
4
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
'<='
,
18
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
'!='
,
35
)
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
'<>'
,
35
)
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
}
public
function
testAndWhere
()
{
$users
=
User
::
where
(
'age'
,
35
)
->
where
(
'title'
,
'admin'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
'>='
,
35
)
->
where
(
'title'
,
'user'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
}
public
function
testLike
()
{
$users
=
User
::
where
(
'name'
,
'like'
,
'%doe'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$users
=
User
::
where
(
'name'
,
'like'
,
'%y%'
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
$users
=
User
::
where
(
'name'
,
'like'
,
't%'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
}
public
function
testPluck
()
{
$name
=
User
::
where
(
'name'
,
'John Doe'
)
->
pluck
(
'name'
);
$this
->
assertEquals
(
'John Doe'
,
$name
);
}
public
function
testList
()
{
$list
=
User
::
lists
(
'title'
);
$this
->
assertEquals
(
9
,
count
(
$list
));
$this
->
assertEquals
(
'admin'
,
$list
[
0
]);
$list
=
User
::
lists
(
'title'
,
'name'
);
$this
->
assertEquals
(
9
,
count
(
$list
));
$this
->
assertEquals
(
'John Doe'
,
key
(
$list
));
$this
->
assertEquals
(
'admin'
,
$list
[
'John Doe'
]);
}
public
function
testSelect
()
{
$user
=
User
::
select
(
'name'
)
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$user
=
User
::
select
(
'name'
,
'title'
)
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'admin'
,
$user
->
title
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$user
=
User
::
get
(
array
(
'name'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
null
,
$user
->
age
);
}
public
function
testOrWhere
()
{
$users
=
User
::
where
(
'age'
,
13
)
->
orWhere
(
'title'
,
'admin'
)
->
get
();
$this
->
assertEquals
(
4
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
13
)
->
orWhere
(
'age'
,
23
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
}
public
function
testBetween
()
{
$users
=
User
::
whereBetween
(
'age'
,
array
(
0
,
25
))
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$users
=
User
::
whereBetween
(
'age'
,
array
(
13
,
23
))
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
}
public
function
testIn
()
{
$users
=
User
::
whereIn
(
'age'
,
array
(
13
,
23
))
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$users
=
User
::
whereIn
(
'age'
,
array
(
33
,
35
,
13
))
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
$users
=
User
::
whereNotIn
(
'age'
,
array
(
33
,
35
))
->
get
();
$this
->
assertEquals
(
4
,
count
(
$users
));
$users
=
User
::
whereNotNull
(
'age'
)
->
whereNotIn
(
'age'
,
array
(
33
,
35
))
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
}
public
function
testWhereNull
()
{
$users
=
User
::
whereNull
(
'age'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
}
public
function
testWhereNotNull
()
{
$users
=
User
::
whereNotNull
(
'age'
)
->
get
();
$this
->
assertEquals
(
8
,
count
(
$users
));
}
public
function
testOrder
()
{
$user
=
User
::
whereNotNull
(
'age'
)
->
orderBy
(
'age'
,
'asc'
)
->
first
();
$this
->
assertEquals
(
13
,
$user
->
age
);
$user
=
User
::
whereNotNull
(
'age'
)
->
orderBy
(
'age'
,
'desc'
)
->
first
();
$this
->
assertEquals
(
37
,
$user
->
age
);
}
public
function
testTake
()
{
$users
=
User
::
take
(
3
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
}
public
function
testOffset
()
{
$users
=
User
::
skip
(
1
)
->
take
(
2
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$this
->
assertEquals
(
'Jane Doe'
,
$users
[
0
]
->
name
);
}
public
function
testIncrements
()
{
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
);
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
,
2
,
array
(
'title'
=>
'user'
));
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
38
,
$user
->
age
);
$this
->
assertEquals
(
'user'
,
$user
->
title
);
User
::
where
(
'name'
,
'John Doe'
)
->
decrement
(
'age'
);
$num
=
User
::
where
(
'name'
,
'John Doe'
)
->
decrement
(
'age'
,
2
,
array
(
'title'
=>
'admin'
));
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
35
,
$user
->
age
);
$this
->
assertEquals
(
'admin'
,
$user
->
title
);
$this
->
assertEquals
(
1
,
$num
);
User
::
increment
(
'age'
);
User
::
increment
(
'age'
,
2
);
$user
=
User
::
where
(
'name'
,
'Mark Moe'
)
->
first
();
$this
->
assertEquals
(
26
,
$user
->
age
);
User
::
decrement
(
'age'
,
2
);
$num
=
User
::
decrement
(
'age'
);
$user
=
User
::
where
(
'name'
,
'Mark Moe'
)
->
first
();
$this
->
assertEquals
(
23
,
$user
->
age
);
$this
->
assertEquals
(
8
,
$num
);
}
public
function
testAggregates
()
{
$this
->
assertEquals
(
9
,
User
::
count
());
$this
->
assertEquals
(
37
,
User
::
max
(
'age'
));
$this
->
assertEquals
(
13
,
User
::
min
(
'age'
));
$this
->
assertEquals
(
30.5
,
User
::
avg
(
'age'
));
$this
->
assertEquals
(
244
,
User
::
sum
(
'age'
));
$this
->
assertEquals
(
35
,
User
::
where
(
'title'
,
'admin'
)
->
max
(
'age'
));
$this
->
assertEquals
(
37
,
User
::
where
(
'title'
,
'user'
)
->
max
(
'age'
));
$this
->
assertEquals
(
33
,
User
::
where
(
'title'
,
'admin'
)
->
min
(
'age'
));
$this
->
assertEquals
(
13
,
User
::
where
(
'title'
,
'user'
)
->
min
(
'age'
));
}
public
function
testGroupBy
()
{
$users
=
User
::
groupBy
(
'title'
)
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
$users
=
User
::
groupBy
(
'age'
)
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
$users
=
User
::
groupBy
(
'age'
)
->
skip
(
1
)
->
get
();
$this
->
assertEquals
(
5
,
count
(
$users
));
$users
=
User
::
groupBy
(
'age'
)
->
take
(
2
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$users
=
User
::
groupBy
(
'age'
)
->
orderBy
(
'age'
,
'desc'
)
->
get
();
$this
->
assertEquals
(
37
,
$users
[
0
]
->
age
);
$this
->
assertEquals
(
35
,
$users
[
1
]
->
age
);
$this
->
assertEquals
(
33
,
$users
[
2
]
->
age
);
$users
=
User
::
groupBy
(
'age'
)
->
skip
(
1
)
->
take
(
2
)
->
orderBy
(
'age'
,
'desc'
)
->
get
();
$this
->
assertEquals
(
35
,
$users
[
0
]
->
age
);
$this
->
assertEquals
(
33
,
$users
[
1
]
->
age
);
}
public
function
testSubquery
()
{
$users
=
User
::
where
(
'title'
,
'admin'
)
->
orWhere
(
function
(
$query
)
{
$query
->
where
(
'name'
,
'Tommy Toe'
)
->
orWhere
(
'name'
,
'Error'
);
})
->
get
();
$this
->
assertEquals
(
5
,
count
(
$users
));
$users
=
User
::
where
(
'title'
,
'user'
)
->
where
(
function
(
$query
)
{
$query
->
where
(
'age'
,
35
)
->
orWhere
(
'name'
,
'like'
,
'%harry%'
);
})
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$users
=
User
::
where
(
'age'
,
35
)
->
orWhere
(
function
(
$query
)
{
$query
->
where
(
'title'
,
'admin'
)
->
orWhere
(
'name'
,
'Error'
);
})
->
get
();
$this
->
assertEquals
(
5
,
count
(
$users
));
}
public
function
testInsert
()
{
User
::
insert
(
array
(
'name'
=>
'Francois'
,
'age'
=>
59
,
'title'
=>
'Senior'
)
);
$this
->
assertEquals
(
10
,
User
::
count
());
User
::
insert
(
array
(
array
(
'name'
=>
'Gaston'
,
'age'
=>
60
,
'title'
=>
'Senior'
),
array
(
'name'
=>
'Jaques'
,
'age'
=>
61
,
'title'
=>
'Senior'
)
));
$this
->
assertEquals
(
12
,
User
::
count
());
}
public
function
testInsertGetId
()
{
$id
=
User
::
insertGetId
(
array
(
'name'
=>
'Gaston'
,
'age'
=>
60
,
'title'
=>
'Senior'
)
);
$this
->
assertEquals
(
10
,
User
::
count
());
$this
->
assertNotNull
(
$id
);
$this
->
assertTrue
(
is_string
(
$id
));
}
public
function
testRaw
()
{
$where
=
array
(
'age'
=>
array
(
'$gt'
=>
30
,
'$lt'
=>
40
));
$users
=
User
::
whereRaw
(
$where
)
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
$where1
=
array
(
'age'
=>
array
(
'$gt'
=>
30
,
'$lte'
=>
35
));
$where2
=
array
(
'age'
=>
array
(
'$gt'
=>
35
,
'$lt'
=>
40
));
$users
=
User
::
whereRaw
(
$where1
)
->
orWhereRaw
(
$where2
)
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
}
}
\ No newline at end of file
tests/QueryBuilderTest.php
0 → 100644
View file @
209b3013
<?php
require_once
(
'tests/app.php'
);
use
Illuminate\Support\Facades\DB
;
class
QueryBuilderTest
extends
PHPUnit_Framework_TestCase
{
public
function
setUp
()
{}
public
function
tearDown
()
{
DB
::
collection
(
'users'
)
->
truncate
();
DB
::
collection
(
'items'
)
->
truncate
();
}
public
function
testCollection
()
{
$this
->
assertInstanceOf
(
'Jenssegers\Mongodb\Builder'
,
DB
::
collection
(
'users'
));
}
public
function
testGet
()
{
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
0
,
count
(
$users
));
DB
::
collection
(
'users'
)
->
insert
(
array
(
'name'
=>
'John Doe'
));
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
}
public
function
testInsert
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
'tags'
=>
array
(
'tag1'
,
'tag2'
),
'name'
=>
'John Doe'
,
));
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
$user
=
$users
[
0
];
$this
->
assertEquals
(
'John Doe'
,
$user
[
'name'
]);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
}
public
function
testInsertGetId
()
{
$id
=
DB
::
collection
(
'users'
)
->
insertGetId
(
array
(
'name'
=>
'John Doe'
));
$this
->
assertTrue
(
is_string
(
$id
));
$this
->
assertEquals
(
24
,
strlen
(
$id
));
}
public
function
testBatchInsert
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'tags'
=>
array
(
'tag1'
,
'tag2'
),
'name'
=>
'Jane Doe'
,
),
array
(
'tags'
=>
array
(
'tag3'
),
'name'
=>
'John Doe'
,
),
));
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$users
));
$user
=
$users
[
0
];
$this
->
assertEquals
(
'Jane Doe'
,
$user
[
'name'
]);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
}
public
function
testFind
()
{
$id
=
DB
::
collection
(
'users'
)
->
insertGetId
(
array
(
'name'
=>
'John Doe'
));
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertEquals
(
'John Doe'
,
$user
[
'name'
]);
}
public
function
testCount
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'name'
=>
'Jane Doe'
),
array
(
'name'
=>
'John Doe'
)
));
$this
->
assertEquals
(
2
,
DB
::
collection
(
'users'
)
->
count
());
}
public
function
testUpdate
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
20
),
array
(
'name'
=>
'John Doe'
,
'age'
=>
21
)
));
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'John Doe'
)
->
update
(
array
(
'age'
=>
100
));
$users
=
DB
::
collection
(
'users'
)
->
get
();
$this
->
assertEquals
(
20
,
$users
[
0
][
'age'
]);
$this
->
assertEquals
(
100
,
$users
[
1
][
'age'
]);
}
public
function
testDelete
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
20
),
array
(
'name'
=>
'John Doe'
,
'age'
=>
25
)
));
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'<'
,
10
)
->
delete
();
$this
->
assertEquals
(
2
,
DB
::
collection
(
'users'
)
->
count
());
DB
::
collection
(
'users'
)
->
where
(
'age'
,
'<'
,
25
)
->
delete
();
$this
->
assertEquals
(
1
,
DB
::
collection
(
'users'
)
->
count
());
}
public
function
testTruncate
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
'name'
=>
'John Doe'
));
DB
::
collection
(
'users'
)
->
truncate
();
$this
->
assertEquals
(
0
,
DB
::
collection
(
'users'
)
->
count
());
}
public
function
testSubKey
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'name'
=>
'John Doe'
,
'address'
=>
array
(
'country'
=>
'Belgium'
,
'city'
=>
'Ghent'
)
),
array
(
'name'
=>
'Jane Doe'
,
'address'
=>
array
(
'country'
=>
'France'
,
'city'
=>
'Paris'
)
)
));
$users
=
DB
::
collection
(
'users'
)
->
where
(
'address.country'
,
'Belgium'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
$this
->
assertEquals
(
'John Doe'
,
$users
[
0
][
'name'
]);
}
public
function
testInArray
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'tags'
=>
array
(
'tag1'
,
'tag2'
,
'tag3'
,
'tag4'
)
),
array
(
'tags'
=>
array
(
'tag2'
)
)
));
$items
=
DB
::
collection
(
'items'
)
->
where
(
'tags'
,
'tag2'
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$items
));
$items
=
DB
::
collection
(
'items'
)
->
where
(
'tags'
,
'tag1'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$items
));
}
public
function
testRaw
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
20
),
array
(
'name'
=>
'John Doe'
,
'age'
=>
25
)
));
$cursor
=
DB
::
collection
(
'users'
)
->
raw
(
function
(
$collection
)
{
return
$collection
->
find
(
array
(
'age'
=>
20
));
});
$this
->
assertInstanceOf
(
'MongoCursor'
,
$cursor
);
$this
->
assertEquals
(
1
,
$cursor
->
count
());
$collection
=
DB
::
collection
(
'users'
)
->
raw
();
$this
->
assertInstanceOf
(
'MongoCollection'
,
$collection
);
}
public
function
testPush
()
{
$id
=
DB
::
collection
(
'users'
)
->
insertGetId
(
array
(
'name'
=>
'John Doe'
,
'tags'
=>
array
()
));
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
push
(
'tags'
,
'tag1'
);
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
1
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag1'
,
$user
[
'tags'
][
0
]);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
push
(
'tags'
,
'tag2'
);
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
2
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag2'
,
$user
[
'tags'
][
1
]);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
push
(
'tags'
,
array
(
'tag3'
,
'tag4'
));
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
4
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag4'
,
$user
[
'tags'
][
3
]);
}
public
function
testPull
()
{
$id
=
DB
::
collection
(
'users'
)
->
insertGetId
(
array
(
'name'
=>
'John Doe'
,
'tags'
=>
array
(
'tag1'
,
'tag2'
,
'tag3'
,
'tag4'
)
));
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
pull
(
'tags'
,
'tag3'
);
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
3
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag4'
,
$user
[
'tags'
][
2
]);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
pull
(
'tags'
,
array
(
'tag2'
,
'tag4'
));
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
1
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag1'
,
$user
[
'tags'
][
0
]);
}
public
function
testDistinct
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'name'
=>
'knife'
,
'type'
=>
'sharp'
,
'amount'
=>
34
),
array
(
'name'
=>
'fork'
,
'type'
=>
'sharp'
,
'amount'
=>
20
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
3
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
));
$items
=
DB
::
collection
(
'items'
)
->
distinct
(
'name'
)
->
get
();
$this
->
assertEquals
(
array
(
'knife'
,
'fork'
,
'spoon'
),
$items
);
$types
=
DB
::
collection
(
'items'
)
->
distinct
(
'type'
)
->
get
();
$this
->
assertEquals
(
array
(
'sharp'
,
'round'
),
$types
);
}
public
function
testCustomId
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'_id'
=>
'knife'
,
'type'
=>
'sharp'
,
'amount'
=>
34
),
array
(
'_id'
=>
'fork'
,
'type'
=>
'sharp'
,
'amount'
=>
20
),
array
(
'_id'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
3
)
));
$item
=
DB
::
collection
(
'items'
)
->
find
(
'knife'
);
$this
->
assertEquals
(
'knife'
,
$item
[
'_id'
]);
$item
=
DB
::
collection
(
'items'
)
->
where
(
'_id'
,
'fork'
)
->
first
();
$this
->
assertEquals
(
'fork'
,
$item
[
'_id'
]);
}
public
function
testTake
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'name'
=>
'knife'
,
'type'
=>
'sharp'
,
'amount'
=>
34
),
array
(
'name'
=>
'fork'
,
'type'
=>
'sharp'
,
'amount'
=>
20
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
3
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
));
$items
=
DB
::
collection
(
'items'
)
->
take
(
2
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertEquals
(
'knife'
,
$items
[
0
][
'name'
]);
}
public
function
testSkip
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'name'
=>
'knife'
,
'type'
=>
'sharp'
,
'amount'
=>
34
),
array
(
'name'
=>
'fork'
,
'type'
=>
'sharp'
,
'amount'
=>
20
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
3
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
));
$items
=
DB
::
collection
(
'items'
)
->
skip
(
2
)
->
get
();
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertEquals
(
'spoon'
,
$items
[
0
][
'name'
]);
}
public
function
testPluck
()
{
DB
::
collection
(
'users'
)
->
insert
(
array
(
array
(
'name'
=>
'Jane Doe'
,
'age'
=>
20
),
array
(
'name'
=>
'John Doe'
,
'age'
=>
25
)
));
$age
=
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'John Doe'
)
->
pluck
(
'age'
);
$this
->
assertEquals
(
25
,
$age
);
}
public
function
testList
()
{
DB
::
collection
(
'items'
)
->
insert
(
array
(
array
(
'name'
=>
'knife'
,
'type'
=>
'sharp'
,
'amount'
=>
34
),
array
(
'name'
=>
'fork'
,
'type'
=>
'sharp'
,
'amount'
=>
20
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
3
),
array
(
'name'
=>
'spoon'
,
'type'
=>
'round'
,
'amount'
=>
14
)
));
$list
=
DB
::
collection
(
'items'
)
->
lists
(
'name'
);
$this
->
assertEquals
(
array
(
'knife'
,
'fork'
,
'spoon'
,
'spoon'
),
$list
);
$list
=
DB
::
collection
(
'items'
)
->
lists
(
'type'
,
'name'
);
$this
->
assertEquals
(
array
(
'knife'
=>
'sharp'
,
'fork'
=>
'sharp'
,
'spoon'
=>
'round'
),
$list
);
}
}
\ No newline at end of file
tests/QueryTest.php
View file @
209b3013
This diff is collapsed.
Click to expand it.
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