Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

POST as JSON to the Twitter API #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions library/ZendService/Twitter/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class Twitter
*/
protected $username;

/**
* Flags to use with json_encode for POST requests
*
* @var int
*/
private $jsonFlags = JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

/**
* @param null|array|Traversable $options
* @param null|OAuth\Consumer $consumer
Expand Down Expand Up @@ -1233,19 +1240,25 @@ protected function post($path, $data = null)
*
* Performs a POST or PUT request. Any data provided is set in the HTTP
* client. String data is pushed in as raw POST data; array or object data
* is pushed in as POST parameters.
* is JSON-encoded before being passed to the request body.
*
* @param mixed $method
* @param mixed $data
* @return Http\Response
*/
protected function performPost($method, $data, Http\Client $client)
{
if (is_string($data)) {
$client->setRawData($data);
} elseif (is_array($data) || is_object($data)) {
$client->setParameterPost((array) $data);
if (is_array($data) || is_object($data)) {
$data = json_encode($data, $this->jsonFlags);
}

if (! empty($data)) {
$client->setRawBody($data);
$client->getRequest()
->getHeaders()
->addHeaderLine('Content-Type', 'application/json');
}

$client->setMethod($method);
return $client->send();
}
Expand Down
24 changes: 21 additions & 3 deletions tests/ZendService/Twitter/TwitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use ReflectionProperty;
use Zend\Http;
use ZendOAuth\Client as OAuthClient;
use ZendOAuth\Consumer as OAuthConsumer;
Expand All @@ -25,6 +26,14 @@

class TwitterTest extends TestCase
{
public function setUp()
{
$twitter = new Twitter\Twitter();
$r = new ReflectionProperty($twitter, 'jsonFlags');
$r->setAccessible(true);
$this->jsonFlags = $r->getValue($twitter);
}

/**
* Quick reusable OAuth client stub setup. Its purpose is to fake
* HTTP interactions with Twitter so the component can focus on what matters:
Expand Down Expand Up @@ -58,9 +67,18 @@ protected function stubOAuthClient(
$client->setHeaders(['Accept-Charset' => 'ISO-8859-1,utf-8'])->will([$client, 'reveal']);
$client->clearCookies()->will([$client, 'reveal']);
$client->getCookies()->willReturn([]);
if (null !== $params) {
$setter = 'setParameter' . ucfirst(strtolower($method));
$client->$setter($params)->shouldBeCalled();
if (null !== $params && $method === 'GET') {
$client->setParameterGet($params)->shouldBeCalled();
}
if (null !== $params && $method === 'POST') {
$requestBody = json_encode($params, $this->jsonFlags);
$client->setRawBody($requestBody)->shouldBeCalled();

$headers = $this->prophesize(Http\Headers::class);
$headers->addHeaderLine('Content-Type', 'application/json')->shouldBeCalled();
$request = $this->prophesize(Http\Request::class);
$request->getHeaders()->will([$headers, 'reveal']);
$client->getRequest()->will([$request, 'reveal']);
}

$response = $this->prophesize(Http\Response::class);
Expand Down