Skip to content

Commit

Permalink
Add Phan
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuakiM committed Sep 24, 2017
1 parent 69af2f8 commit 1d8aaba
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 68 deletions.
14 changes: 14 additions & 0 deletions .phan/config.php
@@ -0,0 +1,14 @@
<?php

return [
'directory_list' => [
'src',
'tests',
'vendor/phpunit/phpunit/src'
],
'exclude_analysis_directory_list' => [
'tests/bin',
'vendor'
],
'skip_slow_php_options_warning' => true
];
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,6 @@ services:

before_script:
- echo 'extension=redis.so' >> $HOME/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer require friendsofphp/php-cs-fixer
- composer install --no-progress --no-ansi --no-interaction --profile

script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -21,7 +21,7 @@ try {

$clients->set('key', 1); // normal phpredis functions.
$clients->get('key');
} catch (ReddishException $e) {
} catch (\RedisException $e) {
...
}
```
Expand Down
2 changes: 1 addition & 1 deletion apigen.yml
@@ -1,4 +1,4 @@
parameters:
title: "reddish"
baseUrl: "http://kazuakim.github.io/reddish/"
overwrite: true # bool
overwrite: true
8 changes: 6 additions & 2 deletions composer.json
Expand Up @@ -16,10 +16,14 @@
"php": "^7.0",
"ext-redis": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.6",
"phpunit/phpunit": "^5.7"
},
"scripts": {
"coverage": "php tests/bin/coveralls.phar --config=.coveralls.yml --coverage_clover=/tmp/build/coverage/clover.xml --verbose",
"csfixer": "php vendor/bin/php-cs-fixer fix --dry-run --verbose",
"document": "apigen generate src tests --destination docs",
"csfixer": "php vendor/bin/php-cs-fixer fix --dry-run",
"document": "php vendor/bin/apigen generate --source src --destination docs",
"quality": "php tests/bin/ocular.phar code-coverage:upload --format=php-clover /tmp/build/coverage/clover.xml --verbose",
"test": "phpunit --colors=always --coverage-clover=/tmp/build/coverage/clover.xml --verbose"
},
Expand Down
84 changes: 56 additions & 28 deletions src/Clients.php
Expand Up @@ -5,6 +5,9 @@
/**
* Clients.
*
* @property array $config
* @property array $_defaultConfig
*
* @copyright KazuakiM <kazuaki_mabuchi_to_go@hotmail.co.jp>
* @author KazuakiM <kazuaki_mabuchi_to_go@hotmail.co.jp>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
Expand All @@ -14,71 +17,96 @@
class Clients extends \Redis //{{{
{
protected $config;
private static $_defaultConfig = [
'host' => null, //can be a host, or the path to a unix domain socket
private $_defaultConfig = [
'host' => null, //can be a host, or the path to a unix domain socket
'port' => 6379,
'timeout' => 0.0, //value in seconds (optional, default is 0 meaning unlimited)
'reserved' => null, //should be NULL if retry_interval is specified
'retry_interval' => null, //value in milliseconds
'persistent_id' => '', //identity for the requested persistent connection
'timeout' => 0.0, //value in seconds (optional, default is 0 meaning unlimited)
'reserved' => null, //should be NULL if retry_interval is specified
'persistent_id' => '', //identity for the requested persistent connection
'password' => null,
'serializer' => \Redis::SERIALIZER_NONE,
'persistent' => false, //default is connect
'persistent' => false, //default is connect
];

/**
* __construct.
*
* @param array $config
*
* @throws \RedisException
*/
public function __construct(array $config) //{{{
{
$this->config = array_merge(self::$_defaultConfig, $config);
$this->config = array_merge($this->_defaultConfig, $config);

$this->connection();
} //}}}

public function connection() //{{{
/**
* connection.
*
* @throws \RedisException
*
* @return \Kazuakim\Reddish\Clients
*/
public function connection(): \Kazuakim\Reddish\Clients //{{{
{
// connect
if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['reserved'], $this->config['retry_interval'])) {
throw new ReddishException('connect errored.');
} elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['persistent_id'], $this->config['retry_interval'])) {
throw new ReddishException('pconnect errored.');
if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['reserved'])) {
throw new \RedisException('connect errored.');
} elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['persistent_id'])) {
throw new \RedisException('pconnect errored.');
}

// auth
if (0 < strlen($this->config['password']) && !$this->auth($this->config['password'])) {
throw new ReddishException('auth errored.');
if (0 < strlen($this->config['password']) && !@$this->auth($this->config['password'])) {
throw new \RedisException('auth errored.');
}

// serializer
$this->setSerializer($this->config['serializer']);

return $this;
} //}}}

public function setSerializer($serializer) //{{{
/**
* setSerializer.
*
* @param int $serializer
*
* @throws \Expectation
*
* @return \Kazuakim\Reddish\Clients
*/
public function setSerializer($serializer): \Kazuakim\Reddish\Clients //{{{
{
assert(in_array($serializer, self::_getSerializerArray(), true), 'serializer setting errored.');

if (!$this->setOption(\Redis::OPT_SERIALIZER, $serializer)) {
throw new ReddishException('serializer errored.');
}
$this->setOption(\Redis::OPT_SERIALIZER, $serializer);

return $this;
} //}}}

/**
* close.
*/
public function close() //{{{
{
if ($this->config['persistent']) {
parent::close();
}
} //}}}

public function ping() //{{{
{
try {
parent::ping();
} catch (\RedisException $e) {
throw new ReddishException($e->getMessage());
}
return;
} //}}}

/**
* _getSerializerArray.
*
* @return array
*/
private static function _getSerializerArray(): array //{{{
{
if (extension_loaded('igbinary') === false) {
if (false === extension_loaded('igbinary')) {
return [
\Redis::SERIALIZER_NONE,
\Redis::SERIALIZER_PHP,
Expand Down
14 changes: 0 additions & 14 deletions src/ReddishException.php

This file was deleted.

82 changes: 61 additions & 21 deletions tests/ClientsTest.php
Expand Up @@ -5,6 +5,9 @@
/**
* ClientsTest.
*
* @property array $_defaultConfig
* @property object $_clients;
*
* @copyright KazuakiM <kazuaki_mabuchi_to_go@hotmail.co.jp>
* @author KazuakiM <kazuaki_mabuchi_to_go@hotmail.co.jp>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
Expand All @@ -14,17 +17,16 @@
class ClientsTest extends \PHPUnit\Framework\TestCase //{{{
{
// Class variable {{{
private static $_defaultConfig = [
private $_defaultConfig = [
// 'connect' paramater
'host' => '127.0.0.1', //can be a host, or the path to a unix domain socket
'host' => '127.0.0.1', //can be a host, or the path to a unix domain socket
'port' => 6379,
'timeout' => 1.0, //value in seconds (optional, default is 0 meaning unlimited)
'reserved' => null, //should be NULL if retry_interval is specified
'retry_interval' => null, //value in milliseconds
'read_timeout' => 1.0, //value in seconds (optional, default is 0 meaning unlimited)
'timeout' => 1.0, //value in seconds (optional, default is 0 meaning unlimited)
'reserved' => null, //should be NULL if retry_interval is specified
'read_timeout' => 1.0, //value in seconds (optional, default is 0 meaning unlimited)

// 'pconnect' paramater
'persistent_id' => '', //identity for the requested persistent connection
'persistent_id' => '', //identity for the requested persistent connection

// 'auth' paramater
'password' => null,
Expand All @@ -33,73 +35,111 @@ class ClientsTest extends \PHPUnit\Framework\TestCase //{{{
'serializer' => \Redis::SERIALIZER_NONE,

// 'connect' or 'pconnect'
'persistent' => false, //default is connect
'persistent' => false, //default is connect
];

private $_clients;
//}}}

/**
* setUp.
*/
protected function setUp() //{{{
{
$this->_clients = new Clients($this->_defaultConfig);
} //}}}

/**
* tearDown.
*/
protected function tearDown() //{{{
{
unset($this->_clients);
} //}}}

/**
* @expectedException \Kazuakim\Reddish\ReddishException
* testConnectionPersistentError.
*
* @expectedException \RedisException
* @expectedExceptionCode 0
* @expectedExceptionMessage connect errored.
*/
public function testConnectionPersistentError() //{{{
{
$config = self::$_defaultConfig;
$config = $this->_defaultConfig;
$config['host'] = '127.0.0.2';
$clients = new Clients($config);
} //}}}

/**
* @expectedException \Kazuakim\Reddish\ReddishException
* testConnectionError.
*
* @expectedException \RedisException
* @expectedExceptionCode 0
* @expectedExceptionMessage pconnect errored.
*/
public function testConnectionError() //{{{
{
$config = self::$_defaultConfig;
$config = $this->_defaultConfig;
$config['host'] = '127.0.0.2';
$config['persistent'] = true;
$clients = new Clients($config);
} //}}}

/**
* @expectedException \Kazuakim\Reddish\ReddishException
* testPingError.
*
* @expectedException \RedisException
* @expectedExceptionCode 0
* @expectedExceptionMessage Connection closed
*/
public function testPingError() //{{{
{
$config = self::$_defaultConfig;
$config = $this->_defaultConfig;
$config['persistent'] = true;
$clients = new Clients($config);
$clients->close();
$clients->ping();
} //}}}

/**
* testConnection.
*/
public function testConnection() //{{{
{
$config = self::$_defaultConfig;
$this->assertTrue($this->_clients->isConnected());

$config = $this->_defaultConfig;
$config['persistent'] = true;
$clients = new Clients($config);
$this->assertTrue($clients->isConnected());
$clients->ping();
$clients->close();

unset($clients);
} //}}}

/**
* testIsConnected.
*/
public function testIsConnected() //{{{
{
$config = $this->_defaultConfig;
$config['persistent'] = true;
$clients = new Clients($config);
$this->assertTrue($clients->isConnected());
$clients->ping();
$clients->close();
unset($clients);

$this->assertFalse($clients->isConnected());
$clients->close();
} //}}}

/**
* testSave.
*/
public function testSave() //{{{
{
$config = self::$_defaultConfig;
$clients = new Clients($config);
$clients->set('key', 1);
$this->assertSame('1', $clients->get('key'));
$this->_clients->set('key', '1');
$this->assertSame('1', $this->_clients->get('key'));
} //}}}
} //}}}

0 comments on commit 1d8aaba

Please sign in to comment.