-
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.
Implement findAndModify command as
Collection::modify()
- Loading branch information
Showing
3 changed files
with
138 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,33 @@ | ||
<?php namespace com\mongodb\result; | ||
|
||
use com\mongodb\Document; | ||
|
||
/** @test com.mongodb.unittest.result.ModificationTest */ | ||
class Modification extends Result { | ||
|
||
/** Returns number of modified documents */ | ||
public function modified(): int { return $this->result['lastErrorObject']['n']; } | ||
|
||
/** Returns whether an existing document was updated */ | ||
public function updatedExisting(): bool { | ||
return $this->result['lastErrorObject']['updatedExisting']; | ||
} | ||
|
||
/** | ||
* Returns the upserted ID, if any | ||
* | ||
* @return ?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; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
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,80 @@ | ||
<?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 */ | ||
private function result(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]; | ||
} | ||
|
||
#[Before] | ||
public function objectId() { | ||
$this->objectId= ObjectId::create(); | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new Modification($this->result()); | ||
} | ||
|
||
#[Test] | ||
public function none_modified() { | ||
Assert::equals(0, (new Modification($this->result()))->modified()); | ||
} | ||
|
||
#[Test] | ||
public function modified() { | ||
$doc= new Document(['test' => true]); | ||
Assert::equals(1, (new Modification($this->result($doc)))->modified()); | ||
} | ||
|
||
#[Test] | ||
public function updated_existing() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::true((new Modification($this->result($doc)))->updatedExisting()); | ||
} | ||
|
||
#[Test] | ||
public function created_new() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::false((new Modification($this->result($doc, true)))->updatedExisting()); | ||
} | ||
|
||
#[Test] | ||
public function not_upserted() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::null((new Modification($this->result($doc)))->upserted()); | ||
} | ||
|
||
#[Test] | ||
public function upserted_id() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals($this->objectId, (new Modification($this->result($doc, true)))->upserted()); | ||
} | ||
|
||
#[Test] | ||
public function document() { | ||
$doc= new Document(['_id' => $this->objectId, 'test' => true]); | ||
Assert::equals($doc, (new Modification($this->result($doc)))->document()); | ||
} | ||
|
||
#[Test] | ||
public function no_document() { | ||
Assert::null((new Modification($this->result()))->document()); | ||
} | ||
} |