Skip to content

Run testsuite against ext-mongo #28

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

Merged
merged 25 commits into from
Feb 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5d4085c
Allow tests to run with ext-mongo installed
alcaeus Jan 8, 2016
e68a904
Add ext-mongo tests to build matrix
alcaeus Jan 14, 2016
ac7e861
Remove obvious errors in test suite
alcaeus Jan 14, 2016
5bc48d9
Fix tests
alcaeus Jan 17, 2016
f8f4906
Remove DEFAULT_CHUNK_SIZE constant
alcaeus Jan 17, 2016
4616752
Change exception handling
alcaeus Jan 17, 2016
ee40980
Stabilize MongoClient class
alcaeus Jan 17, 2016
6301ae8
Stabilize MongoDB class
alcaeus Jan 17, 2016
26ba61c
Stabilize a bunch of tests with ext-mongo
alcaeus Jan 20, 2016
062a8a1
Correctly throw MongoDuplicateKeyExceptions
Jan 26, 2016
f3a9081
Install legacy driver after composer install
alcaeus Jan 26, 2016
3a90379
Fix GridFS tests against ext-mongo
alcaeus Jan 26, 2016
8738430
Fix error in cursor iterator behavior
alcaeus Jan 26, 2016
81d7fa4
Fix failing test due to error message
alcaeus Jan 28, 2016
d4d337c
Change build matrix to only run ext-mongo on 5.6
alcaeus Jan 28, 2016
1deb739
fix MongoDB::CreateDBRef
Jan 30, 2016
c0c01bd
Stabilize batch classes.
Jan 30, 2016
2bcb1f7
Add WriteConcernException to exceptionconverter.
Jan 30, 2016
9e81cea
Start stabilizing MongoCollection
alcaeus Jan 30, 2016
70c5a5d
Stabilize MongoCursor classes
alcaeus Feb 1, 2016
0330cac
Default to '' all update data in findAndModify if doesn't contain a …
martinatsli Feb 3, 2016
c370fc7
Stabilize batch classes
alcaeus Feb 5, 2016
9741e02
Ensure correct exceptions in GridFS class
alcaeus Feb 5, 2016
9f24c12
Fix tests in MongoCollection
alcaeus Feb 6, 2016
199e9d2
Skip aggregation tests until bug in library is fixed
alcaeus Feb 6, 2016
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
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ php:
- 7.0

env:
matrix:
- DRIVER_VERSION=1.1.1
- DRIVER_VERSION=1.1.1

matrix:
include:
- php: 5.6
env: DRIVER_VERSION=1.1.1 LEGACY_DRIVER_VERSION=1.6.12

addons:
apt:
Expand All @@ -20,6 +24,7 @@ addons:
before_script:
- pecl install -f mongodb-${DRIVER_VERSION}
- composer install
- if [ "x$LEGACY_DRIVER_VERSION" != "x" ]; then yes '' | pecl -q install -f mongo-${LEGACY_DRIVER_VERSION}; fi

script:
- ./vendor/bin/phpunit --coverage-clover=coverage.clover
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ return `0` as connection ID.

## MongoCollection

- The [aggregate](https://secure.php.net/manual/en/mongocollection.aggregate.php)
method is not working because of a bug in the underlying library.
- The [createIndex](https://secure.php.net/manual/en/mongocollection.createindex.php)
method does not yet return the same result as the original method. Instead, it
always returns the name of the index created.
Expand All @@ -109,14 +111,22 @@ return `0` as connection ID.
- The [hasNext](https://php.net/manual/en/mongocursor.hasnext.php)
method is not yet implemented.
- The [info](https://php.net/manual/en/mongocursor.info.php) method does not
reliably fill all fields in the cursor information. This includes the `at`, `numReturned`,
and `server` keys once the cursor has started iterating.
reliably fill all fields in the cursor information. This includes the `numReturned`
and `server` keys once the cursor has started iterating. The `numReturned` field
will always show the same value as the `at` field. The `server` field is lacking
authentication information.
- The [setFlag](https://php.net/manual/en/mongocursor.setflag.php)
method is not yet implemented.

## MongoCommandCursor
- The [createFromDocument](https://php.net/manual/en/mongocommandcursor.createfromdocument.php)
method is not yet implemented.
- The [info](https://php.net/manual/en/mongocommandcursor.info.php) method does not
reliably fill all fields in the cursor information. This includes the `at`, `numReturned`,
`firstBatchAt` and `firstBatchNumReturned` fields. The `at` and `numReturned`
fields always return 0 for compatibility to MongoCursor. The `firstBatchAt` and
`firstBatchNumReturned` fields will contain the same value, which is the internal
position of the iterator.

## Types

Expand Down
33 changes: 26 additions & 7 deletions lib/Alcaeus/MongoDbAdapter/AbstractCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class AbstractCursor
/**
* @var int
*/
protected $batchSize;
protected $batchSize = 0;

/**
* @var Collection
Expand Down Expand Up @@ -66,6 +66,11 @@ abstract class AbstractCursor
*/
protected $startedIterating = false;

/**
* @var int
*/
protected $position = 0;

/**
* @var array
*/
Expand Down Expand Up @@ -113,7 +118,10 @@ public function __construct(\MongoClient $connection, $ns)
*/
public function current()
{
$this->startedIterating = true;
if (! $this->startedIterating) {
return null;
}

$document = $this->ensureIterator()->current();
if ($document !== null) {
$document = TypeConverter::toLegacy($document);
Expand All @@ -129,6 +137,10 @@ public function current()
*/
public function key()
{
if (! $this->startedIterating) {
return null;
}

return $this->ensureIterator()->key();
}

Expand All @@ -141,11 +153,12 @@ public function key()
*/
public function next()
{
if (!$this->startedIterating) {
if (! $this->startedIterating) {
$this->ensureIterator();
$this->startedIterating = true;
} else {
$this->ensureIterator()->next();
$this->position++;
}

return $this->current();
Expand All @@ -162,6 +175,7 @@ public function rewind()
// We can recreate the cursor to allow it to be rewound
$this->reset();
$this->startedIterating = true;
$this->position = 0;
$this->ensureIterator()->rewind();
}

Expand All @@ -172,6 +186,10 @@ public function rewind()
*/
public function valid()
{
if (! $this->startedIterating) {
return false;
}

return $this->ensureIterator()->valid();
}

Expand Down Expand Up @@ -323,11 +341,12 @@ protected function getIterationInfo()
$typeString = 'STANDALONE';
}

$cursorId = (string) $this->cursor->getId();
$iterationInfo += [
'id' => (string) $this->cursor->getId(),
'at' => null, // @todo Complete info for cursor that is iterating
'numReturned' => null, // @todo Complete info for cursor that is iterating
'server' => null, // @todo Complete info for cursor that is iterating
'id' => (int) $cursorId,
'at' => $this->position,
'numReturned' => $this->position, // This can't be obtained from the new cursor
'server' => sprintf('%s:%d;-;.;%d', $this->cursor->getServer()->getHost(), $this->cursor->getServer()->getPort(), getmypid()),
'host' => $this->cursor->getServer()->getHost(),
'port' => $this->cursor->getServer()->getPort(),
'connection_type_desc' => $typeString,
Expand Down
41 changes: 29 additions & 12 deletions lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class ExceptionConverter
*
* @return \MongoException
*/
public static function convertException(Exception\Exception $e, $fallbackClass = 'MongoException')
public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
{
$message = $e->getMessage();
$code = $e->getCode();

switch (get_class($e)) {
case Exception\AuthenticationException::class:
case Exception\ConnectionException::class:
Expand All @@ -40,7 +43,25 @@ public static function convertException(Exception\Exception $e, $fallbackClass =

case Exception\BulkWriteException::class:
case Exception\WriteException::class:
$class = 'MongoCursorException';
$writeResult = $e->getWriteResult();

if ($writeResult) {
$writeError = $writeResult->getWriteErrors()[0];

$message = $writeError->getMessage();
$code = $writeError->getCode();
}

switch ($code) {
// see https://github.com/mongodb/mongo-php-driver-legacy/blob/ad3ed45739e9702ae48e53ddfadc482d9c4c7e1c/cursor_shared.c#L540
case 11000:
case 11001:
case 12582:
$class = 'MongoDuplicateKeyException';
break;
default:
$class = 'MongoCursorException';
}
break;

case Exception\ExecutionTimeoutException::class:
Expand All @@ -51,18 +72,14 @@ public static function convertException(Exception\Exception $e, $fallbackClass =
$class = $fallbackClass;
}

if (strpos($e->getMessage(), 'No suitable servers found') !== false) {
return new \MongoConnectionException($e->getMessage(), $e->getCode(), $e);
if (strpos($message, 'No suitable servers found') !== false) {
return new \MongoConnectionException($message, $code, $e);
}

return new $class($e->getMessage(), $e->getCode(), $e);
}
if ($message === "cannot use 'w' > 1 when a host is not replicated") {
return new \MongoWriteConcernException($message, $code, $e);
}

/**
* @throws \MongoException
*/
public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
{
throw self::convertException($e, $fallbackClass);
return new $class($message, $code, $e);
}
}
12 changes: 8 additions & 4 deletions lib/Mongo/MongoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ public function getHosts()
try {
$servers = $this->manager->getServers();
} catch (\MongoDB\Driver\Exception\Exception $e) {
ExceptionConverter::toLegacy($e);
throw ExceptionConverter::toLegacy($e);
}

foreach ($servers as $server) {
$key = sprintf('%s:%d', $server->getHost(), $server->getPort());
$key = sprintf('%s:%d;-;.;%d', $server->getHost(), $server->getPort(), getmypid());
$info = $server->getInfo();

switch ($server->getType()) {
Expand Down Expand Up @@ -246,7 +246,7 @@ public function listDBs()
try {
$databaseInfoIterator = $this->client->listDatabases();
} catch (\MongoDB\Driver\Exception\Exception $e) {
ExceptionConverter::toLegacy($e);
throw ExceptionConverter::toLegacy($e);
}

$databases = [
Expand All @@ -256,7 +256,11 @@ public function listDBs()
];

foreach ($databaseInfoIterator as $databaseInfo) {
$databases['databases'][] = $databaseInfo->getName();
$databases['databases'][] = [
'name' => $databaseInfo->getName(),
'empty' => $databaseInfo->isEmpty(),
'sizeOnDisk' => $databaseInfo->getSizeOnDisk(),
];
$databases['totalSize'] += $databaseInfo->getSizeOnDisk();
}

Expand Down
Loading