-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from xp-forge/feature/findAndModify
Implement Collection `modify()` and `remove()` via findAndModify
- Loading branch information
Showing
4 changed files
with
272 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php namespace com\mongodb\result; | ||
|
||
use com\mongodb\Document; | ||
|
||
/** | ||
* The result of a `findAndModify` operation | ||
* | ||
* @see com.mongodb.Collection::modify | ||
* @see com.mongodb.Collection::remove | ||
* @see https://www.mongodb.com/docs/manual/reference/command/findAndModify/ | ||
* @test com.mongodb.unittest.result.ModificationTest | ||
*/ | ||
class Modification extends Result { | ||
const REMOVED= 'removed'; | ||
const CREATED= 'created'; | ||
const UPDATED= 'updated'; | ||
|
||
/** Returns number of modified documents */ | ||
public function modified(): int { return $this->result['lastErrorObject']['n']; } | ||
|
||
/** Returns kind of modification: created, updated or removed. */ | ||
public function kind(): string { | ||
if (isset($this->result['lastErrorObject']['upserted'])) { | ||
return self::CREATED; | ||
} else if (isset($this->result['lastErrorObject']['updatedExisting'])) { | ||
return self::UPDATED; | ||
} else { | ||
return self::REMOVED; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the upserted ID, if any | ||
* | ||
* @return ?(string|com.mongodb.ObjectId) | ||
*/ | ||
public function upserted() { | ||
return $this->result['lastErrorObject']['upserted'] ?? null; | ||
} | ||
|
||
/** | ||
* Returns the document | ||
* | ||
* @return ?com.mongodb.Document | ||
*/ | ||
public function document() { | ||
return isset($this->result['value']) ? new Document($this->result['value']) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/test/php/com/mongodb/unittest/result/ModificationTest.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php namespace com\mongodb\unittest\result; | ||
|
||
use com\mongodb\result\Modification; | ||
use com\mongodb\{Document, ObjectId}; | ||
use test\{Assert, Before, Test}; | ||
|
||
class ModificationTest { | ||
private $objectId; | ||
|
||
/** Creates a result from an update operation */ | ||
private function update(Document $document= null, $created= false) { | ||
if (null === $document) { | ||
$lastErrorObject= ['n' => 0, 'updatedExisting' => false]; | ||
$value= null; | ||
} else if ($created) { | ||
$lastErrorObject= ['n' => 1, 'updatedExisting' => false, 'upserted' => $document->id()]; | ||
$value= $document->properties(); | ||
} else { | ||
$lastErrorObject= ['n' => 1, 'updatedExisting' => true]; | ||
$value= $document->properties(); | ||
} | ||
return ['lastErrorObject' => $lastErrorObject, 'value' => $value, 'ok' => 1]; | ||
} | ||
|
||
/** Creates a result from a remove operation */ | ||
private function remove(Document $document= null) { | ||
if (null === $document) { | ||
$lastErrorObject= ['n' => 0]; | ||
$value= null; | ||
} else { | ||
$lastErrorObject= ['n' => 1]; | ||
$value= $document->properties(); | ||
} | ||
return ['lastErrorObject' => $lastErrorObject, 'value' => $value, 'ok' => 1]; | ||
} | ||
|
||
#[Before] | ||
public function objectId() { | ||
$this->objectId= ObjectId::create(); | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new Modification($this->update()); | ||
} | ||
|
||
#[Test] | ||
public function none_modified() { | ||
Assert::equals(0, (new Modification($this->update()))->modified()); | ||
} | ||
|
||
#[Test] | ||
public function modified() { | ||
$doc= new Document(['test' => true]); | ||
Assert::equals(1, (new Modification($this->update($doc)))->modified()); | ||
} | ||
|
||
#[Test] | ||
public function updated_existing() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals(Modification::UPDATED, ((new Modification($this->update($doc)))->kind())); | ||
} | ||
|
||
#[Test] | ||
public function created_new() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals(Modification::CREATED, (new Modification($this->update($doc, true)))->kind()); | ||
} | ||
|
||
#[Test] | ||
public function not_upserted() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::null((new Modification($this->update($doc)))->upserted()); | ||
} | ||
|
||
#[Test] | ||
public function upserted_id() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals($this->objectId, (new Modification($this->update($doc, true)))->upserted()); | ||
} | ||
|
||
#[Test] | ||
public function document() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals($doc, (new Modification($this->update($doc)))->document()); | ||
} | ||
|
||
#[Test] | ||
public function no_document() { | ||
Assert::null((new Modification($this->update()))->document()); | ||
} | ||
|
||
#[Test] | ||
public function removed() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals($doc, (new Modification($this->remove($doc)))->document()); | ||
} | ||
|
||
#[Test] | ||
public function not_removed() { | ||
Assert::null((new Modification($this->remove()))->document()); | ||
} | ||
|
||
#[Test] | ||
public function removal_kind() { | ||
Assert::equals(Modification::REMOVED, (new Modification($this->remove()))->kind()); | ||
} | ||
} |