Fix bootstrap.php for tests

This commit is contained in:
Daniel Mason 2019-06-02 22:21:42 +12:00
parent d702f498bf
commit 3b4d2e4c1f
6 changed files with 58 additions and 77 deletions

View File

@ -4,16 +4,18 @@
# Install git (the php image doesn't have it) which is required by composer # Install git (the php image doesn't have it) which is required by composer
apt-get update -yq apt-get update -yq
apt-get install git redis-server -y apt-get install git wget -y
# Install phpunit, the tool that we will use for testing pecl install -o -f redis \
curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar && rm -rf /tmp/pear \
chmod +x /usr/local/bin/phpunit && docker-php-ext-enable redis
docker-php-ext-install pcntl
# Install mysql driver wget https://getcomposer.org/composer.phar
# Here you can install any other extension that you need php composer.phar install
docker-php-ext-install redis
services:
- redis:latest
# Test PHP 7.0 # Test PHP 7.0
test:7.0: test:7.0:
@ -21,16 +23,20 @@ test:7.0:
before_script: before_script:
- *docker_boostrap - *docker_boostrap
script: script:
- curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-6.5.9.phar
- chmod +x /usr/local/bin/phpunit
- phpunit --configuration phpunit.xml.dist - phpunit --configuration phpunit.xml.dist
tags: tags:
- docker - docker
# Test PHP 7.2 # Test PHP 7.1
test:7.1: test:7.1:
image: php:7.1 image: php:7.1
before_script: before_script:
- *docker_boostrap - *docker_boostrap
script: script:
- curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-7.5.9.phar
- chmod +x /usr/local/bin/phpunit
- phpunit --configuration phpunit.xml.dist - phpunit --configuration phpunit.xml.dist
tags: tags:
- docker - docker
@ -41,6 +47,20 @@ test:7.2:
before_script: before_script:
- *docker_boostrap - *docker_boostrap
script: script:
- curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
- chmod +x /usr/local/bin/phpunit
- phpunit --configuration phpunit.xml.dist - phpunit --configuration phpunit.xml.dist
tags: tags:
- docker - docker
# Test PHP 7.3
test:7.3:
image: php:7.3
before_script:
- *docker_boostrap
script:
- curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit.phar
- chmod +x /usr/local/bin/phpunit
- phpunit --configuration phpunit.xml.dist
tags:
- docker

View File

@ -1,3 +1,7 @@
## 1.4.4 (2019-06-02)
- Updated tests to run on GitLab CI.
- Can now run tests locally using `gitlab-runner exec docker test:7.0`
## 1.4.3 (2018-07-16) ## 1.4.3 (2018-07-16)
- Updated README to include supervisor configuration. - Updated README to include supervisor configuration.
- Change logfile date format to `%Y-%m-%d %T`. - Change logfile date format to `%Y-%m-%d %T`.

View File

@ -34,9 +34,15 @@ not exit with a status code as 0
* Has built in support for `setUp` and `tearDown` methods, called * Has built in support for `setUp` and `tearDown` methods, called
pre and post jobs pre and post jobs
On top of the original fork (chrisboulton/php-resque) I have added:
* Custom log levels
* PHP7.0+ compatibility
## Requirements ## ## Requirements ##
* PHP 7.0+ (May work with 5.6+, Untested) * PHP 7.0+
* phpredis * phpredis
* Redis 2.2+ * Redis 2.2+

View File

@ -1,6 +1,6 @@
{ {
"name": "idanoo/php-resque", "name": "idanoo/php-resque",
"version": "1.4.3", "version": "1.4.4",
"type": "library", "type": "library",
"description": "Redis backed library for creating background jobs and processing them later. Based on resque for Ruby. Originally forked from chrisboulton/php-resque.", "description": "Redis backed library for creating background jobs and processing them later. Based on resque for Ruby. Originally forked from chrisboulton/php-resque.",
"keywords": ["job", "background", "redis", "resque"], "keywords": ["job", "background", "redis", "resque"],

View File

@ -21,8 +21,10 @@ class Resque_Tests_TestCase extends PHPUnit\Framework\TestCase
public function setUp() public function setUp()
{ {
// Setup redis connection for testing. // Setup redis connection for testing.
$this->redis = new Credis_Client('localhost', '6379'); global $redisTestServer;
Resque::setBackend('localhost');
$this->redis = new Credis_Client($redisTestServer, '6379');
Resque::setBackend($redisTestServer);
$this->redis->flushAll(); $this->redis->flushAll();
} }
} }

View File

@ -3,82 +3,30 @@
* Resque test bootstrap file - sets up a test environment. * Resque test bootstrap file - sets up a test environment.
* *
* @package Resque/Tests * @package Resque/Tests
* @author Chris Boulton <chris@bigcommerce.com> * @author Daniel Mason <daniel@m2.nz>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
$loader = require __DIR__ . '/../vendor/autoload.php'; $loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add('Resque_Tests', __DIR__); $loader->add('Resque_Tests', __DIR__);
define('TEST_MISC', realpath(__DIR__ . '/misc/')); # Redis configuration
define('REDIS_CONF', TEST_MISC . '/redis.conf'); global $redisTestServer;
$redisTestServer = getenv("REDIS_SERVER") ?? "redis";
Resque::setBackend($redisTestServer);
// Attempt to start our own redis instance for tesitng. # Check Redis is accessable locally
exec('which redis-server', $output, $returnVar); try {
if ($returnVar != 0) { $redisTest = new Resque_Redis($redisTestServer);
echo "Cannot find redis-server in path. Please make sure redis is installed.\n"; } catch (Exception $e) {
exit(1); throw new Exception("Unable to connect to redis. Please check there is a redis-server running.");
} }
$redisTest = null;
exec('cd ' . TEST_MISC . '; redis-server ' . REDIS_CONF, $output, $returnVar);
usleep(500000);
if ($returnVar != 0) {
echo "Cannot start redis-server.\n";
exit(1);
}
// Get redis port from conf
$config = file_get_contents(REDIS_CONF);
if (!preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches)) {
echo "Could not determine redis port from redis.conf";
exit(1);
}
Resque::setBackend('localhost:' . $matches[1]);
// Shutdown
function killRedis($pid)
{
if (getmypid() !== $pid) {
return; // don't kill from a forked worker
}
$config = file_get_contents(REDIS_CONF);
if (!preg_match('#^\s*pidfile\s+([^\s]+)#m', $config, $matches)) {
return;
}
$pidFile = TEST_MISC . '/' . $matches[1];
if (file_exists($pidFile)) {
$pid = trim(file_get_contents($pidFile));
posix_kill((int)$pid, 9);
if (is_file($pidFile)) {
unlink($pidFile);
}
}
// Remove the redis database
if (!preg_match('#^\s*dir\s+([^\s]+)#m', $config, $matches)) {
return;
}
$dir = $matches[1];
if (!preg_match('#^\s*dbfilename\s+([^\s]+)#m', $config, $matches)) {
return;
}
$filename = TEST_MISC . '/' . $dir . '/' . $matches[1];
if (is_file($filename)) {
unlink($filename);
}
}
register_shutdown_function('killRedis', getmypid());
# Cleanup forked workers cleanly
if (function_exists('pcntl_signal')) { if (function_exists('pcntl_signal')) {
// Override INT and TERM signals, so they do a clean shutdown and also
// clean up redis-server as well.
function sigint() function sigint()
{ {
exit; exit;
@ -88,6 +36,7 @@ if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sigint'); pcntl_signal(SIGTERM, 'sigint');
} }
# Bootstrap it
class Test_Job class Test_Job
{ {
public static $called = false; public static $called = false;