From f98fd1e65b9e1c90fbd1a69e655bd0df5af1b7c6 Mon Sep 17 00:00:00 2001 From: Andreas Sundqvist Date: Mon, 16 Oct 2017 17:57:38 +0200 Subject: [PATCH 1/2] Add support for all callable type not just Closure Added a test for the new behavior. composer.json - Added test folder to autoload if in devmode phpunit.xml.dist - Fixed path to test folder --- composer.json | 8 +++++-- phpunit.xml.dist | 2 +- src/Nats/Connection.php | 12 +++++----- src/Nats/EncodedConnection.php | 12 +++++----- test/CallableClass.php | 40 ++++++++++++++++++++++++++++++++++ test/ConnectionTest.php | 29 ++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 test/CallableClass.php diff --git a/composer.json b/composer.json index 74f801f..0f4c914 100644 --- a/composer.json +++ b/composer.json @@ -40,8 +40,12 @@ ], "autoload": { "psr-0": { - "Nats": "src", - "Nats\\Test": "src" + "Nats": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Nats\\tests\\Unit\\": "test/" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 089483c..06c1ecd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,7 +12,7 @@ - ./tests/Unit/ + ./test/ diff --git a/src/Nats/Connection.php b/src/Nats/Connection.php index c16c538..f8cefce 100644 --- a/src/Nats/Connection.php +++ b/src/Nats/Connection.php @@ -481,11 +481,11 @@ public function ping() * * @param string $subject Message topic. * @param string $payload Message data. - * @param \Closure $callback Closure to be executed as callback. + * @param callable $callback Closure to be executed as callback. * * @return void */ - public function request($subject, $payload, \Closure $callback) + public function request($subject, $payload, callable $callback) { $inbox = uniqid('_INBOX.'); $sid = $this->subscribe( @@ -501,11 +501,11 @@ public function request($subject, $payload, \Closure $callback) * Subscribes to an specific event given a subject. * * @param string $subject Message topic. - * @param \Closure $callback Closure to be executed as callback. + * @param callable $callback Closure to be executed as callback. * * @return string */ - public function subscribe($subject, \Closure $callback) + public function subscribe($subject, callable $callback) { $sid = $this->randomGenerator->generateString(16); $msg = 'SUB '.$subject.' '.$sid; @@ -519,11 +519,11 @@ public function subscribe($subject, \Closure $callback) * * @param string $subject Message topic. * @param string $queue Queue name. - * @param \Closure $callback Closure to be executed as callback. + * @param callable $callback Closure to be executed as callback. * * @return string */ - public function queueSubscribe($subject, $queue, \Closure $callback) + public function queueSubscribe($subject, $queue, callable $callback) { $sid = $this->randomGenerator->generateString(16); $msg = 'SUB '.$subject.' '.$queue.' '.$sid; diff --git a/src/Nats/EncodedConnection.php b/src/Nats/EncodedConnection.php index 141c1db..fb8f90e 100644 --- a/src/Nats/EncodedConnection.php +++ b/src/Nats/EncodedConnection.php @@ -36,11 +36,11 @@ public function __construct(ConnectionOptions $options = null, Encoder $encoder * * @param string $subject Message topic. * @param string $payload Message data. - * @param \Closure $callback Closure to be executed as callback. + * @param callable $callback Closure to be executed as callback. * * @return void */ - public function request($subject, $payload, \Closure $callback) + public function request($subject, $payload, callable $callback) { $payload = $this->encoder->encode($payload); parent::request($subject, $payload, $callback); @@ -65,11 +65,11 @@ public function publish($subject, $payload = null, $inbox = null) * Subscribes to an specific event given a subject. * * @param string $subject Message topic. - * @param \Closure $callback Closure to be executed as callback. + * @param callable $callback Closure to be executed as callback. * * @return string */ - public function subscribe($subject, \Closure $callback) + public function subscribe($subject, callable $callback) { $c = function ($message) use ($callback) { $message->setBody($this->encoder->decode($message->getBody())); @@ -83,11 +83,11 @@ public function subscribe($subject, \Closure $callback) * * @param string $subject Message topic. * @param string $queue Queue name. - * @param \Closure $callback Closure to be executed as callback. + * @param callable $callback Closure to be executed as callback. * * @return void */ - public function queueSubscribe($subject, $queue, \Closure $callback) + public function queueSubscribe($subject, $queue, callable $callback) { $c = function ($message) use ($callback) { $message->setBody($this->encoder->decode($message->getBody())); diff --git a/test/CallableClass.php b/test/CallableClass.php new file mode 100644 index 0000000..16e47b1 --- /dev/null +++ b/test/CallableClass.php @@ -0,0 +1,40 @@ +reply('Hello, '.$res->getBody()); + } + public function __invoke($res) + { + $msg = $res->getBody(); + $p = explode(' ', $msg); + $count = array_pop($p); + if (isset($this->tmpCallbacks[$count])) { + $cb = $this->tmpCallbacks[$count]; + $cb($msg, $count); + unset($this->tmpCallbacks[$count]); + } else { + $this->tmpStorage[$count] = $msg; + } + } + + public function getMsg() + { + return $this->msg; + } + + public function test($i, callable $param) + { + if (isset($this->tmpStorage[$i])) { + $param($this->tmpStorage[$i], $i); + } else { + $this->tmpCallbacks[$i] = $param; + } + } +} diff --git a/test/ConnectionTest.php b/test/ConnectionTest.php index 31271d8..50a4aa8 100644 --- a/test/ConnectionTest.php +++ b/test/ConnectionTest.php @@ -225,4 +225,33 @@ public function testRefusedConnection() $c->connect(); $this->assertFalse($this->c->isConnected()); } + + /** + * Test having a class a callable + * + * @return void + */ + public function testCallableRequest() + { + $testCallable = new CallableClass(); + + $i = 0; + do { + $this->c->subscribe( + 'sayhello' . $i, + [$testCallable, 'requestSubTest'] + ); + + /* Test __invoke */ + $this->c->request( + 'sayhello' . $i, + 'McFly ' . $i, + $testCallable + ); + $testCallable->test($i, function ($msg, $i) { + $this->assertEquals('Hello, McFly ' . $i, $msg); + }); + $i++; + } while ($i < 100); + } } From a537e4e05fd991364df4785ca28588be613786cc Mon Sep 17 00:00:00 2001 From: Andreas Sundqvist Date: Mon, 16 Oct 2017 18:22:15 +0200 Subject: [PATCH 2/2] Fix code to be in compliance with psr2 --- src/Nats/Connection.php | 4 ++-- test/ConnectionOptionsTest.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Nats/Connection.php b/src/Nats/Connection.php index f8cefce..f83002f 100644 --- a/src/Nats/Connection.php +++ b/src/Nats/Connection.php @@ -408,7 +408,7 @@ private function handleMSG($line) if (count($parts) === 5) { $length = trim($parts[4]); $subject = $parts[3]; - } else if (count($parts) === 4) { + } elseif (count($parts) === 4) { $length = trim($parts[3]); $subject = $parts[1]; } @@ -587,7 +587,7 @@ public function wait($quantity = 0) { $count = 0; $info = stream_get_meta_data($this->streamSocket); - while (is_resource($this->streamSocket) === true && feof($this->streamSocket) === false && empty($info['timed_out']) === true) { + while (is_resource($this->streamSocket) && feof($this->streamSocket) === false && empty($info['timed_out'])) { $line = $this->receive(); if ($line === false) { diff --git a/test/ConnectionOptionsTest.php b/test/ConnectionOptionsTest.php index 23d12b7..e05fd49 100644 --- a/test/ConnectionOptionsTest.php +++ b/test/ConnectionOptionsTest.php @@ -18,7 +18,16 @@ class ConnectionOptionsTest extends \PHPUnit_Framework_TestCase public function testSettersAndGetters() { $options = new ConnectionOptions(); - $options->setHost('host')->setPort(4222)->setUser('user')->setPass('password')->setLang('lang')->setVersion('version')->setVerbose(true)->setPedantic(true)->setReconnect(true); + $options + ->setHost('host') + ->setPort(4222) + ->setUser('user') + ->setPass('password') + ->setLang('lang') + ->setVersion('version') + ->setVerbose(true) + ->setPedantic(true) + ->setReconnect(true); $this->assertEquals('host', $options->getHost()); $this->assertEquals(4222, $options->getPort()); @@ -40,7 +49,15 @@ public function testSettersAndGetters() public function testAuthToken() { $options = new ConnectionOptions(); - $options->setHost('host')->setPort(4222)->setToken('token')->setLang('lang')->setVersion('version')->setVerbose(true)->setPedantic(true)->setReconnect(true); + $options + ->setHost('host') + ->setPort(4222) + ->setToken('token') + ->setLang('lang') + ->setVersion('version') + ->setVerbose(true) + ->setPedantic(true) + ->setReconnect(true); $this->assertEquals('host', $options->getHost()); $this->assertEquals(4222, $options->getPort()); @@ -63,7 +80,14 @@ public function testAuthToken() public function testSettersAndGettersWithoutCredentials() { $options = new ConnectionOptions(); - $options->setHost('host')->setPort(4222)->setLang('lang')->setVersion('version')->setVerbose(true)->setPedantic(true)->setReconnect(true); + $options + ->setHost('host') + ->setPort(4222) + ->setLang('lang') + ->setVersion('version') + ->setVerbose(true) + ->setPedantic(true) + ->setReconnect(true); $this->assertEquals('host', $options->getHost()); $this->assertEquals(4222, $options->getPort());