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

Commit b40c2ef

Browse files
committed
Merge branch 'feature/88-memcached'
Close #88
2 parents 2d0f369 + e8dfd10 commit b40c2ef

File tree

7 files changed

+208
-11
lines changed

7 files changed

+208
-11
lines changed

.travis.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,79 @@ cache:
77
- $HOME/.composer/cache
88

99
services:
10-
- redis-server
10+
- memcached
1111
- rabbitmq
12+
- redis-server
1213

1314
env:
1415
global:
1516
- COMPOSER_ARGS="--no-interaction"
1617
- COVERAGE_DEPS="php-coveralls/php-coveralls"
18+
- TESTS_ZEND_DIAGNOSTICS_RABBITMQ_ENABLED=true
19+
- TESTS_ZEND_DIAGNOSTICS_REDIS_ENABLED=true
1720

1821
matrix:
1922
include:
2023
- php: 5.6
2124
env:
2225
- DEPS=lowest
26+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED=true
27+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=false
2328
- php: 5.6
2429
env:
2530
- DEPS=locked
2631
- LEGACY_DEPS="phpunit/phpunit"
32+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED=true
33+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=false
2734
- php: 5.6
2835
env:
2936
- DEPS=latest
37+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED=true
38+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=false
3039
- php: 7
3140
env:
3241
- DEPS=lowest
42+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
3343
- php: 7
3444
env:
3545
- DEPS=locked
3646
- LEGACY_DEPS="phpunit/phpunit"
47+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
3748
- php: 7
3849
env:
3950
- DEPS=latest
51+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
4052
- php: 7.1
4153
env:
4254
- DEPS=lowest
55+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
4356
- php: 7.1
4457
env:
4558
- DEPS=locked
59+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
4660
- CS_CHECK=true
4761
- TEST_COVERAGE=true
4862
- php: 7.1
4963
env:
5064
- DEPS=latest
65+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
5166
- php: 7.2
5267
env:
5368
- DEPS=lowest
69+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
5470
- php: 7.2
5571
env:
5672
- DEPS=locked
73+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
5774
- php: 7.2
5875
env:
5976
- DEPS=latest
77+
- TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED=true
6078

6179
before_install:
6280
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
81+
- if [[ $TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED == 'true' ]]; then echo "extension = memcache.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
82+
- if [[ $TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED == 'true' ]]; then echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; fi
6383

6484
install:
6585
- travis_retry composer install $COMPOSER_ARGS --ignore-platform-reqs

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Releases prior to 1.2.0 did not have entries.
88

99
### Added
1010

11+
- [#88](https://github.com/zendframework/zenddiagnostics/pull/88) adds a new `Memcached` diagnostic check.
12+
1113
- [#89](https://github.com/zendframework/zenddiagnostics/pull/89) adds full documentation at https://docs.zendframework.com/zend-diagnostics
1214

1315
- [#89](https://github.com/zendframework/zenddiagnostics/pull/89) adds support for Guzzle 6. While support was previously

docs/book/diagnostics.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,32 @@ use ZendDiagnostics\Check\Memcache;
240240
$checkLocal = new Memcache('127.0.0.1'); // default port
241241
$checkBackup = new Memcache('10.0.30.40', 11212);
242242
```
243+
244+
## Memcached
245+
246+
Attempt to connect to the given Memcached server.
247+
248+
```php
249+
<?php
250+
use ZendDiagnostics\Check\Memcached;
251+
252+
$checkLocal = new Memcached('127.0.0.1'); // default port
253+
$checkBackup = new Memcached('10.0.30.40', 11212);
254+
```
255+
256+
### MongoDb
257+
Check if connection to MongoDb is possible
258+
259+
````php
260+
<?php
261+
use ZendDiagnostics\Check\Mongo;
262+
263+
$mongoCheck = new Mongo('mongodb://127.0.0.1:27017');
264+
// and with user/password
265+
$mongoCheck = new Mongo('mongodb://user:[email protected]:27017');
266+
````
267+
268+
243269

244270
## MongoDb
245271

phpunit.xml.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414
<directory suffix=".php">./src</directory>
1515
</whitelist>
1616
</filter>
17+
18+
<php>
19+
<env name="TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED" value="false" />
20+
<env name="TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED" value="false" />
21+
<env name="TESTS_ZEND_DIAGNOSTICS_RABBITMQ_ENABLED" value="false" />
22+
<env name="TESTS_ZEND_DIAGNOSTICS_REDIS_ENABLED" value="false" />
23+
</php>
1724
</phpunit>

src/Check/Memcache.php

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

88
namespace ZendDiagnostics\Check;
99

10+
use Exception;
11+
use Memcache as MemcacheService;
1012
use InvalidArgumentException;
1113
use ZendDiagnostics\Result\Failure;
1214
use ZendDiagnostics\Result\Success;
@@ -29,9 +31,9 @@ class Memcache extends AbstractCheck
2931
/**
3032
* @param string $host
3133
* @param int $port
32-
* @throws \InvalidArgumentException
34+
* @throws InvalidArgumentException
3335
*/
34-
public function __construct($host, $port = 11211)
36+
public function __construct($host = '127.0.0.1', $port = 11211)
3537
{
3638
if (! is_string($host)) {
3739
throw new InvalidArgumentException(sprintf(
@@ -40,7 +42,7 @@ public function __construct($host, $port = 11211)
4042
));
4143
}
4244

43-
$port = (int)$port;
45+
$port = (int) $port;
4446
if ($port < 1) {
4547
throw new InvalidArgumentException(sprintf(
4648
'Invalid port number - expecting a positive integer',
@@ -53,7 +55,7 @@ public function __construct($host, $port = 11211)
5355
}
5456

5557
/**
56-
* @see ZendDiagnostics\CheckInterface::check()
58+
* @see CheckInterface::check()
5759
*/
5860
public function check()
5961
{
@@ -62,14 +64,16 @@ public function check()
6264
}
6365

6466
try {
65-
$memcache = new \Memcache();
67+
$memcache = new MemcacheService();
6668
$memcache->addServer($this->host, $this->port);
6769
$stats = @$memcache->getExtendedStats();
6870

69-
if (! $stats ||
70-
! is_array($stats) ||
71-
! isset($stats[$this->host . ':' . $this->port]) ||
72-
($stats[$this->host . ':' . $this->port] === false)
71+
$authority = sprintf('%s:%d', $this->host, $this->port);
72+
73+
if (! $stats
74+
|| ! is_array($stats)
75+
|| ! isset($stats[$authority])
76+
|| false === $stats[$authority]
7377
) {
7478
// Attempt a connection to make sure that the server is really down
7579
if (! @$memcache->connect($this->host, $this->port)) {
@@ -80,7 +84,7 @@ public function check()
8084
));
8185
}
8286
}
83-
} catch (\Exception $e) {
87+
} catch (Exception $e) {
8488
return new Failure($e->getMessage());
8589
}
8690

src/Check/Memcached.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-diagnostics for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-diagnostics/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendDiagnostics\Check;
9+
10+
use Exception;
11+
use Memcached as MemcachedService;
12+
use InvalidArgumentException;
13+
use ZendDiagnostics\Result\Failure;
14+
use ZendDiagnostics\Result\Success;
15+
16+
/**
17+
* Check if MemCached extension is loaded and given server is reachable.
18+
*/
19+
class Memcached extends AbstractCheck
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected $host;
25+
26+
/**
27+
* @var int
28+
*/
29+
protected $port;
30+
31+
/**
32+
* @param string $host
33+
* @param int $port
34+
* @throws InvalidArgumentException if host is not a string value
35+
* @throws InvalidArgumentException if port is less than 1
36+
*/
37+
public function __construct($host = '127.0.0.1', $port = 11211)
38+
{
39+
if (! is_string($host)) {
40+
throw new InvalidArgumentException(sprintf(
41+
'Cannot use %s as host - expecting a string',
42+
gettype($host)
43+
));
44+
}
45+
46+
$port = (int) $port;
47+
if ($port < 1) {
48+
throw new InvalidArgumentException(sprintf(
49+
'Invalid port number - expecting a positive integer',
50+
gettype($host)
51+
));
52+
}
53+
54+
$this->host = $host;
55+
$this->port = $port;
56+
}
57+
58+
/**
59+
* @see CheckInterface::check()
60+
*/
61+
public function check()
62+
{
63+
if (! class_exists('Memcached', false)) {
64+
return new Failure('Memcached extension is not loaded');
65+
}
66+
67+
try {
68+
$memcached = new MemcachedService();
69+
$memcached->addServer($this->host, $this->port);
70+
$stats = @$memcached->getStats();
71+
72+
$authority = sprintf('%s:%d', $this->host, $this->port);
73+
74+
if (! $stats
75+
|| ! is_array($stats)
76+
|| ! isset($stats[$authority])
77+
|| false === $stats[$authority]
78+
) {
79+
// Attempt a connection to make sure that the server is really down
80+
if (! @$memcached->getLastDisconnectedServer($this->host, $this->port)) {
81+
return new Failure(sprintf(
82+
'No memcached server running at host %s on port %s',
83+
$this->host,
84+
$this->port
85+
));
86+
}
87+
}
88+
} catch (Exception $e) {
89+
return new Failure($e->getMessage());
90+
}
91+
92+
return new Success(sprintf(
93+
'Memcached server running at host %s on port %s',
94+
$this->host,
95+
$this->port
96+
));
97+
}
98+
}

test/ChecksTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use ZendDiagnostics\Check\ExtensionLoaded;
2323
use ZendDiagnostics\Check\IniFile;
2424
use ZendDiagnostics\Check\JsonFile;
25+
use ZendDiagnostics\Check\Memcache;
26+
use ZendDiagnostics\Check\Memcached;
2527
use ZendDiagnostics\Check\PhpFlag;
2628
use ZendDiagnostics\Check\PhpVersion;
2729
use ZendDiagnostics\Check\ProcessRunning;
@@ -61,6 +63,10 @@ public function testCpuPerformance()
6163

6264
public function testRabbitMQ()
6365
{
66+
if (getenv('TESTS_ZEND_DIAGNOSTICS_RABBITMQ_ENABLED') !== 'true') {
67+
$this->markTestSkipped('RabbitMQ tests are not enabled; enable them in phpunit.xml');
68+
}
69+
6470
$check = new RabbitMQ();
6571
$result = $check->check();
6672
$this->assertInstanceOf(Success::class, $result);
@@ -75,6 +81,10 @@ public function testRabbitMQ()
7581

7682
public function testRedis()
7783
{
84+
if (getenv('TESTS_ZEND_DIAGNOSTICS_REDIS_ENABLED') !== 'true') {
85+
$this->markTestSkipped('Redis tests are not enabled; enable them in phpunit.xml');
86+
}
87+
7888
$check = new Redis();
7989
$result = $check->check();
8090
$this->assertInstanceOf(Success::class, $result);
@@ -84,6 +94,36 @@ public function testRedis()
8494
$check->check();
8595
}
8696

97+
public function testMemcache()
98+
{
99+
if (getenv('TESTS_ZEND_DIAGNOSTICS_MEMCACHE_ENABLED') !== 'true') {
100+
$this->markTestSkipped('Memcache tests are not enabled; enable them in phpunit.xml');
101+
}
102+
103+
$check = new Memcache();
104+
$result = $check->check();
105+
$this->assertInstanceOf(Success::class, $result);
106+
107+
$check = new Memcache('127.0.0.250', 9999);
108+
$result = $check->check();
109+
$this->assertInstanceOf(Failure::class, $result);
110+
}
111+
112+
public function testMemcached()
113+
{
114+
if (getenv('TESTS_ZEND_DIAGNOSTICS_MEMCACHED_ENABLED') !== 'true') {
115+
$this->markTestSkipped('Memcached tests are not enabled; enable them in phpunit.xml');
116+
}
117+
118+
$check = new Memcached();
119+
$result = $check->check();
120+
$this->assertInstanceOf(Success::class, $result);
121+
122+
$check = new Memcached('127.0.0.250', 9999);
123+
$result = $check->check();
124+
$this->assertInstanceOf(Failure::class, $result);
125+
}
126+
87127
public function testClassExists()
88128
{
89129
$check = new ClassExists(__CLASS__);

0 commit comments

Comments
 (0)