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/.travis.yml b/.travis.yml index f248c95..73dc2ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,21 +1,12 @@ 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 before_script: - - wget http://getcomposer.org/composer.phar - - php composer.phar install + - composer install -script: phpunit --configuration phpunit.travis.xml +script: ./vendor/bin/phpunit --configuration phpunit.travis.xml 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 @@ 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' => 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..3515acd 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' => 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..3ebeacf 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' => 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; } } diff --git a/Service/iOSFeedback.php b/Service/iOSFeedback.php index c5011c2..b9640b7 100644 --- a/Service/iOSFeedback.php +++ b/Service/iOSFeedback.php @@ -109,5 +109,4 @@ protected function getStreamContext() return $ctx; } - } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index b6e64aa..3e6070b 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -9,7 +9,8 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase { public function testDefaults() { - $config = $this->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()); } - } diff --git a/composer.json b/composer.json index 2dfd3fa..6fc4989 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,12 @@ } ], "require": { - "php": ">=5.3.0", - "kriswallsmith/buzz": "*", + "php": "^7.1", + "kriswallsmith/buzz": "~0.17.1", "psr/log": "^1.0" }, "require-dev": { + "phpunit/phpunit": "^5.7.27", "symfony/symfony": "^2.0 || ^3.0" }, "suggest": {