Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
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
mongo-php-library
Commits
3108162e
Commit
3108162e
authored
Apr 24, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-46, PHPLIB-63, PHPLIB-69: Functional tests for index methods
parent
4de66e39
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
163 additions
and
0 deletions
+163
-0
CollectionFunctionalTest.php
tests/CollectionFunctionalTest.php
+163
-0
No files found.
tests/CollectionFunctionalTest.php
View file @
3108162e
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests;
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests;
use
MongoDB\Collection
;
use
MongoDB\Collection
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Model\IndexInfo
;
use
InvalidArgumentException
;
class
CollectionFunctionalTest
extends
FunctionalTestCase
class
CollectionFunctionalTest
extends
FunctionalTestCase
{
{
...
@@ -56,4 +58,165 @@ class CollectionFunctionalTest extends FunctionalTestCase
...
@@ -56,4 +58,165 @@ class CollectionFunctionalTest extends FunctionalTestCase
}
}
$this
->
assertEquals
(
0
,
$n
);
$this
->
assertEquals
(
0
,
$n
);
}
}
public
function
testCreateIndex
()
{
$that
=
$this
;
$this
->
assertSame
(
'x_1'
,
$this
->
collection
->
createIndex
(
array
(
'x'
=>
1
),
array
(
'sparse'
=>
true
,
'unique'
=>
true
)));
$this
->
assertIndexExists
(
'x_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertTrue
(
$info
->
isSparse
());
$that
->
assertTrue
(
$info
->
isUnique
());
$that
->
assertFalse
(
$info
->
isTtl
());
});
$this
->
assertSame
(
'y_-1_z_1'
,
$this
->
collection
->
createIndex
(
array
(
'y'
=>
-
1
,
'z'
=>
1
)));
$this
->
assertIndexExists
(
'y_-1_z_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isSparse
());
$that
->
assertFalse
(
$info
->
isUnique
());
$that
->
assertFalse
(
$info
->
isTtl
());
});
$this
->
assertSame
(
'g_2dsphere_z_1'
,
$this
->
collection
->
createIndex
(
array
(
'g'
=>
'2dsphere'
,
'z'
=>
1
)));
$this
->
assertIndexExists
(
'g_2dsphere_z_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isSparse
());
$that
->
assertFalse
(
$info
->
isUnique
());
$that
->
assertFalse
(
$info
->
isTtl
());
});
$this
->
assertSame
(
't_1'
,
$this
->
collection
->
createIndex
(
array
(
't'
=>
1
),
array
(
'expireAfterSeconds'
=>
0
)));
$this
->
assertIndexExists
(
't_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isSparse
());
$that
->
assertFalse
(
$info
->
isUnique
());
$that
->
assertTrue
(
$info
->
isTtl
());
});
}
public
function
testCreateIndexes
()
{
$that
=
$this
;
$expectedNames
=
array
(
'x_1'
,
'y_-1_z_1'
,
'g_2dsphere_z_1'
,
't_1'
);
$indexes
=
array
(
array
(
'key'
=>
array
(
'x'
=>
1
),
'sparse'
=>
true
,
'unique'
=>
true
),
array
(
'key'
=>
array
(
'y'
=>
-
1
,
'z'
=>
1
)),
array
(
'key'
=>
array
(
'g'
=>
'2dsphere'
,
'z'
=>
1
)),
array
(
'key'
=>
array
(
't'
=>
1
),
'expireAfterSeconds'
=>
0
),
);
$this
->
assertSame
(
$expectedNames
,
$this
->
collection
->
createIndexes
(
$indexes
));
$this
->
assertIndexExists
(
'x_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertTrue
(
$info
->
isSparse
());
$that
->
assertTrue
(
$info
->
isUnique
());
$that
->
assertFalse
(
$info
->
isTtl
());
});
$this
->
assertIndexExists
(
'y_-1_z_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isSparse
());
$that
->
assertFalse
(
$info
->
isUnique
());
$that
->
assertFalse
(
$info
->
isTtl
());
});
$this
->
assertIndexExists
(
'g_2dsphere_z_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isSparse
());
$that
->
assertFalse
(
$info
->
isUnique
());
$that
->
assertFalse
(
$info
->
isTtl
());
});
$this
->
assertIndexExists
(
't_1'
,
function
(
IndexInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isSparse
());
$that
->
assertFalse
(
$info
->
isUnique
());
$that
->
assertTrue
(
$info
->
isTtl
());
});
}
public
function
testDropIndex
()
{
$this
->
assertSame
(
'x_1'
,
$this
->
collection
->
createIndex
(
array
(
'x'
=>
1
)));
$this
->
assertIndexExists
(
'x_1'
);
$this
->
assertCommandSucceeded
(
$this
->
collection
->
dropIndex
(
'x_1'
));
foreach
(
$this
->
collection
->
listIndexes
()
as
$index
)
{
if
(
$index
->
getName
()
===
'x_1'
)
{
$this
->
fail
(
'The "x_1" index should have been deleted'
);
}
}
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public
function
testDropIndexShouldNotAllowWildcardCharacter
()
{
$this
->
assertSame
(
'x_1'
,
$this
->
collection
->
createIndex
(
array
(
'x'
=>
1
)));
$this
->
assertIndexExists
(
'x_1'
);
$this
->
collection
->
dropIndex
(
'*'
);
}
public
function
testDropIndexes
()
{
$this
->
assertSame
(
'x_1'
,
$this
->
collection
->
createIndex
(
array
(
'x'
=>
1
)));
$this
->
assertSame
(
'y_1'
,
$this
->
collection
->
createIndex
(
array
(
'y'
=>
1
)));
$this
->
assertIndexExists
(
'x_1'
);
$this
->
assertIndexExists
(
'y_1'
);
$this
->
assertCommandSucceeded
(
$this
->
collection
->
dropIndexes
());
foreach
(
$this
->
collection
->
listIndexes
()
as
$index
)
{
if
(
$index
->
getName
()
===
'x_1'
)
{
$this
->
fail
(
'The "x_1" index should have been deleted'
);
}
if
(
$index
->
getName
()
===
'y_1'
)
{
$this
->
fail
(
'The "y_1" index should have been deleted'
);
}
}
}
public
function
testListIndexes
()
{
$this
->
assertSame
(
'x_1'
,
$this
->
collection
->
createIndex
(
array
(
'x'
=>
1
)));
$indexes
=
$this
->
collection
->
listIndexes
();
$this
->
assertInstanceOf
(
'MongoDB\Model\IndexInfoIterator'
,
$indexes
);
foreach
(
$indexes
as
$index
)
{
$this
->
assertInstanceOf
(
'MongoDB\Model\IndexInfo'
,
$index
);
}
}
/**
* Asserts that an index with the given name exists for the collection.
*
* An optional $callback may be provided, which should take an IndexInfo
* argument as its first and only parameter. If an IndexInfo matching the
* given name is found, it will be passed to the callback, which may perform
* additional assertions.
*
* @param callable $callback
*/
private
function
assertIndexExists
(
$indexName
,
$callback
=
null
)
{
if
(
$callback
!==
null
&&
!
is_callable
(
$callback
))
{
throw
new
InvalidArgumentException
(
'$callback is not a callable'
);
}
$indexes
=
$this
->
collection
->
listIndexes
();
$foundIndex
=
null
;
foreach
(
$indexes
as
$index
)
{
if
(
$index
->
getName
()
===
$indexName
)
{
$foundIndex
=
$index
;
break
;
}
}
$this
->
assertNotNull
(
$foundIndex
,
sprintf
(
'Found %s index for the collection'
,
$indexName
));
if
(
$callback
!==
null
)
{
call_user_func
(
$callback
,
$foundIndex
);
}
}
}
}
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