Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using bools for writeConcern #151

Merged
merged 1 commit into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ All issues and pull requests under this release may be found under the
[1.0.8](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.8)
milestone.

* [#151](https://github.com/alcaeus/mongo-php-adapter/pull/151) allows using
boolean values when passing a write concern option to an insert or update. While
never documented, this was happily accepted by the legacy driver.
* [#150](https://github.com/alcaeus/mongo-php-adapter/pull/150) adds missing
options to `MongoCollection::indexInfo`.
* [#149](https://github.com/alcaeus/mongo-php-adapter/pull/149) fixes calls to
Expand Down
7 changes: 6 additions & 1 deletion lib/Alcaeus/MongoDbAdapter/Helper/WriteConcernConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
trait WriteConcernConverter
{
/**
* @param string|int $wstring
* @param string|int|bool $wstring
* @param int $wtimeout
* @return \MongoDB\Driver\WriteConcern
*/
protected function createWriteConcernFromParameters($wstring, $wtimeout)
{
// Convert legacy write concern
if (is_bool($wstring)) {
$wstring = (int) $wstring;
}

if (! is_string($wstring) && ! is_int($wstring)) {
trigger_error("w for WriteConcern must be a string or integer", E_USER_WARNING);
return false;
Expand Down
20 changes: 20 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ public function testUnacknowledgedWrite()
$this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
}

public function testUnacknowledgedWriteWithBooleanValue()
{
$document = ['foo' => 'bar'];
$this->assertTrue($this->getCollection()->insert($document, ['w' => false]));
}

public function testAcknowledgedWriteConcernWithBool()
{
$document = ['foo' => 'bar'];
$this->assertSame(
[
'ok' => 1.0,
'n' => 0,
'err' => null,
'errmsg' => null,
],
$this->getCollection()->insert($document, ['w' => true])
);
}

public function testInsertWriteConcernException()
{
$this->setExpectedException(
Expand Down