From 7daa211b0bf028243d8038bef3ea725f2de9f860 Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 09:32:06 +0100 Subject: [PATCH 1/7] #167 - specify Buzz lib version; add appropriate phpunit locked version for dev unit testing --- .gitignore | 5 ++++- composer.json | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5657f6e..86eddcb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -vendor \ No newline at end of file +composer.lock +vendor + +.idea/ diff --git a/composer.json b/composer.json index 2dfd3fa..251c7e0 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,11 @@ ], "require": { "php": ">=5.3.0", - "kriswallsmith/buzz": "*", + "kriswallsmith/buzz": "~0.17.1", "psr/log": "^1.0" }, "require-dev": { + "phpunit/phpunit": "5.*", "symfony/symfony": "^2.0 || ^3.0" }, "suggest": { From ebfda10c2a93896b34b3df812e359a944610960a Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 09:35:56 +0100 Subject: [PATCH 2/7] #167 - updates to work with Buzz v0.17+, PSR-7 requests & responses, and middleware over listeners --- Service/OS/AndroidGCMNotification.php | 32 ++++++++++++++++++--------- Service/OS/AndroidNotification.php | 22 +++++++++--------- Service/OS/BlackberryNotification.php | 24 ++++++++++---------- Service/OS/MicrosoftNotification.php | 14 +++++++----- 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/Service/OS/AndroidGCMNotification.php b/Service/OS/AndroidGCMNotification.php index 69a1c1a..e8b82b6 100644 --- a/Service/OS/AndroidGCMNotification.php +++ b/Service/OS/AndroidGCMNotification.php @@ -2,14 +2,15 @@ namespace RMS\PushNotificationsBundle\Service\OS; -use Psr\Log\LoggerInterface; -use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, - RMS\PushNotificationsBundle\Message\AndroidMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; use Buzz\Browser, Buzz\Client\AbstractCurl, Buzz\Client\Curl, Buzz\Client\MultiCurl; +use Nyholm\Psr7\Request; +use Psr\Log\LoggerInterface; +use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, + RMS\PushNotificationsBundle\Message\AndroidMessage, + RMS\PushNotificationsBundle\Message\MessageInterface; class AndroidGCMNotification implements OSNotificationServiceInterface { @@ -63,6 +64,13 @@ class AndroidGCMNotification implements OSNotificationServiceInterface */ protected $logger; + /** + * Buzz request client options as associative array. + * + * @var array + */ + protected $clientOptions; + /** * Constructor * @@ -77,13 +85,15 @@ public function __construct($apiKey, $useMultiCurl, $timeout, $logger, AbstractC { $this->useDryRun = $dryRun; $this->apiKey = $apiKey; + // We'll need to set this per-request if `$client` was provided in constructor. + $this->clientOptions = array( + 'timeout' => $timeout, + 'verify_peer' => false, + ); if (!$client) { - $client = ($useMultiCurl ? new MultiCurl() : new Curl()); + $client = ($useMultiCurl ? new MultiCurl($this->clientOptions) : new Curl($this->clientOptions)); } - $client->setTimeout($timeout); - $this->browser = new Browser($client); - $this->browser->getClient()->setVerifyPeer(false); $this->logger = $logger; } @@ -122,14 +132,16 @@ public function send(MessageInterface $message) if (count($message->getGCMIdentifiers()) == 1) { $data['to'] = $gcmIdentifiers[0]; - $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); + $request = new Request('POST', $this->apiURL, $headers, json_encode($data)); + $this->responses[] = $this->browser->getClient()->sendRequest($request, $this->clientOptions); } else { // Chunk number of registration IDs according to the maximum allowed by GCM $chunks = array_chunk($message->getGCMIdentifiers(), $this->registrationIdMaxCount); foreach ($chunks as $registrationIDs) { $data['registration_ids'] = $registrationIDs; - $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); + $request = new Request('POST', $this->apiURL, $headers, json_encode($data)); + $this->responses[] = $this->browser->getClient()->sendRequest($request, $this->clientOptions); } } diff --git a/Service/OS/AndroidNotification.php b/Service/OS/AndroidNotification.php index 2e35ea4..c19e40d 100644 --- a/Service/OS/AndroidNotification.php +++ b/Service/OS/AndroidNotification.php @@ -2,6 +2,7 @@ namespace RMS\PushNotificationsBundle\Service\OS; +use Buzz\Client\Curl; use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, RMS\PushNotificationsBundle\Message\AndroidMessage, RMS\PushNotificationsBundle\Message\MessageInterface; @@ -32,11 +33,11 @@ class AndroidNotification implements OSNotificationServiceInterface protected $source; /** - * Timeout in seconds for the connecting client + * Buzz request client options as associative array. * - * @var int + * @var array */ - protected $timeout; + protected $clientOptions; /** * Authentication token @@ -51,14 +52,17 @@ class AndroidNotification implements OSNotificationServiceInterface * @param $username * @param $password * @param $source - * @param $timeout + * @param int $timeout Timeout in seconds */ public function __construct($username, $password, $source, $timeout) { $this->username = $username; $this->password = $password; $this->source = $source; - $this->timeout = $timeout; + $this->clientOptions = array( + 'timeout' => $timeout, + 'verify_peer' => false, + ); $this->authToken = ""; } @@ -80,9 +84,7 @@ public function send(MessageInterface $message) $headers[] = "Authorization: GoogleLogin auth=" . $this->authToken; $data = $message->getMessageBody(); - $buzz = new Browser(); - $buzz->getClient()->setVerifyPeer(false); - $buzz->getClient()->setTimeout($this->timeout); + $buzz = new Browser(new Curl($this->clientOptions)); $response = $buzz->post("https://android.apis.google.com/c2dm/send", $headers, http_build_query($data)); return preg_match("/^id=/", $response->getContent()) > 0; @@ -106,9 +108,7 @@ protected function getAuthToken() "service" => "ac2dm" ); - $buzz = new Browser(); - $buzz->getClient()->setVerifyPeer(false); - $buzz->getClient()->setTimeout($this->timeout); + $buzz = new Browser(new Curl($this->clientOptions)); $response = $buzz->post("https://www.google.com/accounts/ClientLogin", array(), http_build_query($data)); if ($response->getStatusCode() !== 200) { return false; diff --git a/Service/OS/BlackberryNotification.php b/Service/OS/BlackberryNotification.php index acc4b4c..49ec354 100644 --- a/Service/OS/BlackberryNotification.php +++ b/Service/OS/BlackberryNotification.php @@ -2,13 +2,14 @@ namespace RMS\PushNotificationsBundle\Service\OS; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerInterface, + Psr\Http\Message\ResponseInterface; use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException, RMS\PushNotificationsBundle\Message\BlackberryMessage, RMS\PushNotificationsBundle\Message\MessageInterface; use Buzz\Browser, - Buzz\Listener\BasicAuthListener, - Buzz\Client\Curl; + Buzz\Client\Curl, + Buzz\Middleware\BasicAuthMiddleware; class BlackberryNotification implements OSNotificationServiceInterface { @@ -91,10 +92,8 @@ protected function doSend(BlackberryMessage $message) { $separator = "mPsbVQo0a68eIL3OAxnm"; $body = $this->constructMessageBody($message, $separator); - $browser = new Browser(new Curl()); - $browser->getClient()->setTimeout($this->timeout); - $listener = new BasicAuthListener($this->appID, $this->password); - $browser->addListener($listener); + $browser = new Browser(new Curl(array('timeout' => $this->timeout))); + $browser->addMiddleware(new BasicAuthMiddleware($this->appID, $this->password)); $url = "https://pushapi.na.blackberry.com/mss/PD_pushRequest"; if ($this->evaluation) { @@ -113,7 +112,7 @@ protected function doSend(BlackberryMessage $message) /** * Builds the actual body of the message * - * @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message + * @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message * @param $separator * @return string */ @@ -143,16 +142,17 @@ protected function constructMessageBody(BlackberryMessage $message, $separator) * Handles and parses the response * Returns a value indicating success/fail * - * @param \Buzz\Message\Response $response + * @param ResponseInterface $response * @return bool */ - protected function parseResponse(\Buzz\Message\Response $response) + protected function parseResponse(ResponseInterface $response) { - if (null !== $response->getStatusCode() && $response->getStatusCode() != 200) { + if (null !== $response->getStatusCode() && $response->getStatusCode() !== 200) { return false; } + $response->getBody()->rewind(); $doc = new \DOMDocument(); - $doc->loadXML($response->getContent()); + $doc->loadXML($response->getBody()->getContents()); $elems = $doc->getElementsByTagName("response-result"); if (!$elems->length) { $this->logger->error('Response is empty'); diff --git a/Service/OS/MicrosoftNotification.php b/Service/OS/MicrosoftNotification.php index 204b1f0..5d9e103 100644 --- a/Service/OS/MicrosoftNotification.php +++ b/Service/OS/MicrosoftNotification.php @@ -31,9 +31,11 @@ class MicrosoftNotification implements OSNotificationServiceInterface */ public function __construct($timeout, $logger) { - $this->browser = new Browser(new Curl()); - $this->browser->getClient()->setVerifyPeer(false); - $this->browser->getClient()->setTimeout($timeout); + $options = array( + 'timeout' => $timeout, + 'verify_peer' => false, + ); + $this->browser = new Browser(new Curl($options)); $this->logger = $logger; } @@ -61,10 +63,12 @@ public function send(MessageInterface $message) $response = $this->browser->post($message->getDeviceIdentifier(), $headers, $xml->asXML()); - if (!$response->isSuccessful()) { + if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { $this->logger->error($response->getStatusCode(). ' : '. $response->getReasonPhrase()); + + return false; } - return $response->isSuccessful(); + return true; } } From e284ede9d2a27329978c7e0ad21564c1cba02aee Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 09:36:46 +0100 Subject: [PATCH 3/7] #167 - minor tweaks for readability and code style consistency --- .../Compiler/AddHandlerPass.php | 2 -- Service/EventListener.php | 1 - Service/EventListenerInterface.php | 8 +++--- Service/iOSFeedback.php | 1 - .../DependencyInjection/ConfigurationTest.php | 25 ++++++++++--------- Tests/Message/AndroidMessageTest.php | 3 +-- Tests/Message/BlackberryMessageTest.php | 3 +-- Tests/Message/MacMessageTest.php | 3 +-- Tests/Message/WindowsphoneMessageTest.php | 3 +-- Tests/Message/iOSMessageTest.php | 4 +-- 10 files changed, 22 insertions(+), 31 deletions(-) diff --git a/DependencyInjection/Compiler/AddHandlerPass.php b/DependencyInjection/Compiler/AddHandlerPass.php index 3151328..a8861c1 100644 --- a/DependencyInjection/Compiler/AddHandlerPass.php +++ b/DependencyInjection/Compiler/AddHandlerPass.php @@ -4,9 +4,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface, Symfony\Component\DependencyInjection\ContainerBuilder, - Symfony\Component\DependencyInjection\Definition, Symfony\Component\DependencyInjection\Reference, - RMS\PushNotificationsBundle\Device\Types, Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; class AddHandlerPass implements CompilerPassInterface diff --git a/Service/EventListener.php b/Service/EventListener.php index bfe8ba2..d761812 100644 --- a/Service/EventListener.php +++ b/Service/EventListener.php @@ -2,7 +2,6 @@ namespace RMS\PushNotificationsBundle\Service; - class EventListener { /** diff --git a/Service/EventListenerInterface.php b/Service/EventListenerInterface.php index 22d107d..b90f28f 100644 --- a/Service/EventListenerInterface.php +++ b/Service/EventListenerInterface.php @@ -1,8 +1,8 @@ process(array()); + $this->process(array()); + $this->addToAssertionCount(1); // Implicitly 'asserting' no exceptions at this point } /** @@ -20,7 +21,7 @@ public function testAddingAndroidKeyRequiresValues() $arr = array( array("android" => "~"), ); - $config = $this->process($arr); + $this->process($arr); } /** @@ -33,7 +34,7 @@ public function testAndroidRequiresUsername() "android" => array("c2dm" => array("password" => "foo")) ), ); - $config = $this->process($arr); + $this->process($arr); } /** @@ -46,7 +47,7 @@ public function testAndroidRequiresPassword() "android" => array("c2dm" => array("username" => "foo")) ), ); - $config = $this->process($arr); + $this->process($arr); } public function testOldFullAndroid() @@ -100,7 +101,7 @@ public function testGCMRequiresAPIKey() ) ), ); - $config = $this->process($arr); + $this->process($arr); } public function testGCMIsOK() @@ -133,7 +134,7 @@ public function testAddingiOsKeyRequiresValues() $arr = array( array("ios" => "~"), ); - $config = $this->process($arr); + $this->process($arr); } /** @@ -146,7 +147,7 @@ public function testiOSRequiresPEM() "ios" => array("pem" => "") ), ); - $config = $this->process($arr); + $this->process($arr); } public function testFulliOS() @@ -172,7 +173,7 @@ public function testAddingMacKeyRequiresValues() $arr = array( array("mac" => "~"), ); - $config = $this->process($arr); + $this->process($arr); } /** @@ -185,7 +186,7 @@ public function testMacRequiresPEM() "mac" => array("pem" => "") ), ); - $config = $this->process($arr); + $this->process($arr); } public function testFullMac() @@ -213,7 +214,7 @@ public function testBlackberryRequiresAppID() "blackberry" => array("password" => "foo") ), ); - $config = $this->process($arr); + $this->process($arr); } /** @@ -226,7 +227,7 @@ public function testBlackberryRequiresPassword() "blackberry" => array("app_id" => "foo") ), ); - $config = $this->process($arr); + $this->process($arr); } public function testFullBlackberry() @@ -254,7 +255,7 @@ public function testAddingWindowsKeyRequiresValues() "windowsphone" => "~" ), ); - $config = $this->process($arr); + $this->process($arr); } public function testFullWindows() diff --git a/Tests/Message/AndroidMessageTest.php b/Tests/Message/AndroidMessageTest.php index 62d8c76..bbdf295 100644 --- a/Tests/Message/AndroidMessageTest.php +++ b/Tests/Message/AndroidMessageTest.php @@ -3,8 +3,7 @@ namespace RMS\PushNotificationsBundle\Tests\Message; use RMS\PushNotificationsBundle\Device\Types, - RMS\PushNotificationsBundle\Message\AndroidMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; + RMS\PushNotificationsBundle\Message\AndroidMessage; class AndroidMessageTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Message/BlackberryMessageTest.php b/Tests/Message/BlackberryMessageTest.php index aacd5be..17b9c34 100644 --- a/Tests/Message/BlackberryMessageTest.php +++ b/Tests/Message/BlackberryMessageTest.php @@ -3,8 +3,7 @@ namespace RMS\PushNotificationsBundle\Tests\Message; use RMS\PushNotificationsBundle\Device\Types, - RMS\PushNotificationsBundle\Message\BlackberryMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; + RMS\PushNotificationsBundle\Message\BlackberryMessage; class BlackberryMessageTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Message/MacMessageTest.php b/Tests/Message/MacMessageTest.php index a5ffaa2..ff05ffc 100644 --- a/Tests/Message/MacMessageTest.php +++ b/Tests/Message/MacMessageTest.php @@ -3,8 +3,7 @@ namespace RMS\PushNotificationsBundle\Tests\Message; use RMS\PushNotificationsBundle\Device\Types, - RMS\PushNotificationsBundle\Message\MacMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; + RMS\PushNotificationsBundle\Message\MacMessage; class MacMessageTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Message/WindowsphoneMessageTest.php b/Tests/Message/WindowsphoneMessageTest.php index ed36ba5..b8a4026 100644 --- a/Tests/Message/WindowsphoneMessageTest.php +++ b/Tests/Message/WindowsphoneMessageTest.php @@ -3,8 +3,7 @@ namespace RMS\PushNotificationsBundle\Tests\Message; use RMS\PushNotificationsBundle\Device\Types, - RMS\PushNotificationsBundle\Message\WindowsphoneMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; + RMS\PushNotificationsBundle\Message\WindowsphoneMessage; class WindowsphoneMessageTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Message/iOSMessageTest.php b/Tests/Message/iOSMessageTest.php index 49226d3..74e855b 100644 --- a/Tests/Message/iOSMessageTest.php +++ b/Tests/Message/iOSMessageTest.php @@ -3,8 +3,7 @@ namespace RMS\PushNotificationsBundle\Tests\Message; use RMS\PushNotificationsBundle\Device\Types, - RMS\PushNotificationsBundle\Message\iOSMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; + RMS\PushNotificationsBundle\Message\iOSMessage; class iOSMessageTest extends \PHPUnit_Framework_TestCase { @@ -82,5 +81,4 @@ public function testMutableContentAddOk() $msg->setMutableContent(true); $this->assertEquals($expected, $msg->getMessageBody()); } - } From f427929dc1e7a1fd534a2e0f988287c80ce3efd1 Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 09:52:28 +0100 Subject: [PATCH 4/7] #167 - support PHP 7.1+ to match latest Buzz requirements; update Travis config --- .travis.yml | 10 +--------- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index f248c95..8d2e19c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,8 @@ language: php -before_install: - # If PHP >= 5.6, download & install PHPunit 5.7 to avoid builds failures - - if php -r "exit( (int)! version_compare( '$TRAVIS_PHP_VERSION', '5.6', '>=' ) );"; then wget -O phpunit https://phar.phpunit.de/phpunit-5.7.phar && chmod +x phpunit && mkdir ~/bin && mv -v phpunit ~/bin; fi - php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - 7.0 - 7.1 + - 7.2 env: - SYMFONY_VERSION=origin/master diff --git a/composer.json b/composer.json index 251c7e0..6fc4989 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ } ], "require": { - "php": ">=5.3.0", + "php": "^7.1", "kriswallsmith/buzz": "~0.17.1", "psr/log": "^1.0" }, "require-dev": { - "phpunit/phpunit": "5.*", + "phpunit/phpunit": "^5.7.27", "symfony/symfony": "^2.0 || ^3.0" }, "suggest": { From 9256e20241a5ad28076d1aface87952424eb2746 Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 09:58:23 +0100 Subject: [PATCH 5/7] #167 - use our defined PHPUnit 5.7 instead of Travis's --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8d2e19c..28d634b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ before_script: - wget http://getcomposer.org/composer.phar - php composer.phar install -script: phpunit --configuration phpunit.travis.xml +script: ./vendor/bin/phpunit --configuration phpunit.travis.xml From aeea503929fbba3ee935812e00f922557804cde6 Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Thu, 5 Apr 2018 10:21:25 +0100 Subject: [PATCH 6/7] #167 - use Travis's Composer and simplify `before_script` --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 28d634b..73dc2ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ env: - SYMFONY_VERSION=origin/master before_script: - - wget http://getcomposer.org/composer.phar - - php composer.phar install + - composer install script: ./vendor/bin/phpunit --configuration phpunit.travis.xml From 6c2e72ae5c967c28bb45612d6b9bc87c0c94f835 Mon Sep 17 00:00:00 2001 From: Noel Light-Hilary Date: Tue, 26 Jun 2018 23:46:47 +0100 Subject: [PATCH 7/7] #167 - use correct `verify` option for latest Buzz lib This will disable both peer and host SSL verification by cURL --- Service/OS/AndroidGCMNotification.php | 2 +- Service/OS/AndroidNotification.php | 2 +- Service/OS/MicrosoftNotification.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Service/OS/AndroidGCMNotification.php b/Service/OS/AndroidGCMNotification.php index e8b82b6..d33a06f 100644 --- a/Service/OS/AndroidGCMNotification.php +++ b/Service/OS/AndroidGCMNotification.php @@ -88,7 +88,7 @@ public function __construct($apiKey, $useMultiCurl, $timeout, $logger, AbstractC // We'll need to set this per-request if `$client` was provided in constructor. $this->clientOptions = array( 'timeout' => $timeout, - 'verify_peer' => false, + 'verify' => false, ); if (!$client) { $client = ($useMultiCurl ? new MultiCurl($this->clientOptions) : new Curl($this->clientOptions)); diff --git a/Service/OS/AndroidNotification.php b/Service/OS/AndroidNotification.php index c19e40d..3515acd 100644 --- a/Service/OS/AndroidNotification.php +++ b/Service/OS/AndroidNotification.php @@ -61,7 +61,7 @@ public function __construct($username, $password, $source, $timeout) $this->source = $source; $this->clientOptions = array( 'timeout' => $timeout, - 'verify_peer' => false, + 'verify' => false, ); $this->authToken = ""; } diff --git a/Service/OS/MicrosoftNotification.php b/Service/OS/MicrosoftNotification.php index 5d9e103..3ebeacf 100644 --- a/Service/OS/MicrosoftNotification.php +++ b/Service/OS/MicrosoftNotification.php @@ -33,7 +33,7 @@ public function __construct($timeout, $logger) { $options = array( 'timeout' => $timeout, - 'verify_peer' => false, + 'verify' => false, ); $this->browser = new Browser(new Curl($options)); $this->logger = $logger;