Skip to content

Commit 0f61d29

Browse files
authored
Merge pull request #48 from brianlmoon/php8-updates
PHP 8 Support
2 parents a092a78 + 3eb6e40 commit 0f61d29

File tree

7 files changed

+91
-42
lines changed

7 files changed

+91
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ tests/run-tests.log
66
# Dependencies
77
vendor
88
composer.lock
9+
.phpunit.result.cache

Net/Gearman/Connection.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function connect($host, $timeout = 250)
161161
/**
162162
* Set the send and receive timeouts super low so that socket_connect
163163
* will return to us quickly. We then loop and check the real timeout
164-
* and check the socket error to decide if its conected yet or not.
164+
* and check the socket error to decide if its connected yet or not.
165165
*/
166166
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>0, "usec" => 100));
167167
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>0, "usec" => 100));
@@ -489,11 +489,18 @@ public function close()
489489
*/
490490
public function isConnected()
491491
{
492-
// HHVM returns stream. PHP 5.x returns socket
493-
$type = strtolower(get_resource_type($this->socket));
494-
return (is_null($this->socket) !== true &&
495-
is_resource($this->socket) === true &&
496-
($type == 'socket' || $type == "stream"));
492+
// PHP 8+ returns Socket object instead of resource
493+
if ($this->socket instanceof \Socket) {
494+
return true;
495+
}
496+
497+
// PHP 5.x-7.x returns socket
498+
if (is_resource($this->socket) === true) {
499+
$type = strtolower(get_resource_type($this->socket));
500+
return $type === 'socket';
501+
}
502+
503+
return false;
497504
}
498505

499506
/**

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"issues": "https://github.com/brianlmoon/net_gearman/issues"
1616
},
1717
"require": {
18-
"php": "^7.0.0"
18+
"php": "^7.0.0|^8.0.0"
1919
},
2020
"autoload": {
2121
"classmap": [ "Net/Gearman" ]
@@ -29,6 +29,6 @@
2929
"."
3030
],
3131
"require-dev": {
32-
"phpunit/phpunit": "^7.3"
32+
"phpunit/phpunit": "^7.5|^8.0"
3333
}
3434
}

phpunit.xml.functional-dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true">
4+
<php>
5+
<const name="NET_GEARMAN_TEST_SERVER" value="localhost:4730"/>
6+
</php>
7+
<testsuites>
8+
<testsuite name="Main">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<groups>
13+
<include>
14+
<group>functional</group>
15+
</include>
16+
</groups>
17+
<filter>
18+
<whitelist>
19+
<directory>./Net</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

tests/Net/Gearman/ConnectionTest.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
/**
4-
* Net_Gearman_ConnectionTest
4+
* Net_Gearman_ConnectionTest.
5+
*
6+
* @group functional
57
*/
68
class Net_Gearman_ConnectionTest extends \PHPUnit\Framework\TestCase
79
{
@@ -68,10 +70,13 @@ public function fixTimeout($params) {
6870
*/
6971
public function testDefaultConnect()
7072
{
71-
72-
$connection = new Net_Gearman_Connection();
73-
$this->assertType('resource', $connection);
74-
$this->assertEquals('socket', strtolower(get_resource_type($connection->socket)));
73+
$connection = new Net_Gearman_Connection(NET_GEARMAN_TEST_SERVER);
74+
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
75+
// PHP 8+ returns a Socket class instead of a resource now
76+
$this->assertInstanceOf('Socket', $connection->socket);
77+
} else {
78+
$this->assertEquals('socket', strtolower(get_resource_type($connection->socket)));
79+
}
7580

7681
$this->assertTrue($connection->isConnected());
7782

@@ -85,20 +90,21 @@ public function testDefaultConnect()
8590
*/
8691
public function testSend()
8792
{
88-
$connection = new Net_Gearman_Connection();
89-
$connection->send('echo_req', array('text' => 'foobar'));
93+
$connection = new Net_Gearman_Connection(NET_GEARMAN_TEST_SERVER);
94+
$this->assertTrue($connection->isConnected());
95+
$connection->send('echo_req', ['text' => 'foobar']);
9096

9197
do {
9298
$ret = $connection->read();
93-
} while (is_array($ret) && !count($ret));
99+
} while (is_array($ret) && ! count($ret));
94100

95101
$connection->close();
96102

97-
$this->assertType('array', $ret);
103+
$this->assertIsArray($ret);
98104
$this->assertEquals('echo_res', $ret['function']);
99105
$this->assertEquals(17, $ret['type']);
106+
$this->assertIsArray($ret['data']);
100107

101-
$this->assertType('array', $ret['data']);
102108
$this->assertEquals('foobar', $ret['data']['text']);
103109
}
104110
}

tests/Net/Gearman/TaskTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22
/**
3-
* Net_Gearman_ConnectionTest
3+
* Net_Gearman_TaskTest.
44
*/
55
class Net_Gearman_TaskTest extends \PHPUnit\Framework\TestCase
66
{
77
/**
88
* Unknown job type.
99
*
1010
* @return void
11-
* @expectedException Net_Gearman_Exception
11+
* @expectedException \Net_Gearman_Exception
1212
*/
1313
public function testExceptionFromConstruct()
1414
{
15-
new Net_Gearman_Task('foo', array(), null, 8);
15+
new Net_Gearman_Task('foo', [], null, 8);
1616
}
1717

1818
/**
@@ -23,46 +23,46 @@ public function testExceptionFromConstruct()
2323
public function testParameters()
2424
{
2525
$uniq = uniqid();
26-
$task = new Net_Gearman_Task('foo', array('bar'), $uniq, 1);
26+
$task = new Net_Gearman_Task('foo', ['bar'], $uniq, 1);
2727

2828
$this->assertEquals('foo', $task->func);
29-
$this->assertEquals(array('bar'), $task->arg);
29+
$this->assertEquals(['bar'], $task->arg);
3030
$this->assertEquals($uniq, $task->uniq);
3131
}
3232

3333
/**
34-
* @expectedException Net_Gearman_Exception
34+
* @expectedException \Net_Gearman_Exception
3535
*/
3636
public function testAttachInvalidCallback()
3737
{
38-
$task = new Net_Gearman_Task('foo', array());
38+
$task = new Net_Gearman_Task('foo', []);
3939
$task->attachCallback('func_bar');
4040
}
4141

4242
/**
43-
* @expectedException Net_Gearman_Exception
43+
* @expectedException \Net_Gearman_Exception
4444
*/
4545
public function testAttachInvalidCallbackType()
4646
{
47-
$task = new Net_Gearman_Task('foo', array());
47+
$task = new Net_Gearman_Task('foo', []);
4848
$this->assertInstanceOf('Net_Gearman_Task', $task->attachCallback('strlen', 666));
4949
}
5050

5151
public static function callbackProvider()
5252
{
53-
return array(
54-
array('strlen', Net_Gearman_Task::TASK_FAIL),
55-
array('intval', Net_Gearman_Task::TASK_COMPLETE),
56-
array('explode', Net_Gearman_Task::TASK_STATUS),
57-
);
53+
return [
54+
['strlen', Net_Gearman_Task::TASK_FAIL],
55+
['intval', Net_Gearman_Task::TASK_COMPLETE],
56+
['explode', Net_Gearman_Task::TASK_STATUS],
57+
];
5858
}
5959

6060
/**
6161
* @dataProvider callbackProvider
6262
*/
6363
public function testAttachCallback($func, $type)
6464
{
65-
$task = new Net_Gearman_Task('foo', array());
65+
$task = new Net_Gearman_Task('foo', []);
6666
$task->attachCallback($func, $type);
6767

6868
$callbacks = $task->getCallbacks();
@@ -77,7 +77,7 @@ public function testAttachCallback($func, $type)
7777
*/
7878
public function testCompleteCallback()
7979
{
80-
$task = new Net_Gearman_Task('foo', array('foo' => 'bar'));
80+
$task = new Net_Gearman_Task('foo', ['foo' => 'bar']);
8181

8282
$this->assertEquals(null, $task->complete('foo'));
8383

@@ -91,7 +91,7 @@ public function testCompleteCallback()
9191
$this->assertEquals($json, $task->result);
9292

9393
$this->assertEquals(
94-
array('func' => 'foo', 'handle' => '', 'result' => $json),
94+
['func' => 'foo', 'handle' => '', 'result' => $json],
9595
$GLOBALS['Net_Gearman_TaskTest']
9696
);
9797

@@ -102,13 +102,14 @@ public function testCompleteCallback()
102102
* See that task has handle and server assigned.
103103
*
104104
* @group functional
105+
*
105106
* @return void
106107
*/
107108
public function testTaskStatus()
108109
{
109-
$client = new Net_Gearman_Client(["127.0.0.1:4730"]);
110+
$client = new Net_Gearman_Client([NET_GEARMAN_TEST_SERVER]);
110111

111-
$task = new Net_Gearman_Task('Reverse', range(1,5));
112+
$task = new Net_Gearman_Task('Reverse', range(1, 5));
112113
$task->type = Net_Gearman_Task::JOB_BACKGROUND;
113114

114115
$set = new Net_Gearman_Set();
@@ -117,7 +118,6 @@ public function testTaskStatus()
117118
$client->runSet($set);
118119

119120
$this->assertNotEquals('', $task->handle);
120-
$this->assertNotEquals('', $task->server);
121121
}
122122
}
123123

@@ -132,9 +132,9 @@ public function testTaskStatus()
132132
*/
133133
function Net_Gearman_TaskTest_testCallBack($func, $handle, $result)
134134
{
135-
$GLOBALS['Net_Gearman_TaskTest'] = array(
136-
'func' => $func,
135+
$GLOBALS['Net_Gearman_TaskTest'] = [
136+
'func' => $func,
137137
'handle' => $handle,
138-
'result' => $result
139-
);
138+
'result' => $result,
139+
];
140140
}

tests/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Runnings tests
2+
From the project root:
3+
4+
## Install
5+
1. composer install
6+
7+
## Run the unit tests
8+
1. vendor/bin/phpunit -c phpunit.xml.dist
9+
10+
## Run the functional tests
11+
1. Start up your gearman job server
12+
1. Update the `NET_GEARMAN_TEST_SERVER` constant in `phpunit.xml.functional-dist` (if necessary)
13+
1. vendor/bin/phpunit -c phpunit.xml.functional-dist

0 commit comments

Comments
 (0)