From c7158c74005b8d23fe15f01f6f2f3f4914b3d3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Thu, 7 Jul 2016 12:05:41 +0200 Subject: [PATCH] EZP-26034: Re add Guzzle5 support to be able to work on php5.4 --- .travis.yml | 1 + Client/YooChooseNotifier.php | 41 ++++++++++++++++++++++++++ Tests/Client/YooChooseNotifierTest.php | 38 ++++++++++++++++-------- composer.json | 2 +- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3d6cf9..1982dbc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php sudo: false php: + - 5.4 - 5.5 - 5.6 diff --git a/Client/YooChooseNotifier.php b/Client/YooChooseNotifier.php index f8bdace..901406a 100644 --- a/Client/YooChooseNotifier.php +++ b/Client/YooChooseNotifier.php @@ -242,6 +242,7 @@ protected function getContentUri($contentId) * * @param array $events * + * @throws \InvalidArgumentException If provided $events seems to be of wrong type * @throws \GuzzleHttp\Exception\RequestException if a request error occurs */ protected function notify(array $events) @@ -256,6 +257,46 @@ protected function notify(array $events) $this->logger->debug('POST notification to YooChoose:' . json_encode($events, true)); } + if (method_exists($this->guzzle, 'post')) { + $this->notifyGuzzle5($events); + } else { + $this->notifyGuzzle6($events); + } + } + + /** + * Notifies the YooChoose API using Guzzle 5 (for PHP 5.4 support). + * + * @param array $events + */ + private function notifyGuzzle5(array $events) + { + $response = $this->guzzle->post( + $this->getNotificationEndpoint(), + array( + 'json' => array( + 'transaction' => null, + 'events' => $events, + ), + 'auth' => array( + $this->options['customer-id'], + $this->options['license-key'], + ), + ) + ); + + if (isset($this->logger)) { + $this->logger->debug('Got ' . $response->getStatusCode() . ' from YooChoose notification POST'); + } + } + + /** + * Notifies the YooChoose API using Guzzle 6. + * + * @param array $events + */ + private function notifyGuzzle6(array $events) + { $response = $this->guzzle->request( 'POST', $this->getNotificationEndpoint(), diff --git a/Tests/Client/YooChooseNotifierTest.php b/Tests/Client/YooChooseNotifierTest.php index 0716a8c..07bf32b 100644 --- a/Tests/Client/YooChooseNotifierTest.php +++ b/Tests/Client/YooChooseNotifierTest.php @@ -5,8 +5,9 @@ */ namespace EzSystems\RecommendationBundle\Tests\Client; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\Response as PSR7Response; use PHPUnit_Framework_TestCase; +use GuzzleHttp\Message\Response; use eZ\Publish\Core\Repository\Values\Content\Content; use eZ\Publish\Core\Repository\Values\ContentType\ContentType; use eZ\Publish\API\Repository\Values\Content\ContentInfo; @@ -145,17 +146,30 @@ protected function setGuzzleExpectations( $licenseKey, $apiEndpoint ) { - $this->guzzleClientMock - ->expects($this->once()) - ->method('request') - ->with( - 'POST', - $this->equalTo($this->getExpectedEndpoint($apiEndpoint, $customerId)), - $this->equalTo($this->getNotificationBody( - $action, $contentId, $contentTypeId, $serverUri, $customerId, $licenseKey - )) - ) - ->will($this->returnValue(new Response(202))); + if (method_exists($this->guzzleClientMock, 'post')) { + $this->guzzleClientMock + ->expects($this->once()) + ->method('post') + ->with( + $this->equalTo($this->getExpectedEndpoint($apiEndpoint, $customerId)), + $this->equalTo($this->getNotificationBody( + $action, $contentId, $contentTypeId, $serverUri, $customerId, $licenseKey + )) + ) + ->will($this->returnValue(new Response(202))); + } else { + $this->guzzleClientMock + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + $this->equalTo($this->getExpectedEndpoint($apiEndpoint, $customerId)), + $this->equalTo($this->getNotificationBody( + $action, $contentId, $contentTypeId, $serverUri, $customerId, $licenseKey + )) + ) + ->will($this->returnValue(new PSR7Response(200))); + } } /** diff --git a/composer.json b/composer.json index 1662b82..57682a6 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ } ], "require": { - "guzzlehttp/guzzle": "~6.0", + "guzzlehttp/guzzle": "~5.0|~6.0", "components/handlebars.js": "~3.0.0", "symfony/symfony": "~2.6" },