Skip to content

Commit

Permalink
EZP-26034: Re add Guzzle5 support to be able to work on php5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerom committed Jul 7, 2016
1 parent 8851e4a commit c7158c7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
sudo: false

php:
- 5.4
- 5.5
- 5.6

Expand Down
41 changes: 41 additions & 0 deletions Client/YooChooseNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(),
Expand Down
38 changes: 26 additions & 12 deletions Tests/Client/YooChooseNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit c7158c7

Please sign in to comment.