Skip to content

Commit e8bb929

Browse files
committed
Merge pull request #28 from alcaeus/run-ext-mongo-tests
Run testsuite against ext-mongo
2 parents f719cb7 + 199e9d2 commit e8bb929

35 files changed

+660
-392
lines changed

.travis.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ php:
77
- 7.0
88

99
env:
10-
matrix:
11-
- DRIVER_VERSION=1.1.1
10+
- DRIVER_VERSION=1.1.1
11+
12+
matrix:
13+
include:
14+
- php: 5.6
15+
env: DRIVER_VERSION=1.1.1 LEGACY_DRIVER_VERSION=1.6.12
1216

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

2429
script:
2530
- ./vendor/bin/phpunit --coverage-clover=coverage.clover

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ return `0` as connection ID.
9191

9292
## MongoCollection
9393

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

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

121131
## Types
122132

lib/Alcaeus/MongoDbAdapter/AbstractCursor.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class AbstractCursor
2929
/**
3030
* @var int
3131
*/
32-
protected $batchSize;
32+
protected $batchSize = 0;
3333

3434
/**
3535
* @var Collection
@@ -66,6 +66,11 @@ abstract class AbstractCursor
6666
*/
6767
protected $startedIterating = false;
6868

69+
/**
70+
* @var int
71+
*/
72+
protected $position = 0;
73+
6974
/**
7075
* @var array
7176
*/
@@ -113,7 +118,10 @@ public function __construct(\MongoClient $connection, $ns)
113118
*/
114119
public function current()
115120
{
116-
$this->startedIterating = true;
121+
if (! $this->startedIterating) {
122+
return null;
123+
}
124+
117125
$document = $this->ensureIterator()->current();
118126
if ($document !== null) {
119127
$document = TypeConverter::toLegacy($document);
@@ -129,6 +137,10 @@ public function current()
129137
*/
130138
public function key()
131139
{
140+
if (! $this->startedIterating) {
141+
return null;
142+
}
143+
132144
return $this->ensureIterator()->key();
133145
}
134146

@@ -141,11 +153,12 @@ public function key()
141153
*/
142154
public function next()
143155
{
144-
if (!$this->startedIterating) {
156+
if (! $this->startedIterating) {
145157
$this->ensureIterator();
146158
$this->startedIterating = true;
147159
} else {
148160
$this->ensureIterator()->next();
161+
$this->position++;
149162
}
150163

151164
return $this->current();
@@ -162,6 +175,7 @@ public function rewind()
162175
// We can recreate the cursor to allow it to be rewound
163176
$this->reset();
164177
$this->startedIterating = true;
178+
$this->position = 0;
165179
$this->ensureIterator()->rewind();
166180
}
167181

@@ -172,6 +186,10 @@ public function rewind()
172186
*/
173187
public function valid()
174188
{
189+
if (! $this->startedIterating) {
190+
return false;
191+
}
192+
175193
return $this->ensureIterator()->valid();
176194
}
177195

@@ -323,11 +341,12 @@ protected function getIterationInfo()
323341
$typeString = 'STANDALONE';
324342
}
325343

344+
$cursorId = (string) $this->cursor->getId();
326345
$iterationInfo += [
327-
'id' => (string) $this->cursor->getId(),
328-
'at' => null, // @todo Complete info for cursor that is iterating
329-
'numReturned' => null, // @todo Complete info for cursor that is iterating
330-
'server' => null, // @todo Complete info for cursor that is iterating
346+
'id' => (int) $cursorId,
347+
'at' => $this->position,
348+
'numReturned' => $this->position, // This can't be obtained from the new cursor
349+
'server' => sprintf('%s:%d;-;.;%d', $this->cursor->getServer()->getHost(), $this->cursor->getServer()->getPort(), getmypid()),
331350
'host' => $this->cursor->getServer()->getHost(),
332351
'port' => $this->cursor->getServer()->getPort(),
333352
'connection_type_desc' => $typeString,

lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php

+29-12
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ class ExceptionConverter
2828
*
2929
* @return \MongoException
3030
*/
31-
public static function convertException(Exception\Exception $e, $fallbackClass = 'MongoException')
31+
public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
3232
{
33+
$message = $e->getMessage();
34+
$code = $e->getCode();
35+
3336
switch (get_class($e)) {
3437
case Exception\AuthenticationException::class:
3538
case Exception\ConnectionException::class:
@@ -40,7 +43,25 @@ public static function convertException(Exception\Exception $e, $fallbackClass =
4043

4144
case Exception\BulkWriteException::class:
4245
case Exception\WriteException::class:
43-
$class = 'MongoCursorException';
46+
$writeResult = $e->getWriteResult();
47+
48+
if ($writeResult) {
49+
$writeError = $writeResult->getWriteErrors()[0];
50+
51+
$message = $writeError->getMessage();
52+
$code = $writeError->getCode();
53+
}
54+
55+
switch ($code) {
56+
// see https://github.com/mongodb/mongo-php-driver-legacy/blob/ad3ed45739e9702ae48e53ddfadc482d9c4c7e1c/cursor_shared.c#L540
57+
case 11000:
58+
case 11001:
59+
case 12582:
60+
$class = 'MongoDuplicateKeyException';
61+
break;
62+
default:
63+
$class = 'MongoCursorException';
64+
}
4465
break;
4566

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

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

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

61-
/**
62-
* @throws \MongoException
63-
*/
64-
public static function toLegacy(Exception\Exception $e, $fallbackClass = 'MongoException')
65-
{
66-
throw self::convertException($e, $fallbackClass);
83+
return new $class($message, $code, $e);
6784
}
6885
}

lib/Mongo/MongoClient.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ public function getHosts()
191191
try {
192192
$servers = $this->manager->getServers();
193193
} catch (\MongoDB\Driver\Exception\Exception $e) {
194-
ExceptionConverter::toLegacy($e);
194+
throw ExceptionConverter::toLegacy($e);
195195
}
196196

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

201201
switch ($server->getType()) {
@@ -246,7 +246,7 @@ public function listDBs()
246246
try {
247247
$databaseInfoIterator = $this->client->listDatabases();
248248
} catch (\MongoDB\Driver\Exception\Exception $e) {
249-
ExceptionConverter::toLegacy($e);
249+
throw ExceptionConverter::toLegacy($e);
250250
}
251251

252252
$databases = [
@@ -256,7 +256,11 @@ public function listDBs()
256256
];
257257

258258
foreach ($databaseInfoIterator as $databaseInfo) {
259-
$databases['databases'][] = $databaseInfo->getName();
259+
$databases['databases'][] = [
260+
'name' => $databaseInfo->getName(),
261+
'empty' => $databaseInfo->isEmpty(),
262+
'sizeOnDisk' => $databaseInfo->getSizeOnDisk(),
263+
];
260264
$databases['totalSize'] += $databaseInfo->getSizeOnDisk();
261265
}
262266

0 commit comments

Comments
 (0)