From b99217f2c0a9fda5ed13c5b17f3165d0e674ce83 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 11 Apr 2020 09:24:18 +1200 Subject: [PATCH] Update for PHP7.4 compatibility + UnitTests --- .gitlab-ci.yml | 52 ++++------------------------- CHANGELOG.md | 4 +++ composer.json | 6 ++-- lib/Resque.php | 40 ++++++++++++---------- test/Resque/Tests/EventTest.php | 4 +-- test/Resque/Tests/JobStatusTest.php | 2 +- test/Resque/Tests/JobTest.php | 10 ++++-- test/Resque/Tests/RedisTest.php | 4 ++- test/Resque/Tests/TestCase.php | 4 +-- 9 files changed, 51 insertions(+), 75 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c7bb222..ed4b864 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ # Install git (the php image doesn't have it) which is required by composer apt-get update -yq - apt-get install git wget procps -y + apt-get install git wget procps unzip -y # Install pcntl and redis extentions pecl install -o -f redis \ @@ -17,55 +17,17 @@ # Install Composer wget https://getcomposer.org/composer.phar - php composer.phar install + php composer.phar install --dev services: - redis:latest -# Test PHP 7.0 -test:7.0: - image: php:7.0 +# Test PHP 7.4 +test:7.4: + image: php:7.4 before_script: - *docker_boostrap script: - - curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-6.5.9.phar - - chmod +x /usr/local/bin/phpunit - - phpunit --verbose --configuration phpunit.xml.dist + - php vendor/bin/phpunit --verbose --configuration phpunit.xml.dist tags: - - docker - -# Test PHP 7.1 -test:7.1: - image: php:7.1 - before_script: - - *docker_boostrap - script: - - curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-7.5.9.phar - - chmod +x /usr/local/bin/phpunit - - phpunit --verbose --configuration phpunit.xml.dist - tags: - - docker - -# Test PHP 7.2 -test:7.2: - image: php:7.2 - before_script: - - *docker_boostrap - script: - - curl --location --output /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-7.5.9.phar - - chmod +x /usr/local/bin/phpunit - - phpunit --verbose --configuration phpunit.xml.dist - tags: - - 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 --verbose --configuration phpunit.xml.dist - tags: - - docker + - docker \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d58049..84ef43f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.7 (2020-04-11) +- Update PHPUnit to 9 +- Start adding return types + ## 1.4.6 (2020-01-10) - Switched IF Statement order to prevent excess calls to redis. diff --git a/composer.json b/composer.json index d4327b8..887fa57 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,13 @@ { "name": "idanoo/php-resque", - "version": "1.4.6", + "version": "1.4.7", "type": "library", "replace": { "chrisboulton/php-resque": "*", "danhunsaker/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", "php"], "homepage": "http://www.github.com/idanoo/php-resque/", "license": "MIT", "authors": [ @@ -24,7 +24,7 @@ "colinmollenhour/credis": "^1.10" }, "require-dev": { - "phpunit/phpunit": "^6" + "phpunit/phpunit": "^9" }, "bin": [ "bin/resque" diff --git a/lib/Resque.php b/lib/Resque.php index 68b0067..49770e4 100644 --- a/lib/Resque.php +++ b/lib/Resque.php @@ -10,7 +10,7 @@ class Resque { - const VERSION = '1.4.6'; + const VERSION = '1.4.7'; const DEFAULT_INTERVAL = 5; @@ -51,6 +51,7 @@ class Resque * Return an instance of the Resque_Redis class instantiated for Resque. * * @return Resque_Redis Instance of Resque_Redis. + * * @throws Resque_RedisException */ public static function redis() @@ -101,9 +102,10 @@ class Resque * * @param string $queue The name of the queue to add the job to. * @param array $item Job description as an array to be JSON encoded. + * * @return bool */ - public static function push($queue, $item) + public static function push($queue, $item): bool { $encodedItem = json_encode($item); if ($encodedItem === false) { @@ -122,6 +124,7 @@ class Resque * return it. * * @param string $queue The name of the queue to fetch an item from. + * * @return mixed Decoded item from the queue. */ public static function pop($queue) @@ -142,7 +145,7 @@ class Resque * @param array $items * @return integer number of deleted items */ - public static function dequeue($queue, $items = Array()) + public static function dequeue($queue, $items = []) { if (count($items) > 0) { return self::removeItems($queue, $items); @@ -155,13 +158,14 @@ class Resque * Remove specified queue * * @param string $queue The name of the queue to remove. - * @return integer Number of deleted items + * + * @return int Number of deleted items */ - public static function removeQueue($queue) + public static function removeQueue($queue): int { $num = self::removeList($queue); self::redis()->srem('queues', $queue); - return $num; + return intval($num); } /** @@ -170,12 +174,13 @@ class Resque * * @param array $queues * @param int $timeout + * * @return array|null|void */ public static function blpop(array $queues, $timeout) { $list = array(); - foreach ($queues AS $queue) { + foreach ($queues as $queue) { $list[] = 'queue:' . $queue; } @@ -192,10 +197,10 @@ class Resque */ $queue = substr($item[0], strlen(self::redis()->getPrefix() . 'queue:')); - return array( + return [ 'queue' => $queue, 'payload' => json_decode($item[1], true) - ); + ]; } /** @@ -205,9 +210,9 @@ class Resque * * @return int The size of the queue. */ - public static function size($queue) + public static function size($queue): int { - return self::redis()->llen('queue:' . $queue); + return intval(self::redis()->llen('queue:' . $queue)); } /** @@ -245,6 +250,7 @@ class Resque * Reserve and return the next available job in the specified queue. * * @param string $queue Queue to fetch next available job from. + * * @return false|object|Resque_Job */ public static function reserve($queue) @@ -257,13 +263,10 @@ class Resque * * @return array Array of queues. */ - public static function queues() + public static function queues(): array { $queues = self::redis()->smembers('queues'); - if (!is_array($queues)) { - $queues = array(); - } - return $queues; + return is_array($queues) ? $queues : []; } /** @@ -276,9 +279,10 @@ class Resque * * @param string $queue The name of the queue * @param array $items - * @return integer number of deleted items + * + * @return int number of deleted items */ - private static function removeItems($queue, $items = Array()) + private static function removeItems($queue, $items = []): int { $counter = 0; $originalQueue = 'queue:' . $queue; diff --git a/test/Resque/Tests/EventTest.php b/test/Resque/Tests/EventTest.php index 24a83c7..2bb4f40 100644 --- a/test/Resque/Tests/EventTest.php +++ b/test/Resque/Tests/EventTest.php @@ -13,7 +13,7 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase private $callbacksHit = []; private $worker; - public function setUp() + public function setUp(): void { Test_Job::$called = false; @@ -23,7 +23,7 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase $this->worker->registerWorker(); } - public function tearDown() + public function tearDown(): void { Resque_Event::clearListeners(); $this->callbacksHit = []; diff --git a/test/Resque/Tests/JobStatusTest.php b/test/Resque/Tests/JobStatusTest.php index d573456..3cb0ca8 100644 --- a/test/Resque/Tests/JobStatusTest.php +++ b/test/Resque/Tests/JobStatusTest.php @@ -15,7 +15,7 @@ class Resque_Tests_JobStatusTest extends Resque_Tests_TestCase */ protected $worker; - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index ada26cb..67865f8 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -12,7 +12,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase { protected $worker; - public function setUp() + public function setUp(): void { parent::setUp(); @@ -44,7 +44,9 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase */ public function testObjectArgumentsCannotBePassedToJob() { - $args = new stdClass; + $this->expectException(InvalidArgumentException::class); + + $args = new stdClass(); $args->test = 'somevalue'; Resque::enqueue('jobs', 'Test_Job', $args); } @@ -121,6 +123,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase */ public function testJobWithoutPerformMethodThrowsException() { + $this->expectException(Resque_Exception::class); Resque::enqueue('jobs', 'Test_Job_Without_Perform_Method'); $job = $this->worker->reserve(); $job->worker = $this->worker; @@ -132,6 +135,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase */ public function testInvalidJobThrowsException() { + $this->expectException(Resque_Exception::class); Resque::enqueue('jobs', 'Invalid_Job'); $job = $this->worker->reserve(); $job->worker = $this->worker; @@ -334,7 +338,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $this->assertEquals($removedItems, 2); $this->assertEquals(Resque::size($queue), 1); $item = Resque::pop($queue); - $this->assertInternalType('array', $item['args']); + $this->assertIsArray($item['args']); $this->assertEquals(10, $item['args'][0]['bar'], 'Wrong items were dequeued from queue!'); } diff --git a/test/Resque/Tests/RedisTest.php b/test/Resque/Tests/RedisTest.php index 7e9a346..2f73bc4 100644 --- a/test/Resque/Tests/RedisTest.php +++ b/test/Resque/Tests/RedisTest.php @@ -181,12 +181,14 @@ class Resque_Tests_RedisTest extends Resque_Tests_TestCase /** * @dataProvider bogusDsnStringProvider + * * @expectedException InvalidArgumentException + * * @param $dsn */ public function testParsingBogusDsnStringThrowsException($dsn) { - // The next line should throw an InvalidArgumentException + $this->expectException(InvalidArgumentException::class); Resque_Redis::parseDsn($dsn); } } \ No newline at end of file diff --git a/test/Resque/Tests/TestCase.php b/test/Resque/Tests/TestCase.php index a428d12..0e93578 100644 --- a/test/Resque/Tests/TestCase.php +++ b/test/Resque/Tests/TestCase.php @@ -13,12 +13,12 @@ class Resque_Tests_TestCase extends PHPUnit\Framework\TestCase protected $resque; protected $redis; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { date_default_timezone_set('UTC'); } - public function setUp() + public function setUp(): void { // Setup redis connection for testing. global $redisTestServer;