Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit df9919e

Browse files
committed
Merge branch 'hotfix/80-mongo-connection'
Close #80
2 parents b40c2ef + 628c820 commit df9919e

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ cache:
88

99
services:
1010
- memcached
11+
- mongodb
1112
- rabbitmq
1213
- redis-server
1314

1415
env:
1516
global:
1617
- COMPOSER_ARGS="--no-interaction"
1718
- COVERAGE_DEPS="php-coveralls/php-coveralls"
19+
- TESTS_ZEND_DIAGNOSTICS_MONGO_ENABLED=true
1820
- TESTS_ZEND_DIAGNOSTICS_RABBITMQ_ENABLED=true
1921
- TESTS_ZEND_DIAGNOSTICS_REDIS_ENABLED=true
2022

@@ -23,17 +25,20 @@ matrix:
2325
- php: 5.6
2426
env:
2527
- DEPS=lowest
28+
- MONGO_LEGACY=true
2629
- TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED=true
2730
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=false
2831
- php: 5.6
2932
env:
3033
- DEPS=locked
34+
- MONGO_LEGACY=true
3135
- LEGACY_DEPS="phpunit/phpunit"
3236
- TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED=true
3337
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=false
3438
- php: 5.6
3539
env:
3640
- DEPS=latest
41+
- MONGO_LEGACY=true
3742
- TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED=true
3843
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=false
3944
- php: 7
@@ -80,12 +85,17 @@ before_install:
8085
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
8186
- if [[ $TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED == 'true' ]]; then echo "extension = memcache.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
8287
- if [[ $TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED == 'true' ]]; then echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
88+
- if [[ $MONGO_LEGACY == 'true' ]]; then echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
89+
- if [[ $MONGO_LEGACY != 'true' ]]; then pecl upgrade mongodb ; fi
90+
- if [[ $MONGO_LEGACY != 'true' ]]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
8391

8492
install:
8593
- travis_retry composer install $COMPOSER_ARGS --ignore-platform-reqs
8694
- if [[ $LEGACY_DEPS != '' ]]; then travis_retry composer update $COMPOSER_ARGS --with-dependencies $LEGACY_DEPS ; fi
8795
- if [[ $DEPS == 'latest' ]]; then travis_retry composer update $COMPOSER_ARGS ; fi
8896
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable $COMPOSER_ARGS ; fi
97+
- if [[ $MONGO_LEGACY != 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS mongodb/mongodb ; fi
98+
- stty cols 120 && php -m
8999
- stty cols 120 && composer show
90100

91101
script:

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ Releases prior to 1.2.0 did not have entries.
3434

3535
### Fixed
3636

37-
- Nothing.
37+
- [#80](https://github.com/zendframework/zenddiagnostics/pull/80) fixes how the `MongoDB\Client` instance is created when using ext-mongodb + mongodb/mongodb,
38+
ensuring it uses the provided connection URI.

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<php>
1919
<env name="TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED" value="false" />
2020
<env name="TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED" value="false" />
21+
<env name="TESTS_ZEND_DIAGNOSTICS_MONGO_ENABLED" value="false" />
2122
<env name="TESTS_ZEND_DIAGNOSTICS_RABBITMQ_ENABLED" value="false" />
2223
<env name="TESTS_ZEND_DIAGNOSTICS_REDIS_ENABLED" value="false" />
2324
</php>

src/Check/Mongo.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
namespace ZendDiagnostics\Check;
99

10+
use Exception;
11+
use MongoClient;
12+
use MongoDB\Client as MongoDBClient;
13+
use RuntimeException;
1014
use ZendDiagnostics\Result\Success;
1115
use ZendDiagnostics\Result\Failure;
1216

@@ -20,7 +24,7 @@ class Mongo extends AbstractCheck
2024
/**
2125
* @param string $connectionUri
2226
*/
23-
public function __construct($connectionUri)
27+
public function __construct($connectionUri = 'mongodb://127.0.0.1/')
2428
{
2529
$this->connectionUri = $connectionUri;
2630
}
@@ -32,8 +36,11 @@ public function check()
3236
{
3337
try {
3438
$this->getListDBs();
35-
} catch (\Exception $e) {
36-
return new Failure(sprintf('Failed to connect to MongoDB server. Reason: `%s`', $e->getMessage()));
39+
} catch (Exception $e) {
40+
return new Failure(sprintf(
41+
'Failed to connect to MongoDB server. Reason: `%s`',
42+
$e->getMessage()
43+
));
3744
}
3845

3946
return new Success();
@@ -48,12 +55,14 @@ public function check()
4855
*/
4956
private function getListDBs()
5057
{
51-
if (class_exists('\MongoDB\Client')) {
52-
return (new \MongoDB\Client($this->connectionUri))->listDatabases();
53-
} elseif (class_exists('\MongoClient')) {
54-
return (new \MongoClient($this->server))->listDBs();
58+
if (class_exists(MongoDBClient::class)) {
59+
return (new MongoDBClient($this->connectionUri))->listDatabases();
5560
}
5661

57-
throw new \RuntimeException('Neither the mongo extension or mongodb are installed');
62+
if (class_exists(MongoClient::class)) {
63+
return (new MongoClient($this->connectionUri))->listDBs();
64+
}
65+
66+
throw new RuntimeException('Neither the mongo extension or mongodb are installed');
5867
}
5968
}

test/ChecksTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ZendDiagnostics\Check\JsonFile;
2525
use ZendDiagnostics\Check\Memcache;
2626
use ZendDiagnostics\Check\Memcached;
27+
use ZendDiagnostics\Check\Mongo;
2728
use ZendDiagnostics\Check\PhpFlag;
2829
use ZendDiagnostics\Check\PhpVersion;
2930
use ZendDiagnostics\Check\ProcessRunning;
@@ -124,6 +125,21 @@ public function testMemcached()
124125
$this->assertInstanceOf(Failure::class, $result);
125126
}
126127

128+
public function testMongo()
129+
{
130+
if (getenv('TESTS_ZEND_DIAGNOSTICS_MONGO_ENABLED') !== 'true') {
131+
$this->markTestSkipped('Mongo tests are not enabled; enable them in phpunit.xml');
132+
}
133+
134+
$check = new Mongo();
135+
$result = $check->check();
136+
$this->assertInstanceOf(Success::class, $result);
137+
138+
$check = new Memcached('127.0.0.250', 9999);
139+
$result = $check->check();
140+
$this->assertInstanceOf(Failure::class, $result);
141+
}
142+
127143
public function testClassExists()
128144
{
129145
$check = new ClassExists(__CLASS__);

0 commit comments

Comments
 (0)