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

Commit 69699c1

Browse files
committed
Adds tests for memcached-based checks
Updates the logic in both the memcache and memcached checks to follow current coding guidelines, particularly around multi-conditionals and importing of classes. Also adds unit tests for memcache and memcached checks, running them solely on the condition that a given env variable is true.
1 parent 51571a7 commit 69699c1

File tree

5 files changed

+94
-18
lines changed

5 files changed

+94
-18
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

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: 12 additions & 8 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,7 +31,7 @@ class Memcache extends AbstractCheck
2931
/**
3032
* @param string $host
3133
* @param int $port
32-
* @throws \InvalidArgumentException
34+
* @throws InvalidArgumentException
3335
*/
3436
public function __construct($host = '127.0.0.1', $port = 11211)
3537
{
@@ -53,7 +55,7 @@ public function __construct($host = '127.0.0.1', $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: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace ZendDiagnostics\Check;
99

10+
use Exception;
11+
use Memcached as MemcachedService;
1012
use InvalidArgumentException;
1113
use ZendDiagnostics\Result\Failure;
1214
use ZendDiagnostics\Result\Success;
@@ -29,7 +31,8 @@ class Memcached extends AbstractCheck
2931
/**
3032
* @param string $host
3133
* @param int $port
32-
* @throws \InvalidArgumentException
34+
* @throws InvalidArgumentException if host is not a string value
35+
* @throws InvalidArgumentException if port is less than 1
3336
*/
3437
public function __construct($host = '127.0.0.1', $port = 11211)
3538
{
@@ -40,7 +43,7 @@ public function __construct($host = '127.0.0.1', $port = 11211)
4043
));
4144
}
4245

43-
$port = (int)$port;
46+
$port = (int) $port;
4447
if ($port < 1) {
4548
throw new InvalidArgumentException(sprintf(
4649
'Invalid port number - expecting a positive integer',
@@ -53,7 +56,7 @@ public function __construct($host = '127.0.0.1', $port = 11211)
5356
}
5457

5558
/**
56-
* @see ZendDiagnostics\CheckInterface::check()
59+
* @see CheckInterface::check()
5760
*/
5861
public function check()
5962
{
@@ -62,14 +65,16 @@ public function check()
6265
}
6366

6467
try {
65-
$memcached = new \Memcached();
68+
$memcached = new MemcachedService();
6669
$memcached->addServer($this->host, $this->port);
6770
$stats = @$memcached->getStats();
6871

69-
if (! $stats ||
70-
! is_array($stats) ||
71-
! isset($stats[$this->host . ':' . $this->port]) ||
72-
($stats[$this->host . ':' . $this->port] === false)
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]
7378
) {
7479
// Attempt a connection to make sure that the server is really down
7580
if (! @$memcached->getLastDisconnectedServer($this->host, $this->port)) {
@@ -80,7 +85,7 @@ public function check()
8085
));
8186
}
8287
}
83-
} catch (\Exception $e) {
88+
} catch (Exception $e) {
8489
return new Failure($e->getMessage());
8590
}
8691

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)