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
52b81e99
Commit
52b81e99
authored
Nov 24, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-49: withOptions() clone method for Database and Collection
parent
d13e1109
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
126 additions
and
0 deletions
+126
-0
Collection.php
src/Collection.php
+29
-0
Database.php
src/Database.php
+29
-0
CollectionFunctionalTest.php
tests/Collection/CollectionFunctionalTest.php
+35
-0
DatabaseFunctionalTest.php
tests/Database/DatabaseFunctionalTest.php
+33
-0
No files found.
src/Collection.php
View file @
52b81e99
...
@@ -598,4 +598,33 @@ class Collection
...
@@ -598,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 @
52b81e99
...
@@ -189,4 +189,33 @@ class Database
...
@@ -189,4 +189,33 @@ class Database
return
new
Collection
(
$this
->
manager
,
$this
->
databaseName
.
'.'
.
$collectionName
,
$options
);
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
Database
(
$this
->
manager
,
$this
->
databaseName
,
$options
);
}
}
}
tests/Collection/CollectionFunctionalTest.php
View file @
52b81e99
...
@@ -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.
...
@@ -109,6 +111,39 @@ class CollectionFunctionalTest extends FunctionalTestCase
...
@@ -109,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 @
52b81e99
...
@@ -109,4 +109,37 @@ class DatabaseFunctionalTest extends FunctionalTestCase
...
@@ -109,4 +109,37 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertInstanceOf
(
'MongoDB\Driver\WriteConcern'
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
$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
());
}
}
}
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