Skip to content

Commit

Permalink
Merge pull request #48 from brianlmoon/php8-updates
Browse files Browse the repository at this point in the history
PHP 8 Support
  • Loading branch information
brianlmoon authored Aug 29, 2022
2 parents a092a78 + 3eb6e40 commit 0f61d29
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tests/run-tests.log
# Dependencies
vendor
composer.lock
.phpunit.result.cache
19 changes: 13 additions & 6 deletions Net/Gearman/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand All @@ -29,6 +29,6 @@
"."
],
"require-dev": {
"phpunit/phpunit": "^7.3"
"phpunit/phpunit": "^7.5|^8.0"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.functional-dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true">
<php>
<const name="NET_GEARMAN_TEST_SERVER" value="localhost:4730"/>
</php>
<testsuites>
<testsuite name="Main">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<groups>
<include>
<group>functional</group>
</include>
</groups>
<filter>
<whitelist>
<directory>./Net</directory>
</whitelist>
</filter>
</phpunit>
26 changes: 16 additions & 10 deletions tests/Net/Gearman/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

/**
* Net_Gearman_ConnectionTest
* Net_Gearman_ConnectionTest.
*
* @group functional
*/
class Net_Gearman_ConnectionTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -68,10 +70,13 @@ public function fixTimeout($params) {
*/
public function testDefaultConnect()
{

$connection = new Net_Gearman_Connection();
$this->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());

Expand All @@ -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']);
}
}
48 changes: 24 additions & 24 deletions tests/Net/Gearman/TaskTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
/**
* Net_Gearman_ConnectionTest
* Net_Gearman_TaskTest.
*/
class Net_Gearman_TaskTest extends \PHPUnit\Framework\TestCase
{
/**
* Unknown job type.
*
* @return void
* @expectedException Net_Gearman_Exception
* @expectedException \Net_Gearman_Exception
*/
public function testExceptionFromConstruct()
{
new Net_Gearman_Task('foo', array(), null, 8);
new Net_Gearman_Task('foo', [], null, 8);
}

/**
Expand All @@ -23,46 +23,46 @@ public function testExceptionFromConstruct()
public function testParameters()
{
$uniq = uniqid();
$task = new Net_Gearman_Task('foo', array('bar'), $uniq, 1);
$task = new Net_Gearman_Task('foo', ['bar'], $uniq, 1);

$this->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],
];
}

/**
* @dataProvider 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();
Expand All @@ -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'));

Expand All @@ -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']
);

Expand All @@ -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();
Expand All @@ -117,7 +118,6 @@ public function testTaskStatus()
$client->runSet($set);

$this->assertNotEquals('', $task->handle);
$this->assertNotEquals('', $task->server);
}
}

Expand All @@ -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,
];
}
13 changes: 13 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0f61d29

Please sign in to comment.