-
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.
Make this library compatible with
xp-forge/mongodb
2.0.0
- Loading branch information
Showing
5 changed files
with
210 additions
and
98 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
100 changes: 100 additions & 0 deletions
100
src/test/php/web/session/mongo/unittest/CollectionV1.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,100 @@ | ||
<?php namespace web\session\mongo\unittest; | ||
|
||
use com\mongodb\result\{Cursor, Delete, Insert, Run, Update}; | ||
use com\mongodb\{Collection, Document, Int64, ObjectId, Session}; | ||
|
||
class CollectionV1 extends Collection { | ||
private $lookup= []; | ||
|
||
public function __construct($documents) { | ||
foreach ($documents as $document) { | ||
$this->lookup[$document->id()->string()]= $document; | ||
} | ||
} | ||
|
||
public function present($id) { | ||
return isset($this->lookup[$id]); | ||
} | ||
|
||
public function find($query= [], Session $session= null): Cursor { | ||
|
||
// Query will always be an ObjectId | ||
$result= $this->lookup[$query->string()] ?? null; | ||
return new Cursor(null, $session, [ | ||
'firstBatch' => [$result->properties()], | ||
'id' => new Int64(0), | ||
'ns' => 'test.sessions' | ||
]); | ||
} | ||
|
||
public function insert($arg, Session $session= null): Insert { | ||
|
||
// Argument will always be a Document | ||
$arg['_id']= ObjectId::create(); | ||
$this->lookup[$arg['_id']->string()]= $arg; | ||
return new Insert([], [$arg['_id']]); | ||
} | ||
|
||
public function run($name, array $params= [], $method= 'write', Session $session= null) { | ||
switch ($name) { | ||
case 'listIndexes': | ||
return new Run(null, null, ['body' => [ | ||
'ok' => 1, | ||
'cursor' => [ | ||
'id' => new Int64(0), | ||
'ns' => 'test.sessions', | ||
'firstBatch' => [[ | ||
'v' => 2, | ||
'key' => ['_created' => 1], | ||
'name' => '_created_1', | ||
'expireAfterSeconds' => 1800, | ||
]] | ||
] | ||
]]); | ||
|
||
case 'findAndModify': | ||
|
||
// Query will always be an ObjectId | ||
$result= &$this->lookup[$params['query']['_id']->string()]; | ||
switch (key($params['update'])) { | ||
case '$set': | ||
foreach ($params['update']['$set'] as $name => $value) { | ||
$result[$name]= $value; | ||
} | ||
break; | ||
|
||
case '$unset': | ||
foreach ($params['update']['$unset'] as $name => $_) { | ||
unset($result[$name]); | ||
} | ||
break; | ||
} | ||
|
||
return new Run(null, null, ['body' => [ | ||
'lastErrorObject' => ['n' => 1, 'updatedExisting' => true], | ||
'value' => $result->properties() | ||
]]); | ||
|
||
default: | ||
throw new IllegalStateException('Unreachable code - command "'.$name.'"'); | ||
} | ||
} | ||
|
||
public function delete($query, Session $session= null): Delete { | ||
|
||
// Query will either be an ObjectId or [_created => [$lt => time()]] | ||
if ($query instanceof ObjectId) { | ||
unset($this->lookup[$query->string()]); | ||
$n= 1; | ||
} else { | ||
$n= 0; | ||
foreach ($this->lookup as $id => $document) { | ||
if ($document['_created']->isBefore($query['_created']['$lt'])) { | ||
unset($this->lookup[$id]); | ||
$n++; | ||
} | ||
} | ||
} | ||
return new Delete(['n' => $n], [$query]); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/test/php/web/session/mongo/unittest/CollectionV2.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,100 @@ | ||
<?php namespace web\session\mongo\unittest; | ||
|
||
use com\mongodb\result\{Cursor, Delete, Insert, Run, Update}; | ||
use com\mongodb\{Collection, Document, Int64, ObjectId, Options}; | ||
|
||
class CollectionV2 extends Collection { | ||
private $lookup= []; | ||
|
||
public function __construct($documents) { | ||
foreach ($documents as $document) { | ||
$this->lookup[$document->id()->string()]= $document; | ||
} | ||
} | ||
|
||
public function present($id) { | ||
return isset($this->lookup[$id]); | ||
} | ||
|
||
public function find($query= [], Options... $options): Cursor { | ||
|
||
// Query will always be an ObjectId | ||
$result= $this->lookup[$query->string()] ?? null; | ||
return new Cursor(null, $options, [ | ||
'firstBatch' => [$result->properties()], | ||
'id' => new Int64(0), | ||
'ns' => 'test.sessions' | ||
]); | ||
} | ||
|
||
public function insert($arg, Options... $options): Insert { | ||
|
||
// Argument will always be a Document | ||
$arg['_id']= ObjectId::create(); | ||
$this->lookup[$arg['_id']->string()]= $arg; | ||
return new Insert([], [$arg['_id']]); | ||
} | ||
|
||
public function run($name, array $params= [], $method= 'write', Options... $options) { | ||
switch ($name) { | ||
case 'listIndexes': | ||
return new Run(null, null, ['body' => [ | ||
'ok' => 1, | ||
'cursor' => [ | ||
'id' => new Int64(0), | ||
'ns' => 'test.sessions', | ||
'firstBatch' => [[ | ||
'v' => 2, | ||
'key' => ['_created' => 1], | ||
'name' => '_created_1', | ||
'expireAfterSeconds' => 1800, | ||
]] | ||
] | ||
]]); | ||
|
||
case 'findAndModify': | ||
|
||
// Query will always be an ObjectId | ||
$result= &$this->lookup[$params['query']['_id']->string()]; | ||
switch (key($params['update'])) { | ||
case '$set': | ||
foreach ($params['update']['$set'] as $name => $value) { | ||
$result[$name]= $value; | ||
} | ||
break; | ||
|
||
case '$unset': | ||
foreach ($params['update']['$unset'] as $name => $_) { | ||
unset($result[$name]); | ||
} | ||
break; | ||
} | ||
|
||
return new Run(null, null, ['body' => [ | ||
'lastErrorObject' => ['n' => 1, 'updatedExisting' => true], | ||
'value' => $result->properties() | ||
]]); | ||
|
||
default: | ||
throw new IllegalStateException('Unreachable code - command "'.$name.'"'); | ||
} | ||
} | ||
|
||
public function delete($query, Options... $options): Delete { | ||
|
||
// Query will either be an ObjectId or [_created => [$lt => time()]] | ||
if ($query instanceof ObjectId) { | ||
unset($this->lookup[$query->string()]); | ||
$n= 1; | ||
} else { | ||
$n= 0; | ||
foreach ($this->lookup as $id => $document) { | ||
if ($document['_created']->isBefore($query['_created']['$lt'])) { | ||
unset($this->lookup[$id]); | ||
$n++; | ||
} | ||
} | ||
} | ||
return new Delete(['n' => $n], [$query]); | ||
} | ||
} |
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