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
57e05c13
Commit
57e05c13
authored
Nov 24, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #51
parents
be188e3e
52b81e99
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
437 additions
and
59 deletions
+437
-59
Client.php
src/Client.php
+41
-22
Collection.php
src/Collection.php
+73
-9
Database.php
src/Database.php
+91
-18
ClientTest.php
tests/ClientTest.php
+70
-0
CollectionFunctionalTest.php
tests/Collection/CollectionFunctionalTest.php
+59
-0
DatabaseFunctionalTest.php
tests/Database/DatabaseFunctionalTest.php
+92
-0
TestCase.php
tests/Operation/TestCase.php
+0
-10
TestCase.php
tests/TestCase.php
+11
-0
No files found.
src/Client.php
View file @
57e05c13
...
@@ -34,6 +34,20 @@ class Client
...
@@ -34,6 +34,20 @@ class Client
$this
->
uri
=
(
string
)
$uri
;
$this
->
uri
=
(
string
)
$uri
;
}
}
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public
function
__debugInfo
()
{
return
[
'manager'
=>
$this
->
manager
,
'uri'
=>
$this
->
uri
,
];
}
/**
/**
* Return the connection string (i.e. URI).
* Return the connection string (i.e. URI).
*
*
...
@@ -75,40 +89,45 @@ class Client
...
@@ -75,40 +89,45 @@ class Client
/**
/**
* Select a collection.
* Select a collection.
*
*
* If a write concern or read preference is not specified, the write concern
* Supported options:
* or read preference of the Client will be applied, respectively.
*
*
* @param string $databaseName Name of the database containing the collection
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* @param string $collectionName Name of the collection to select
* preference to use for collection operations. Defaults to the Client's
* @param WriteConcern $writeConcern Default write concern to apply
* read preference.
* @param ReadPreference $readPreference Default read preference to apply
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to the Client's write
* concern.
*
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options
* @return Collection
* @return Collection
*/
*/
public
function
selectCollection
(
$databaseName
,
$collectionName
,
WriteConcern
$writeConcern
=
null
,
ReadPreference
$readPreference
=
null
)
public
function
selectCollection
(
$databaseName
,
$collectionName
,
array
$options
=
[]
)
{
{
$namespace
=
$databaseName
.
'.'
.
$collectionName
;
return
new
Collection
(
$this
->
manager
,
$databaseName
.
'.'
.
$collectionName
,
$options
);
$writeConcern
=
$writeConcern
?:
$this
->
manager
->
getWriteConcern
();
$readPreference
=
$readPreference
?:
$this
->
manager
->
getReadPreference
();
return
new
Collection
(
$this
->
manager
,
$namespace
,
$writeConcern
,
$readPreference
);
}
}
/**
/**
* Select a database.
* Select a database.
*
*
* If a write concern or read preference is not specified, the write concern
* Supported options:
* or read preference of the Client will be applied, respectively.
*
*
* @param string $databaseName Name of the database to select
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* @param WriteConcern $writeConcern Default write concern to apply
* preference to use for database operations and selected collections.
* @param ReadPreference $readPreference Default read preference to apply
* Defaults to the Client's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for database operations and selected collections. Defaults to
* the Client's write concern.
*
* @param string $databaseName Name of the database to select
* @param array $options Database constructor options
* @return Database
* @return Database
*/
*/
public
function
selectDatabase
(
$databaseName
,
WriteConcern
$writeConcern
=
null
,
ReadPreference
$readPreference
=
null
)
public
function
selectDatabase
(
$databaseName
,
array
$options
=
[]
)
{
{
$writeConcern
=
$writeConcern
?:
$this
->
manager
->
getWriteConcern
();
return
new
Database
(
$this
->
manager
,
$databaseName
,
$options
);
$readPreference
=
$readPreference
?:
$this
->
manager
->
getReadPreference
();
return
new
Database
(
$this
->
manager
,
$databaseName
,
$writeConcern
,
$readPreference
);
}
}
}
}
src/Collection.php
View file @
57e05c13
...
@@ -9,6 +9,7 @@ use MongoDB\Driver\ReadPreference;
...
@@ -9,6 +9,7 @@ use MongoDB\Driver\ReadPreference;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentTypeException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Exception\UnexpectedTypeException
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInput
;
use
MongoDB\Model\IndexInput
;
...
@@ -48,13 +49,22 @@ class Collection
...
@@ -48,13 +49,22 @@ class Collection
* This class provides methods for collection-specific operations, such as
* This class provides methods for collection-specific operations, such as
* CRUD (i.e. create, read, update, and delete) and index management.
* CRUD (i.e. create, read, update, and delete) and index management.
*
*
* @param Manager $manager Manager instance from the driver
* Supported options:
* @param string $namespace Collection namespace (e.g. "db.collection")
*
* @param WriteConcern $writeConcern Default write concern to apply
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* @param ReadPreference $readPreference Default read preference to apply
* preference to use for collection operations. Defaults to the Manager's
* @throws InvalidArgumentException if $namespace is invalid
* read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to the Manager's write
* concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param array $options Collection options
* @throws InvalidArgumentException
*/
*/
public
function
__construct
(
Manager
$manager
,
$namespace
,
WriteConcern
$writeConcern
=
null
,
ReadPreference
$readPreference
=
null
)
public
function
__construct
(
Manager
$manager
,
$namespace
,
array
$options
=
[]
)
{
{
$parts
=
explode
(
'.'
,
$namespace
,
2
);
$parts
=
explode
(
'.'
,
$namespace
,
2
);
...
@@ -65,13 +75,38 @@ class Collection
...
@@ -65,13 +75,38 @@ class Collection
$this
->
databaseName
=
$parts
[
0
];
$this
->
databaseName
=
$parts
[
0
];
$this
->
collectionName
=
$parts
[
1
];
$this
->
collectionName
=
$parts
[
1
];
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
new
InvalidArgumentTypeException
(
'"readPreference" option'
,
$options
[
'readPreference'
],
'MongoDB\Driver\ReadPreference'
);
}
if
(
isset
(
$options
[
'writeConcern'
])
&&
!
$options
[
'writeConcern'
]
instanceof
WriteConcern
)
{
throw
new
InvalidArgumentTypeException
(
'"writeConcern" option'
,
$options
[
'writeConcern'
],
'MongoDB\Driver\WriteConcern'
);
}
$this
->
manager
=
$manager
;
$this
->
manager
=
$manager
;
$this
->
writeConcern
=
$writeConcern
?:
$this
->
manager
->
getWriteConcern
();
$this
->
readPreference
=
isset
(
$options
[
'readPreference'
])
?
$options
[
'readPreference'
]
:
$this
->
manager
->
getReadPreference
();
$this
->
readPreference
=
$readPreference
?:
$this
->
manager
->
getReadPreference
();
$this
->
writeConcern
=
isset
(
$options
[
'writeConcern'
])
?
$options
[
'writeConcern'
]
:
$this
->
manager
->
getWriteConcern
();
}
}
/**
/**
* Return the collection namespace.
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public
function
__debugInfo
()
{
return
[
'collectionName'
=>
$this
->
collectionName
,
'databaseName'
=>
$this
->
databaseName
,
'manager'
=>
$this
->
manager
,
'readPreference'
=>
$this
->
readPreference
,
'writeConcern'
=>
$this
->
writeConcern
,
];
}
/**
* Return the collection namespace (e.g. "db.collection").
*
*
* @param string
* @param string
*/
*/
...
@@ -563,4 +598,33 @@ class Collection
...
@@ -563,4 +598,33 @@ class Collection
return
$operation
->
execute
(
$server
);
return
$operation
->
execute
(
$server
);
}
}
/**
* Get a clone of this collection with different options.
*
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to this
* Collection's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for collection operations. Defaults to this Collection's write
* concern.
*
* @param array $options Collection constructor options
* @return Collection
*/
public
function
withOptions
(
array
$options
=
[])
{
if
(
!
isset
(
$options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
readPreference
;
}
if
(
!
isset
(
$options
[
'writeConcern'
]))
{
$options
[
'writeConcern'
]
=
$this
->
writeConcern
;
}
return
new
Collection
(
$this
->
manager
,
$this
->
databaseName
.
'.'
.
$this
->
collectionName
,
$options
);
}
}
}
src/Database.php
View file @
57e05c13
...
@@ -11,6 +11,7 @@ use MongoDB\Driver\ReadPreference;
...
@@ -11,6 +11,7 @@ use MongoDB\Driver\ReadPreference;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\InvalidArgumentTypeException
;
use
MongoDB\Model\CollectionInfoIterator
;
use
MongoDB\Model\CollectionInfoIterator
;
use
MongoDB\Operation\CreateCollection
;
use
MongoDB\Operation\CreateCollection
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\DropCollection
;
...
@@ -30,22 +31,55 @@ class Database
...
@@ -30,22 +31,55 @@ class Database
* This class provides methods for database-specific operations and serves
* This class provides methods for database-specific operations and serves
* as a gateway for accessing collections.
* as a gateway for accessing collections.
*
*
* @param Manager $manager Manager instance from the driver
* Supported options:
* @param string $databaseName Database name
*
* @param WriteConcern $writeConcern Default write concern to apply
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* @param ReadPreference $readPreference Default read preference to apply
* preference to use for database operations and selected collections.
* @throws InvalidArgumentException if $databaseName is invalid
* Defaults to the Manager's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for database operations and selected collections. Defaults to
* the Manager's write concern.
*
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param array $options Database options
* @throws InvalidArgumentException
*/
*/
public
function
__construct
(
Manager
$manager
,
$databaseName
,
WriteConcern
$writeConcern
=
null
,
ReadPreference
$readPreference
=
null
)
public
function
__construct
(
Manager
$manager
,
$databaseName
,
array
$options
=
[]
)
{
{
if
(
strlen
(
$databaseName
)
<
1
)
{
if
(
strlen
(
$databaseName
)
<
1
)
{
throw
new
InvalidArgumentException
(
'$databaseName is invalid: '
.
$databaseName
);
throw
new
InvalidArgumentException
(
'$databaseName is invalid: '
.
$databaseName
);
}
}
if
(
isset
(
$options
[
'readPreference'
])
&&
!
$options
[
'readPreference'
]
instanceof
ReadPreference
)
{
throw
new
InvalidArgumentTypeException
(
'"readPreference" option'
,
$options
[
'readPreference'
],
'MongoDB\Driver\ReadPreference'
);
}
if
(
isset
(
$options
[
'writeConcern'
])
&&
!
$options
[
'writeConcern'
]
instanceof
WriteConcern
)
{
throw
new
InvalidArgumentTypeException
(
'"writeConcern" option'
,
$options
[
'writeConcern'
],
'MongoDB\Driver\WriteConcern'
);
}
$this
->
manager
=
$manager
;
$this
->
manager
=
$manager
;
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
databaseName
=
(
string
)
$databaseName
;
$this
->
writeConcern
=
$writeConcern
?:
$this
->
manager
->
getWriteConcern
();
$this
->
readPreference
=
isset
(
$options
[
'readPreference'
])
?
$options
[
'readPreference'
]
:
$this
->
manager
->
getReadPreference
();
$this
->
readPreference
=
$readPreference
?:
$this
->
manager
->
getReadPreference
();
$this
->
writeConcern
=
isset
(
$options
[
'writeConcern'
])
?
$options
[
'writeConcern'
]
:
$this
->
manager
->
getWriteConcern
();
}
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @param array
*/
public
function
__debugInfo
()
{
return
[
'databaseName'
=>
$this
->
databaseName
,
'manager'
=>
$this
->
manager
,
'readPreference'
=>
$this
->
readPreference
,
'writeConcern'
=>
$this
->
writeConcern
,
];
}
}
/**
/**
...
@@ -129,20 +163,59 @@ class Database
...
@@ -129,20 +163,59 @@ class Database
/**
/**
* Select a collection within this database.
* Select a collection within this database.
*
*
* If a write concern or read preference is not specified, the write concern
* Supported options:
* or read preference of the Database will be applied, respectively.
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for collection operations. Defaults to the
* Database's read preference.
*
*
* @param string $collectionName Name of the collection to select
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* @param WriteConcern $writeConcern Default write concern to apply
* to use for collection operations. Defaults to the Database's write
* @param ReadPreference $readPreference Default read preference to apply
* concern.
*
* @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options
* @return Collection
* @return Collection
*/
*/
public
function
selectCollection
(
$collectionName
,
WriteConcern
$writeConcern
=
null
,
ReadPreference
$readPreference
=
null
)
public
function
selectCollection
(
$collectionName
,
array
$options
=
[]
)
{
{
$namespace
=
$this
->
databaseName
.
'.'
.
$collectionName
;
if
(
!
isset
(
$options
[
'readPreference'
]))
{
$writeConcern
=
$writeConcern
?:
$this
->
writeConcern
;
$options
[
'readPreference'
]
=
$this
->
readPreference
;
$readPreference
=
$readPreference
?:
$this
->
readPreference
;
}
if
(
!
isset
(
$options
[
'writeConcern'
]))
{
$options
[
'writeConcern'
]
=
$this
->
writeConcern
;
}
return
new
Collection
(
$this
->
manager
,
$this
->
databaseName
.
'.'
.
$collectionName
,
$options
);
}
/**
* Get a clone of this database with different options.
*
* Supported options:
*
* * readPreference (MongoDB\Driver\ReadPreference): The default read
* preference to use for database operations and selected collections.
* Defaults to this Database's read preference.
*
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
* to use for database operations and selected collections. Defaults to
* this Database's write concern.
*
* @param array $options Database constructor options
* @return Database
*/
public
function
withOptions
(
array
$options
=
[])
{
if
(
!
isset
(
$options
[
'readPreference'
]))
{
$options
[
'readPreference'
]
=
$this
->
readPreference
;
}
if
(
!
isset
(
$options
[
'writeConcern'
]))
{
$options
[
'writeConcern'
]
=
$this
->
writeConcern
;
}
return
new
Collection
(
$this
->
manager
,
$namespace
,
$writeConcern
,
$readPreference
);
return
new
Database
(
$this
->
manager
,
$this
->
databaseName
,
$options
);
}
}
}
}
tests/ClientTest.php
View file @
57e05c13
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
namespace
MongoDB\Tests
;
namespace
MongoDB\Tests
;
use
MongoDB\Client
;
use
MongoDB\Client
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
/**
/**
* Unit tests for the Client class.
* Unit tests for the Client class.
...
@@ -22,4 +24,72 @@ class ClientTest extends TestCase
...
@@ -22,4 +24,72 @@ class ClientTest extends TestCase
$this
->
assertSame
(
$this
->
getUri
(),
(
string
)
$client
);
$this
->
assertSame
(
$this
->
getUri
(),
(
string
)
$client
);
}
}
public
function
testSelectCollectionInheritsReadPreferenceAndWriteConcern
()
{
$clientOptions
=
[
'readPreference'
=>
'secondaryPreferred'
,
'w'
=>
WriteConcern
::
MAJORITY
,
];
$client
=
new
Client
(
$this
->
getUri
(),
$clientOptions
);
$collection
=
$client
->
selectCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$debug
=
$collection
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testSelectCollectionPassesReadPreferenceAndWriteConcern
()
{
$collectionOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$client
=
new
Client
(
$this
->
getUri
());
$collection
=
$client
->
selectCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$collectionOptions
);
$debug
=
$collection
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testSelectDatabaseInheritsReadPreferenceAndWriteConcern
()
{
$clientOptions
=
[
'readPreference'
=>
'secondaryPreferred'
,
'w'
=>
WriteConcern
::
MAJORITY
,
];
$client
=
new
Client
(
$this
->
getUri
(),
$clientOptions
);
$database
=
$client
->
selectDatabase
(
$this
->
getDatabaseName
());
$debug
=
$database
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testSelectDatabasePassesReadPreferenceAndWriteConcern
()
{
$databaseOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$client
=
new
Client
(
$this
->
getUri
());
$database
=
$client
->
selectDatabase
(
$this
->
getDatabaseName
(),
$databaseOptions
);
$debug
=
$database
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
}
}
tests/Collection/CollectionFunctionalTest.php
View file @
57e05c13
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Collection;
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Collection;
use
MongoDB\Collection
;
use
MongoDB\Collection
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
/**
/**
* Functional tests for the Collection class.
* Functional tests for the Collection class.
...
@@ -31,6 +33,30 @@ class CollectionFunctionalTest extends FunctionalTestCase
...
@@ -31,6 +33,30 @@ class CollectionFunctionalTest extends FunctionalTestCase
];
];
}
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public
function
testConstructorOptionTypeChecks
(
array
$options
)
{
new
Collection
(
$this
->
manager
,
$this
->
getNamespace
(),
$options
);
}
public
function
provideInvalidConstructorOptions
()
{
$options
=
[];
foreach
(
$this
->
getInvalidReadPreferenceValues
()
as
$value
)
{
$options
[][]
=
[
'readPreference'
=>
$value
];
}
foreach
(
$this
->
getInvalidWriteConcernValues
()
as
$value
)
{
$options
[][]
=
[
'writeConcern'
=>
$value
];
}
return
$options
;
}
public
function
testToString
()
public
function
testToString
()
{
{
$this
->
assertEquals
(
$this
->
getNamespace
(),
(
string
)
$this
->
collection
);
$this
->
assertEquals
(
$this
->
getNamespace
(),
(
string
)
$this
->
collection
);
...
@@ -85,6 +111,39 @@ class CollectionFunctionalTest extends FunctionalTestCase
...
@@ -85,6 +111,39 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this
->
assertEquals
(
$expected
,
$this
->
collection
->
findOne
(
$filter
,
$options
));
$this
->
assertEquals
(
$expected
,
$this
->
collection
->
findOne
(
$filter
,
$options
));
}
}
public
function
testWithOptionsInheritsReadPreferenceAndWriteConcern
()
{
$collectionOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$collection
=
new
Collection
(
$this
->
manager
,
$this
->
getNamespace
(),
$collectionOptions
);
$clone
=
$collection
->
withOptions
();
$debug
=
$clone
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testWithOptionsPassesReadPreferenceAndWriteConcern
()
{
$collectionOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$clone
=
$this
->
collection
->
withOptions
(
$collectionOptions
);
$debug
=
$clone
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
/**
/**
* Create data fixtures.
* Create data fixtures.
*
*
...
...
tests/Database/DatabaseFunctionalTest.php
View file @
57e05c13
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Database;
...
@@ -4,6 +4,8 @@ namespace MongoDB\Tests\Database;
use
MongoDB\Database
;
use
MongoDB\Database
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
/**
/**
* Functional tests for the Database class.
* Functional tests for the Database class.
...
@@ -28,6 +30,30 @@ class DatabaseFunctionalTest extends FunctionalTestCase
...
@@ -28,6 +30,30 @@ class DatabaseFunctionalTest extends FunctionalTestCase
];
];
}
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentTypeException
* @dataProvider provideInvalidConstructorOptions
*/
public
function
testConstructorOptionTypeChecks
(
array
$options
)
{
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$options
);
}
public
function
provideInvalidConstructorOptions
()
{
$options
=
[];
foreach
(
$this
->
getInvalidReadPreferenceValues
()
as
$value
)
{
$options
[][]
=
[
'readPreference'
=>
$value
];
}
foreach
(
$this
->
getInvalidWriteConcernValues
()
as
$value
)
{
$options
[][]
=
[
'writeConcern'
=>
$value
];
}
return
$options
;
}
public
function
testToString
()
public
function
testToString
()
{
{
$this
->
assertEquals
(
$this
->
getDatabaseName
(),
(
string
)
$this
->
database
);
$this
->
assertEquals
(
$this
->
getDatabaseName
(),
(
string
)
$this
->
database
);
...
@@ -50,4 +76,70 @@ class DatabaseFunctionalTest extends FunctionalTestCase
...
@@ -50,4 +76,70 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertCollectionCount
(
$this
->
getNamespace
(),
0
);
$this
->
assertCollectionCount
(
$this
->
getNamespace
(),
0
);
}
}
public
function
testSelectCollectionInheritsReadPreferenceAndWriteConcern
()
{
$databaseOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$database
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$databaseOptions
);
$collection
=
$database
->
selectCollection
(
$this
->
getCollectionName
());
$debug
=
$collection
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testSelectCollectionPassesReadPreferenceAndWriteConcern
()
{
$collectionOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$collection
=
$this
->
database
->
selectCollection
(
$this
->
getCollectionName
(),
$collectionOptions
);
$debug
=
$collection
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testWithOptionsInheritsReadPreferenceAndWriteConcern
()
{
$databaseOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$database
=
new
Database
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$databaseOptions
);
$clone
=
$database
->
withOptions
();
$debug
=
$clone
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
public
function
testWithOptionsPassesReadPreferenceAndWriteConcern
()
{
$databaseOptions
=
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_SECONDARY_PREFERRED
),
'writeConcern'
=>
new
WriteConcern
(
WriteConcern
::
MAJORITY
),
];
$clone
=
$this
->
database
->
withOptions
(
$databaseOptions
);
$debug
=
$clone
->
__debugInfo
();
$this
->
assertInstanceOf
(
'MongoDB\Driver\ReadPreference'
,
$debug
[
'readPreference'
]);
$this
->
assertSame
(
ReadPreference
::
RP_SECONDARY_PREFERRED
,
$debug
[
'readPreference'
]
->
getMode
());
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
}
}
tests/Operation/TestCase.php
View file @
57e05c13
...
@@ -45,16 +45,6 @@ abstract class TestCase extends BaseTestCase
...
@@ -45,16 +45,6 @@ abstract class TestCase extends BaseTestCase
return
[
123
,
3.14
,
true
,
[],
new
stdClass
];
return
[
123
,
3.14
,
true
,
[],
new
stdClass
];
}
}
protected
function
getInvalidReadPreferenceValues
()
{
return
[
123
,
3.14
,
'foo'
,
true
,
[],
new
stdClass
];
}
protected
function
getInvalidWriteConcernValues
()
{
return
[
123
,
3.14
,
'foo'
,
true
,
[],
new
stdClass
];
}
protected
function
wrapValuesForDataProvider
(
array
$values
)
protected
function
wrapValuesForDataProvider
(
array
$values
)
{
{
return
array_map
(
function
(
$value
)
{
return
[
$value
];
},
$values
);
return
array_map
(
function
(
$value
)
{
return
[
$value
];
},
$values
);
...
...
tests/TestCase.php
View file @
57e05c13
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
MongoDB\Tests
;
namespace
MongoDB\Tests
;
use
ReflectionClass
;
use
ReflectionClass
;
use
stdClass
;
abstract
class
TestCase
extends
\PHPUnit_Framework_TestCase
abstract
class
TestCase
extends
\PHPUnit_Framework_TestCase
{
{
...
@@ -28,6 +29,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
...
@@ -28,6 +29,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
return
getenv
(
'MONGODB_DATABASE'
)
?:
'phplib_test'
;
return
getenv
(
'MONGODB_DATABASE'
)
?:
'phplib_test'
;
}
}
protected
function
getInvalidReadPreferenceValues
()
{
return
[
123
,
3.14
,
'foo'
,
true
,
[],
new
stdClass
];
}
protected
function
getInvalidWriteConcernValues
()
{
return
[
123
,
3.14
,
'foo'
,
true
,
[],
new
stdClass
];
}
/**
/**
* Return the test namespace.
* Return the test namespace.
*
*
...
...
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