Skip to content

Commit

Permalink
Merge pull request #3 from DavidGoodwin/github-action-add-more-phps
Browse files Browse the repository at this point in the history
GitHub action add more phps
  • Loading branch information
DavidGoodwin authored Mar 4, 2023
2 parents cd58c03 + 1716baf commit ecd002b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 21 deletions.
89 changes: 77 additions & 12 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,63 @@ name: GitHub Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]


jobs:
build:
sanity-check:
runs-on: ubuntu-22.04
env:
key: cache-v1
extensions: apc, redis, apcu, memcache, memcached

runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3
- name: Setup cache environment for ${{ matrix.php-versions }}
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: 7.4
extensions: ${{ env.extensions }}
key: ${{ env.key }}

- name: Cache extensions
uses: actions/cache@v3
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}

- name: Setup PHP {{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer
extensions: ${{ env.extensions }}
ini-values: apc.enable_cli=1

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: format check etc
run: composer check-format

- name: phpstan
run: composer phpstan

- name: psalm
run: composer psalm


unit-test:
runs-on: ubuntu-22.04
needs: [sanity-check]
env:
key: cache-v1
extensions: apc, redis, apcu, memcache, memcached

services:
redis:
Expand All @@ -23,28 +72,44 @@ jobs:
- 11211:11211
strategy:
matrix:
php-versions: [ '7.2', '7.3', '7.4', '8.0' ]
php-versions: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]

steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Setup cache environment for ${{ matrix.php-versions }}
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ matrix.php-versions }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}

- name: Cache extensions
uses: actions/cache@v3
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}

- name: Setup PHP
- name: Setup PHP {{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: composer
extensions: apc, redis, apcu, memcache, memcached
extensions: ${{ env.extensions }}
ini-values: apc.enable_cli=1

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: psalm
run: composer psalm

- name: Run test suite
run: composer build
run: composer test
env:
REDIS_HOST: localhost
MEMCACHE_HOST: localhost
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "palepurple/rate-limit",
"description": "PHP rate limiting library with Token Bucket Algorithm, originally touhonoob/rate-limit",
"require": {
"php": ">=5.6"
"php": ">=5.6 | 7.* | ^8.1 | ^8.2 "
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.7",
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config file:///home/david/src/RateLimit/vendor/vimeo/psalm/config.xsd"
Expand Down
13 changes: 11 additions & 2 deletions src/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,32 @@ public function __construct(\Redis $redis)
* @param float $value
* @param int $ttl
* @return bool
* @throws \RedisException
*/
public function set($key, $value, $ttl)
{
return $this->redis->set($key, (string)$value, $ttl);
$ret = $this->redis->set($key, (string)$value, $ttl);
return $ret == true; /* redis returns true OR \Redis (when in multimode). */
}

/**
* @return float
* @param string $key
* @throws \RedisException
*/
public function get($key)
{
return (float)$this->redis->get($key);
$ret = $this->redis->get($key);
if (is_numeric($ret)) {
return (float) $ret;
}
return (float) 0;
}

/**
* @param string $key
* @return bool
* @throws \RedisException
*/
public function exists($key)
{
Expand All @@ -51,6 +59,7 @@ public function exists($key)
/**
* @param string $key
* @return bool
* @throws \RedisException
*/
public function del($key)
{
Expand Down
10 changes: 5 additions & 5 deletions tests/RateLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public function testCheckMemcached()
}


private function check($adapter)
private function check(Adapter $adapter)
{
$label = uniqid("label", true); // should stop storage conflicts if tests are running in parallel.
$rateLimit = $this->getRateLimit($adapter);
$label = phpversion() . '-' . uniqid("label", true); // should stop storage conflicts if tests are running in parallel.
$rateLimit = $this->getRateLimit($adapter, $label);

$rateLimit->purge($label); // make sure a previous failed test doesn't mess up this one .

Expand All @@ -153,8 +153,8 @@ private function check($adapter)
$this->assertTrue($rateLimit->check($label));
}

private function getRateLimit(Adapter $adapter)
private function getRateLimit(Adapter $adapter, $label)
{
return new RateLimit(self::NAME . uniqid(), self::MAX_REQUESTS, self::PERIOD, $adapter);
return new RateLimit($label, self::MAX_REQUESTS, self::PERIOD, $adapter);
}
}

0 comments on commit ecd002b

Please sign in to comment.