Skip to content

Commit

Permalink
Merge pull request #1 from dealnews/next
Browse files Browse the repository at this point in the history
Updated to build and test on Github
  • Loading branch information
brianlmoon authored Jun 11, 2024
2 parents 04cd879 + 30f2ef1 commit 1a9ef2c
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 89 deletions.
22 changes: 17 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ jobs:
services:

# Label used to access the service container
memcached-sandbox:
memcached:
# Docker Hub image
image: memcached:latest
ports:
- 11211:11211

# Label used to access the service container
redis-sandbox:
redis:
# Docker Hub image
image: redis:7.2
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps port 6379 on service container to the host
- 6379:6379

steps:

Expand All @@ -41,9 +52,10 @@ jobs:
- name: PHPUnit tests
uses: php-actions/phpunit@v3
with:
php_extensions: "pcov yaml"
php_extensions: "pcov yaml memcached"
version: "9.6"
php_version: ${{ matrix.php-versions }}
group: unit

- name: Run Phan
uses: k1LoW/phan-action@v0
# - name: Run Phan
# uses: k1LoW/phan-action@v0
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
'functions_opening_brace' => 'same_line',
],
'constant_case' => [
'case' => 'upper'
'case' => 'lower'
],
'lowercase_keywords' => true,
'no_extra_blank_lines' => [
'tokens' => [
'extra',
Expand Down
2 changes: 1 addition & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Cache {
*
* @throws \LogicException Thrown when the cache type is invalid
*/
public static function factory(string $cluster, ?GetConfig $config = NULL): CacheInterface {
public static function factory(string $cluster, ?GetConfig $config = null): CacheInterface {
$config ??= GetConfig::init();
$type = $config->get("caching.cache.{$cluster}.type");

Expand Down
16 changes: 8 additions & 8 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class File implements CacheInterface {
public function __construct(string $cluster) {
$this->dir = sys_get_temp_dir()."/caching/{$cluster}";
if (!file_exists($this->dir)) {
mkdir($this->dir, 0o775, TRUE);
mkdir($this->dir, 0775, true);
}
}

Expand Down Expand Up @@ -61,10 +61,10 @@ public static function init(string $cluster): File {
* @param int $expire Expiration in seconds from now or unix timestamp
*/
public function add(string $key, $var, int $expire = 0): bool {
$return = FALSE;
$return = false;

$value = $this->get($key);
if (FALSE === $value) {
if (false === $value) {
$return = $this->set($key, $var, $expire);
}

Expand All @@ -79,10 +79,10 @@ public function add(string $key, $var, int $expire = 0): bool {
* @param int $expire Expiration in seconds from now or unix timestamp
*/
public function replace(string $key, $var, int $expire = 0): bool {
$return = FALSE;
$return = false;

$value = $this->get($key);
if (FALSE !== $value) {
if (false !== $value) {
$return = $this->set($key, $var, $expire);
}

Expand All @@ -107,7 +107,7 @@ public function set(string $key, $var, int $expire = 0): bool {
$key = $this->fixKey($key);
$success = file_put_contents($this->dir.'/'.$key, serialize($struct));

return FALSE !== $success;
return false !== $success;
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ public function delete(string $key): bool {
unlink($this->dir.'/'.$key);
}

return TRUE;
return true;
}

/**
Expand Down Expand Up @@ -210,7 +210,7 @@ public function fixKey(string $key): string {
*/
protected function realGet(string $key) {
$key = $this->fixKey($key);
$value = FALSE;
$value = false;
if (file_exists($this->dir.'/'.$key)) {
$struct = unserialize(file_get_contents($this->dir.'/'.$key));
if (
Expand Down
8 changes: 4 additions & 4 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Redis implements CacheInterface {
* is something stored in an ini file or an
* environment variable.
*/
public function __construct(string $cluster, ?GetConfig $config = NULL) {
public function __construct(string $cluster, ?GetConfig $config = null) {
$this->config = $config ?? GetConfig::init();

$servers = $this->getServerList($cluster);
Expand All @@ -50,7 +50,7 @@ public function __construct(string $cluster, ?GetConfig $config = NULL) {
}

foreach ($servers as $server) {
if (FALSE === strpos($server, '://')) {
if (false === strpos($server, '://')) {
$server = "tcp://{$server}";
}

Expand Down Expand Up @@ -210,7 +210,7 @@ protected function getServerList(string $cluster): array {
*/
protected function getOptions(string $cluster): array {
$options = [
'exceptions' => FALSE,
'exceptions' => false,
];

$possible_options = [
Expand All @@ -226,7 +226,7 @@ protected function getOptions(string $cluster): array {

foreach ($possible_options as $opt) {
$value = $this->config->get("caching.redis.{$cluster}.{$opt}");
if (NULL !== $value) {
if (null !== $value) {
if ('password' === $opt || 'username' === $opt) {
$options['parameters'][$opt] = $value;
} else {
Expand Down
8 changes: 3 additions & 5 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
use PHPUnit\Framework\TestCase;

abstract class AbstractTestCase extends TestCase {
/**
* @group unit
*/

protected function interfaceTest(CacheInterface $object) {
$key = 'testing_'.uniqid();
$var = rand(1, 100);
Expand Down Expand Up @@ -54,7 +52,7 @@ protected function interfaceTest(CacheInterface $object) {

$object->delete($key);
$this->assertEquals(
FALSE,
false,
$object->get($key)
);

Expand All @@ -80,7 +78,7 @@ protected function badKeyTest(CacheInterface $object) {
$result = $object->set($key, $key);

$this->assertEquals(
TRUE,
true,
$result
);

Expand Down
6 changes: 3 additions & 3 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CacheTest extends TestCase {
* @param null|mixed $exception
* @param null|mixed $code
*/
public function testFactory($expect, $type, $servers = NULL, $cluster = 'test', $exception = NULL, $code = NULL) {
public function testFactory($expect, $type, $servers = null, $cluster = 'test', $exception = null, $code = null) {
if (!empty($exception)) {
$this->expectException($exception);
$this->expectExceptionCode($code);
Expand Down Expand Up @@ -89,7 +89,7 @@ public function factoryData() {
'Bad Cluster' => [
File::class,
'',
NULL,
null,
'foo',
\LogicException::class,
1,
Expand All @@ -98,7 +98,7 @@ public function factoryData() {
'Bad Type' => [
File::class,
'asdf',
NULL,
null,
'test',
\LogicException::class,
2,
Expand Down
43 changes: 25 additions & 18 deletions tests/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@
* @coversNothing
*/
class MemcachedTest extends AbstractTestCase {
public function setUp(): void {
if (!class_exists('\\Memcached')) {
$this->markTestSkipped('This test relies on pecl-memcached');
}

// loop and try to connect as the
// sandbox can take a bit to start up
$tries = 20;
for ($x = 1; $x <= $tries; ++$x) {
$object = new Memcached('test');
$success = $object->set('setup_test', 1);
if ($success) {
break;
}
if ($x < $tries) {
fwrite(STDERR, "Waiting for Memcached to start...\n");
sleep(5);
} else {
$this->assertTrue($success);
public static function setUpBeforeClass(): void {

if (RUN_FUNCTIONAL) {
// loop and try to connect as the
// sandbox can take a bit to start up
$tries = 5;
for ($x = 1; $x <= $tries; ++$x) {
$object = new Memcached('test');
$success = $object->set('setup_test', 1);
if ($success) {
break;
}
if ($x < $tries) {
fwrite(STDERR, "Waiting for Memcached to start (try {$x})...\n");
sleep(5);
}
}
}
}
Expand All @@ -52,16 +50,25 @@ public function testKeyFix() {
$this->assertEquals('long_ord32_key_ord32_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_sha129657c827683a98bac548732c683c3c52d777807', $value);
}

/**
* @group unit
*/
public function testBadCluster() {
$this->expectException(\Exception::class);
$redis = new Memcached('badname');
}

/**
* @group functional
*/
public function testInterface() {
$object = Memcached::init('test');
$this->interfaceTest($object);
}

/**
* @group functional
*/
public function testBadKey() {
$object = new Memcached('test');
$this->badKeyTest($object);
Expand Down
60 changes: 34 additions & 26 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,43 @@
use DealNews\GetConfig\GetConfig;
use Predis\Connection\ConnectionException;

/**
* @internal
*
* @coversNothing
*/
class RedisTest extends AbstractTestCase {
public function setUp(): void {
// loop and try to connect as the
// sandbox can take a bit to start up
$tries = 20;
for ($x = 1; $x <= $tries; ++$x) {
try {
$object = new Redis('test');
$success = $object->set('setup_test', 1);
} catch (ConnectionException $e) {
_debug($e->getMessage());
$success = FALSE;
}
if ($success) {
break;
}
if ($x < $tries) {
fwrite(STDERR, "Waiting for Redis to start...\n");
sleep(5);
} else {
$this->assertTrue($success);

public static function setUpBeforeClass(): void {

if (RUN_FUNCTIONAL) {
// loop and try to connect as the
// sandbox can take a bit to start up
$tries = 5;
for ($x = 1; $x <= $tries; ++$x) {
try {
$object = new Redis('test');
$success = $object->set('setup_test', 1);
} catch (ConnectionException $e) {
$success = false;
}
if ($success) {
break;
}
if ($x < $tries) {
fwrite(STDERR, "Waiting for Redis to start (try {$x})...\n");
sleep(5);
}
}
}
}

/**
* @group unit
*/
public function testBadCluster() {
$this->expectException(\Exception::class);
$redis = new Redis('badname');
}

/**
* @group unit
*/
public function testGetOptions() {
putenv('CACHING_REDIS_TEST2_SERVERS=127.0.0.1');
putenv('CACHING_REDIS_TEST2_USERNAME=foo');
Expand All @@ -65,7 +67,7 @@ public function getOptions(string $cluster): array {

$this->assertEquals(
[
'exceptions' => FALSE,
'exceptions' => false,
'prefix' => 'bar',
'replication' => 'sentinel',
'parameters' => [
Expand All @@ -77,11 +79,17 @@ public function getOptions(string $cluster): array {
);
}

/**
* @group functional
*/
public function testInterface() {
$object = Redis::init('test');
$this->interfaceTest($object);
}

/**
* @group functional
*/
public function testBadKey() {
$object = new Redis('test');
$this->badKeyTest($object);
Expand Down
Loading

0 comments on commit 1a9ef2c

Please sign in to comment.