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
797ef704
Commit
797ef704
authored
Nov 18, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #49
parents
ce26ec3c
c50d9a6d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
362 additions
and
11 deletions
+362
-11
CreateIndexes.php
src/Operation/CreateIndexes.php
+2
-1
CollectionFunctionalTest.php
tests/Collection/CollectionFunctionalTest.php
+9
-0
CreateIndexesFunctionalTest.php
tests/Operation/CreateIndexesFunctionalTest.php
+194
-0
CreateIndexesTest.php
tests/Operation/CreateIndexesTest.php
+30
-0
DropIndexesFunctionalTest.php
tests/Operation/DropIndexesFunctionalTest.php
+102
-0
DropIndexesTest.php
tests/Operation/DropIndexesTest.php
+16
-0
ListIndexesFunctionalTest.php
tests/Operation/ListIndexesFunctionalTest.php
+9
-10
No files found.
src/Operation/CreateIndexes.php
View file @
797ef704
...
@@ -5,6 +5,7 @@ namespace MongoDB\Operation;
...
@@ -5,6 +5,7 @@ namespace MongoDB\Operation;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\BulkWrite
as
Bulk
;
use
MongoDB\Driver\BulkWrite
as
Bulk
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\RuntimeException
;
use
MongoDB\Exception\RuntimeException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Exception\UnexpectedTypeException
;
...
@@ -113,6 +114,6 @@ class CreateIndexes implements Executable
...
@@ -113,6 +114,6 @@ class CreateIndexes implements Executable
$bulk
->
insert
(
$index
);
$bulk
->
insert
(
$index
);
}
}
$server
->
executeBulkWrite
(
$this
->
databaseName
.
'.system.indexes'
,
$bulk
);
$server
->
executeBulkWrite
(
$this
->
databaseName
.
'.system.indexes'
,
$bulk
,
new
WriteConcern
(
1
)
);
}
}
}
}
tests/Collection/CollectionFunctionalTest.php
View file @
797ef704
...
@@ -61,6 +61,15 @@ class CollectionFunctionalTest extends FunctionalTestCase
...
@@ -61,6 +61,15 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this
->
assertCollectionCount
(
$this
->
getNamespace
(),
0
);
$this
->
assertCollectionCount
(
$this
->
getNamespace
(),
0
);
}
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @todo Move this to a unit test once Manager can be mocked
*/
public
function
testDropIndexShouldNotAllowWildcardCharacter
()
{
$this
->
collection
->
dropIndex
(
'*'
);
}
public
function
testFindOne
()
public
function
testFindOne
()
{
{
$this
->
createFixtures
(
5
);
$this
->
createFixtures
(
5
);
...
...
tests/
Collection/IndexManagement
FunctionalTest.php
→
tests/
Operation/CreateIndexes
FunctionalTest.php
View file @
797ef704
This diff is collapsed.
Click to expand it.
tests/Operation/CreateIndexesTest.php
0 → 100644
View file @
797ef704
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Operation\CreateIndexes
;
class
CreateIndexesTest
extends
TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public
function
testCreateIndexesRequiresAtLeastOneIndex
()
{
new
CreateIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[]);
}
/**
* @expectedException MongoDB\Exception\UnexpectedTypeException
* @dataProvider provideInvalidIndexSpecificationTypes
*/
public
function
testCreateIndexesRequiresIndexSpecificationsToBeAnArray
(
$index
)
{
new
CreateIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
$index
]);
}
public
function
provideInvalidIndexSpecificationTypes
()
{
return
$this
->
wrapValuesForDataProvider
(
$this
->
getInvalidArrayValues
());
}
}
tests/Operation/DropIndexesFunctionalTest.php
0 → 100644
View file @
797ef704
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Model\IndexInfo
;
use
MongoDB\Operation\CreateIndexes
;
use
MongoDB\Operation\DropIndexes
;
use
MongoDB\Operation\ListIndexes
;
use
InvalidArgumentException
;
class
DropIndexesFunctionalTest
extends
FunctionalTestCase
{
public
function
testDropOneIndexByName
()
{
$indexes
=
[[
'key'
=>
[
'x'
=>
1
]]];
$operation
=
new
CreateIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$indexes
);
$createdIndexNames
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertSame
(
'x_1'
,
$createdIndexNames
[
0
]);
$this
->
assertIndexExists
(
'x_1'
);
$operation
=
new
DropIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
'x_1'
);
$this
->
assertCommandSucceeded
(
$operation
->
execute
(
$this
->
getPrimaryServer
()));
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$indexes
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
foreach
(
$indexes
as
$index
)
{
if
(
$index
->
getName
()
===
'x_1'
)
{
$this
->
fail
(
'The "x_1" index should have been deleted'
);
}
}
}
public
function
testDropAllIndexesByWildcard
()
{
$indexes
=
[
[
'key'
=>
[
'x'
=>
1
]],
[
'key'
=>
[
'y'
=>
1
]],
];
$operation
=
new
CreateIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$indexes
);
$createdIndexNames
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertSame
(
'x_1'
,
$createdIndexNames
[
0
]);
$this
->
assertSame
(
'y_1'
,
$createdIndexNames
[
1
]);
$this
->
assertIndexExists
(
'x_1'
);
$this
->
assertIndexExists
(
'y_1'
);
$operation
=
new
DropIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
'*'
);
$this
->
assertCommandSucceeded
(
$operation
->
execute
(
$this
->
getPrimaryServer
()));
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$indexes
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
foreach
(
$indexes
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'
);
}
}
}
/**
* 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'
);
}
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$indexes
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$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
);
}
}
}
tests/Operation/DropIndexesTest.php
0 → 100644
View file @
797ef704
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Operation\DropIndexes
;
class
DropIndexesTest
extends
TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
*/
public
function
testDropIndexShouldNotAllowEmptyIndexName
()
{
new
DropIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
''
);
}
}
tests/Operation/ListIndexesFunctionalTest.php
View file @
797ef704
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
namespace
MongoDB\Tests\Operation
;
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Driver\Server
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\InsertOne
;
use
MongoDB\Operation\InsertOne
;
use
MongoDB\Operation\ListIndexes
;
use
MongoDB\Operation\ListIndexes
;
...
@@ -11,18 +10,20 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
...
@@ -11,18 +10,20 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
{
{
public
function
testListIndexesForNewlyCreatedCollection
()
public
function
testListIndexesForNewlyCreatedCollection
()
{
{
$server
=
$this
->
getPrimaryServer
();
$operation
=
new
DropCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
=
new
DropCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
->
execute
(
$
server
);
$operation
->
execute
(
$
this
->
getPrimaryServer
()
);
$insertOne
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
]);
$insertOne
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
]);
$writeResult
=
$insertOne
->
execute
(
$
server
);
$writeResult
=
$insertOne
->
execute
(
$
this
->
getPrimaryServer
()
);
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$indexes
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertInstanceOf
(
'MongoDB\Model\IndexInfoIterator'
,
$indexes
);
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
$indexes
=
iterator_to_array
(
$
operation
->
execute
(
$server
)
);
$indexes
=
iterator_to_array
(
$
indexes
);
$this
->
assertCount
(
1
,
$indexes
);
$this
->
assertCount
(
1
,
$indexes
);
...
@@ -34,13 +35,11 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
...
@@ -34,13 +35,11 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
public
function
testListIndexesForNonexistentCollection
()
public
function
testListIndexesForNonexistentCollection
()
{
{
$server
=
$this
->
getPrimaryServer
();
$operation
=
new
DropCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
=
new
DropCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
->
execute
(
$
server
);
$operation
->
execute
(
$
this
->
getPrimaryServer
()
);
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$indexes
=
$operation
->
execute
(
$
server
);
$indexes
=
$operation
->
execute
(
$
this
->
getPrimaryServer
()
);
$this
->
assertCount
(
0
,
$indexes
);
$this
->
assertCount
(
0
,
$indexes
);
}
}
...
...
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