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
fde9d8ed
Commit
fde9d8ed
authored
Apr 07, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-64: Collection creation method
parent
0ffbae83
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
3 deletions
+63
-3
Database.php
src/Database.php
+5
-1
DatabaseFunctionalTest.php
tests/DatabaseFunctionalTest.php
+58
-2
No files found.
src/Database.php
View file @
fde9d8ed
...
...
@@ -51,7 +51,11 @@ class Database
*/
public
function
createCollection
(
$collectionName
,
array
$options
=
array
())
{
// TODO
$collectionName
=
(
string
)
$collectionName
;
$command
=
new
Command
(
array
(
'create'
=>
$collectionName
)
+
$options
);
$readPreference
=
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
);
return
$this
->
manager
->
executeCommand
(
$this
->
databaseName
,
$command
,
$readPreference
);
}
/**
...
...
tests/DatabaseFunctionalTest.php
View file @
fde9d8ed
...
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests;
use
MongoDB\Client
;
use
MongoDB\Database
;
use
MongoDB\Model\CollectionInfo
;
use
InvalidArgumentException
;
/**
* Functional tests for the Database class.
...
...
@@ -20,6 +22,33 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this
->
database
->
drop
();
}
public
function
testCreateCollection
()
{
$that
=
$this
;
$basicCollectionName
=
$this
->
getCollectionName
()
.
'.basic'
;
$commandResult
=
$this
->
database
->
createCollection
(
$basicCollectionName
);
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertCollectionExists
(
$basicCollectionName
,
function
(
CollectionInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isCapped
());
});
$cappedCollectionName
=
$this
->
getCollectionName
()
.
'.capped'
;
$cappedCollectionOptions
=
array
(
'capped'
=>
true
,
'max'
=>
100
,
'size'
=>
1048576
,
);
$commandResult
=
$this
->
database
->
createCollection
(
$cappedCollectionName
,
$cappedCollectionOptions
);
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertCollectionExists
(
$cappedCollectionName
,
function
(
CollectionInfo
$info
)
use
(
$that
)
{
$that
->
assertTrue
(
$info
->
isCapped
());
$that
->
assertEquals
(
100
,
$info
->
getCappedMax
());
$that
->
assertEquals
(
1048576
,
$info
->
getCappedSize
());
});
}
public
function
testDrop
()
{
$writeResult
=
$this
->
manager
->
executeInsert
(
$this
->
getNamespace
(),
array
(
'x'
=>
1
));
...
...
@@ -48,15 +77,42 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$collections
=
$this
->
database
->
listCollections
();
$this
->
assertInstanceOf
(
'MongoDB\Model\CollectionInfoIterator'
,
$collections
);
foreach
(
$collections
as
$collection
)
{
$this
->
assertInstanceOf
(
'MongoDB\Model\CollectionInfo'
,
$collection
);
}
}
/**
* Asserts that a collection with the given name exists in the database.
*
* An optional $callback may be provided, which should take a CollectionInfo
* argument as its first and only parameter. If a CollectionInfo matching
* the given name is found, it will be passed to the callback, which may
* perform additional assertions.
*
* @param callable $callback
*/
private
function
assertCollectionExists
(
$collectionName
,
$callback
=
null
)
{
if
(
$callback
!==
null
&&
!
is_callable
(
$callback
))
{
throw
new
InvalidArgumentException
(
'$callback is not a callable'
);
}
$collections
=
$this
->
database
->
listCollections
();
$foundCollection
=
null
;
foreach
(
$collections
as
$collection
)
{
if
(
$collection
->
getName
()
===
$
this
->
getCollectionName
()
)
{
if
(
$collection
->
getName
()
===
$
collectionName
)
{
$foundCollection
=
$collection
;
break
;
}
}
$this
->
assertNotNull
(
$foundCollection
,
'Found test collection in list of collection'
);
$this
->
assertNotNull
(
$foundCollection
,
sprintf
(
'Found %s collection in the database'
,
$collectionName
));
if
(
$callback
!==
null
)
{
call_user_func
(
$callback
,
$foundCollection
);
}
}
}
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