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/Net/Gearman/Connection.php b/Net/Gearman/Connection.php index a2163bb0..824486c3 100644 --- a/Net/Gearman/Connection.php +++ b/Net/Gearman/Connection.php @@ -161,7 +161,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)); @@ -489,11 +489,18 @@ public function close() */ public function isConnected() { - // 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")); + // 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'; + } + + return false; } /** diff --git a/composer.json b/composer.json index b09cd8de..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" + "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 c7e93cab..793664f1 100644 --- a/tests/Net/Gearman/ConnectionTest.php +++ b/tests/Net/Gearman/ConnectionTest.php @@ -1,7 +1,9 @@ assertType('resource', $connection); - $this->assertEquals('socket', strtolower(get_resource_type($connection->socket))); + $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', $connection->socket); + } else { + $this->assertEquals('socket', strtolower(get_resource_type($connection->socket))); + } $this->assertTrue($connection->isConnected()); @@ -85,20 +90,21 @@ public function testDefaultConnect() */ 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