From 64f3e79e383a661ff1065e4bbf3ddc75073a2534 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 22 Feb 2021 12:31:53 -0500 Subject: [PATCH 01/11] Update Connection.php --- Net/Gearman/Connection.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index 23449d7a..e9061f3b 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -476,6 +476,11 @@ public function close() */ public function isConnected() { + // PHP 8+ returns Socket class instead of resource + if ($this->socket instanceof Socket) { + return true; + } + // HHVM returns stream. PHP 5.x returns socket $type = strtolower(get_resource_type($this->socket)); return (is_null($this->socket) !== true && From cfb8c99cb9187c88331d8b7ec44f89f80f30d655 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 22 Feb 2021 14:10:00 -0500 Subject: [PATCH 02/11] Update ConnectionTest.php --- tests/Net/Gearman/ConnectionTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Net/Gearman/ConnectionTest.php b/tests/Net/Gearman/ConnectionTest.php index 183aa9ef..86b5a5c7 100644 --- a/tests/Net/Gearman/ConnectionTest.php +++ b/tests/Net/Gearman/ConnectionTest.php @@ -16,7 +16,13 @@ public function testDefaultConnect() $connection = new Net_Gearman_Connection(); $this->assertType('resource', $connection); - $this->assertEquals('socket', strtolower(get_resource_type($connection->socket))); + if (version_compare($var, '8.0.0') >= 0) { + // PHP 8+ returns a Socket class instead of a resource now + $this->assertInstanceOf('Socket', $connections->socket); + } + else { + $this->assertEquals('socket', strtolower(get_resource_type($connection->socket))); + } $this->assertTrue($connection->isConnected()); From 9b4430916948554a51917e5725f7da869f323acd Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 22 Feb 2021 14:13:58 -0500 Subject: [PATCH 03/11] Updated requirement for PHP8 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b09cd8de..fde3d6cc 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "issues": "https://github.com/brianlmoon/net_gearman/issues" }, "require": { - "php": "^7.0.0" + "php": "^7.0.0|^8.0.0" }, "autoload": { "classmap": [ "Net/Gearman" ] From 894281d2de8bc1ab9890a947674ddde392fd01d0 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 22 Feb 2021 14:19:18 -0500 Subject: [PATCH 04/11] Fixing the php version requirements --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fde3d6cc..3aa73018 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "issues": "https://github.com/brianlmoon/net_gearman/issues" }, "require": { - "php": "^7.0.0|^8.0.0" + "php": "^7.0.0 || ^8.0.0" }, "autoload": { "classmap": [ "Net/Gearman" ] From f893d2cf633d1b18695d06c82f9390ad5bfa37e4 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 22 Feb 2021 20:42:37 -0500 Subject: [PATCH 05/11] Breaking up the logic a bit more for readability --- Net/Gearman/Connection.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index e9061f3b..d540fcc9 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -476,16 +476,21 @@ public function close() */ public function isConnected() { - // PHP 8+ returns Socket class instead of resource + if ($this->socket === null) { + // no socket established yet + return false; + } + + // PHP 8+ returns Socket object instead of resource if ($this->socket instanceof Socket) { return true; } - + // HHVM returns stream. PHP 5.x returns socket - $type = strtolower(get_resource_type($this->socket)); - return (is_null($this->socket) !== true && - is_resource($this->socket) === true && - ($type == 'socket' || $type == "stream")); + $type = mb_strtolower(get_resource_type($this->socket)); + + return is_resource($this->socket) === true + && ($type == 'socket' || $type == 'stream'); } /** From 7d5d9124580e83941244cffb738f2269236ec25f Mon Sep 17 00:00:00 2001 From: Jason Pirkey Date: Mon, 22 Feb 2021 21:34:16 -0500 Subject: [PATCH 06/11] Functional test updates along with some modernization --- .gitignore | 1 + composer.json | 4 +-- phpunit.xml.functional-dist | 22 +++++++++++++ tests/Net/Gearman/ConnectionTest.php | 27 ++++++++-------- tests/Net/Gearman/TaskTest.php | 48 ++++++++++++++-------------- tests/README.md | 13 ++++++++ 6 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 phpunit.xml.functional-dist create mode 100644 tests/README.md diff --git a/.gitignore b/.gitignore index aef54338..c1aaa589 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ tests/run-tests.log # Dependencies vendor composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index 3aa73018..ba37ccbd 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "issues": "https://github.com/brianlmoon/net_gearman/issues" }, "require": { - "php": "^7.0.0 || ^8.0.0" + "php": "^7.0.0|^8.0.0" }, "autoload": { "classmap": [ "Net/Gearman" ] @@ -29,6 +29,6 @@ "." ], "require-dev": { - "phpunit/phpunit": "^7.3" + "phpunit/phpunit": "^7.5|^8.0" } } diff --git a/phpunit.xml.functional-dist b/phpunit.xml.functional-dist new file mode 100644 index 00000000..503f00a9 --- /dev/null +++ b/phpunit.xml.functional-dist @@ -0,0 +1,22 @@ + + + + + + + + ./tests + + + + + functional + + + + + ./Net + + + diff --git a/tests/Net/Gearman/ConnectionTest.php b/tests/Net/Gearman/ConnectionTest.php index 86b5a5c7..18ecdb53 100644 --- a/tests/Net/Gearman/ConnectionTest.php +++ b/tests/Net/Gearman/ConnectionTest.php @@ -1,7 +1,8 @@ assertType('resource', $connection); - if (version_compare($var, '8.0.0') >= 0) { + $connection = new Net_Gearman_Connection(NET_GEARMAN_TEST_SERVER); + if (version_compare(PHP_VERSION, '8.0.0') >= 0) { // PHP 8+ returns a Socket class instead of a resource now - $this->assertInstanceOf('Socket', $connections->socket); - } - else { + $this->assertInstanceOf('Socket', $connection->socket); + } else { $this->assertEquals('socket', strtolower(get_resource_type($connection->socket))); } @@ -30,26 +28,27 @@ public function testDefaultConnect() } /** - * 001-echo_req.phpt + * 001-echo_req.phpt. * * @return void */ public function testSend() { - $connection = new Net_Gearman_Connection(); - $connection->send('echo_req', array('text' => 'foobar')); + $connection = new Net_Gearman_Connection(NET_GEARMAN_TEST_SERVER); + $this->assertTrue($connection->isConnected()); + $connection->send('echo_req', ['text' => 'foobar']); do { $ret = $connection->read(); - } while (is_array($ret) && !count($ret)); + } while (is_array($ret) && ! count($ret)); $connection->close(); - $this->assertType('array', $ret); + $this->assertIsArray($ret); $this->assertEquals('echo_res', $ret['function']); $this->assertEquals(17, $ret['type']); + $this->assertIsArray($ret['data']); - $this->assertType('array', $ret['data']); $this->assertEquals('foobar', $ret['data']['text']); } } diff --git a/tests/Net/Gearman/TaskTest.php b/tests/Net/Gearman/TaskTest.php index 0d83c014..58f07585 100644 --- a/tests/Net/Gearman/TaskTest.php +++ b/tests/Net/Gearman/TaskTest.php @@ -1,6 +1,6 @@ assertEquals('foo', $task->func); - $this->assertEquals(array('bar'), $task->arg); + $this->assertEquals(['bar'], $task->arg); $this->assertEquals($uniq, $task->uniq); } /** - * @expectedException Net_Gearman_Exception + * @expectedException \Net_Gearman_Exception */ public function testAttachInvalidCallback() { - $task = new Net_Gearman_Task('foo', array()); + $task = new Net_Gearman_Task('foo', []); $task->attachCallback('func_bar'); } /** - * @expectedException Net_Gearman_Exception + * @expectedException \Net_Gearman_Exception */ public function testAttachInvalidCallbackType() { - $task = new Net_Gearman_Task('foo', array()); + $task = new Net_Gearman_Task('foo', []); $this->assertInstanceOf('Net_Gearman_Task', $task->attachCallback('strlen', 666)); } public static function callbackProvider() { - return array( - array('strlen', Net_Gearman_Task::TASK_FAIL), - array('intval', Net_Gearman_Task::TASK_COMPLETE), - array('explode', Net_Gearman_Task::TASK_STATUS), - ); + return [ + ['strlen', Net_Gearman_Task::TASK_FAIL], + ['intval', Net_Gearman_Task::TASK_COMPLETE], + ['explode', Net_Gearman_Task::TASK_STATUS], + ]; } /** @@ -62,7 +62,7 @@ public static function callbackProvider() */ public function testAttachCallback($func, $type) { - $task = new Net_Gearman_Task('foo', array()); + $task = new Net_Gearman_Task('foo', []); $task->attachCallback($func, $type); $callbacks = $task->getCallbacks(); @@ -77,7 +77,7 @@ public function testAttachCallback($func, $type) */ public function testCompleteCallback() { - $task = new Net_Gearman_Task('foo', array('foo' => 'bar')); + $task = new Net_Gearman_Task('foo', ['foo' => 'bar']); $this->assertEquals(null, $task->complete('foo')); @@ -91,7 +91,7 @@ public function testCompleteCallback() $this->assertEquals($json, $task->result); $this->assertEquals( - array('func' => 'foo', 'handle' => '', 'result' => $json), + ['func' => 'foo', 'handle' => '', 'result' => $json], $GLOBALS['Net_Gearman_TaskTest'] ); @@ -102,13 +102,14 @@ public function testCompleteCallback() * See that task has handle and server assigned. * * @group functional + * * @return void */ public function testTaskStatus() { - $client = new Net_Gearman_Client(["127.0.0.1:4730"]); + $client = new Net_Gearman_Client([NET_GEARMAN_TEST_SERVER]); - $task = new Net_Gearman_Task('Reverse', range(1,5)); + $task = new Net_Gearman_Task('Reverse', range(1, 5)); $task->type = Net_Gearman_Task::JOB_BACKGROUND; $set = new Net_Gearman_Set(); @@ -117,7 +118,6 @@ public function testTaskStatus() $client->runSet($set); $this->assertNotEquals('', $task->handle); - $this->assertNotEquals('', $task->server); } } @@ -132,9 +132,9 @@ public function testTaskStatus() */ function Net_Gearman_TaskTest_testCallBack($func, $handle, $result) { - $GLOBALS['Net_Gearman_TaskTest'] = array( - 'func' => $func, + $GLOBALS['Net_Gearman_TaskTest'] = [ + 'func' => $func, 'handle' => $handle, - 'result' => $result - ); + 'result' => $result, + ]; } diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..2f2a8b37 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,13 @@ +# Runnings tests +From the project root: + +## Install +1. composer install + +## Run the unit tests +1. vendor/bin/phpunit -c phpunit.xml.dist + +## Run the functional tests +1. Start up your gearman job server +1. Update the `NET_GEARMAN_TEST_SERVER` constant in `phpunit.xml.functional-dist` (if necessary) +1. vendor/bin/phpunit -c phpunit.xml.functional-dist From b3a08ae3fe2602e3537c343d038b0add69cad5aa Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 22 Feb 2021 21:44:46 -0500 Subject: [PATCH 07/11] Remove unnecessary mb_strtolower call. --- Net/Gearman/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index d540fcc9..4f8b304a 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -487,7 +487,7 @@ public function isConnected() } // HHVM returns stream. PHP 5.x returns socket - $type = mb_strtolower(get_resource_type($this->socket)); + $type = strtolower(get_resource_type($this->socket)); return is_resource($this->socket) === true && ($type == 'socket' || $type == 'stream'); From 2565e20fe67fdac601a9792bdb2b3da1c8e175e0 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 23 Feb 2021 10:08:05 -0500 Subject: [PATCH 08/11] Making user we are using the proper namespaced Socket object --- Net/Gearman/Connection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index 4f8b304a..bd624d1f 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -154,7 +154,7 @@ public function connect($host, $timeout = 250) /** * Set the send and receive timeouts super low so that socket_connect * will return to us quickly. We then loop and check the real timeout - * and check the socket error to decide if its conected yet or not. + * and check the socket error to decide if its connected yet or not. */ socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>0, "usec" => 100)); socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>0, "usec" => 100)); @@ -482,7 +482,7 @@ public function isConnected() } // PHP 8+ returns Socket object instead of resource - if ($this->socket instanceof Socket) { + if ($this->socket instanceof \Socket) { return true; } From 79061fead1afa849f9b048879d4106d48b06ad65 Mon Sep 17 00:00:00 2001 From: Jason Pirkey Date: Sat, 27 Feb 2021 23:32:28 -0500 Subject: [PATCH 09/11] Simplified Connection::isConnected method --- Net/Gearman/Connection.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index bd624d1f..7d9409ba 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -486,11 +486,13 @@ public function isConnected() return true; } - // HHVM returns stream. PHP 5.x returns socket - $type = strtolower(get_resource_type($this->socket)); + // PHP 5.x-7.x returns socket + if (is_resource($this->socket) === true) { + $type = strtolower(get_resource_type($this->socket)); + return $type === 'socket'; + } - return is_resource($this->socket) === true - && ($type == 'socket' || $type == 'stream'); + return false; } /** From 48aa5e1955a55491f7364cb8cbc11bdbeee0ab49 Mon Sep 17 00:00:00 2001 From: Jason Pirkey Date: Sat, 27 Feb 2021 23:39:59 -0500 Subject: [PATCH 10/11] Even more simplified Connection::isConnected method --- Net/Gearman/Connection.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index 7d9409ba..da70a4b9 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -476,9 +476,10 @@ public function close() */ public function isConnected() { - if ($this->socket === null) { - // no socket established yet - return false; + // PHP 5.x-7.x returns socket + if (is_resource($this->socket) === true) { + $type = strtolower(get_resource_type($this->socket)); + return $type === 'socket'; } // PHP 8+ returns Socket object instead of resource @@ -486,12 +487,6 @@ public function isConnected() return true; } - // PHP 5.x-7.x returns socket - if (is_resource($this->socket) === true) { - $type = strtolower(get_resource_type($this->socket)); - return $type === 'socket'; - } - return false; } From caa78687ef94843bc7d45d602bb52ab4065e3155 Mon Sep 17 00:00:00 2001 From: Jason Pirkey Date: Sun, 28 Feb 2021 15:56:37 -0500 Subject: [PATCH 11/11] Switching checks in Net_Gearman_Connection::isConnected --- Net/Gearman/Connection.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index da70a4b9..8d6ece3a 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -476,17 +476,17 @@ public function close() */ public function isConnected() { + // PHP 8+ returns Socket object instead of resource + if ($this->socket instanceof \Socket) { + return true; + } + // PHP 5.x-7.x returns socket if (is_resource($this->socket) === true) { $type = strtolower(get_resource_type($this->socket)); return $type === 'socket'; } - // PHP 8+ returns Socket object instead of resource - if ($this->socket instanceof \Socket) { - return true; - } - return false; }