Skip to content

Commit

Permalink
Merge pull request #15 from spwilson806/master
Browse files Browse the repository at this point in the history
Centralize calls and add subsequent tests for curl http class
  • Loading branch information
spwilson806 committed Sep 24, 2015
2 parents acbc552 + 0fbe2fa commit 3a188c0
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/Http/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,27 @@ public function initialize()
}

/**
* Returns string response
* Sets a generic option on the curl resource
*
* @return $this
**/
public function setReturnTransfer()
public function setOpt($opt, $value)
{
curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->resource, $opt, $value);

return $this;
}

/**
* Returns string response
*
* @return $this
**/
public function setReturnTransfer()
{
return $this->setOpt(CURLOPT_RETURNTRANSFER, 1);
}

/**
* Sets the url endpoint
*
Expand All @@ -62,9 +72,7 @@ public function setReturnTransfer()
**/
public function setUrl($url)
{
curl_setopt($this->resource, CURLOPT_URL, $url);

return $this;
return $this->setOpt(CURLOPT_URL, $url);
}

/**
Expand All @@ -89,8 +97,8 @@ public function setPostFields($fields)
$first = false;
}

curl_setopt($this->resource, CURLOPT_POST, count($fields));
curl_setopt($this->resource, CURLOPT_POSTFIELDS, $raw);
$this->setOpt(CURLOPT_POST, count($fields));
$this->setOpt(CURLOPT_POSTFIELDS, $raw);

return $this;
}
Expand All @@ -113,7 +121,7 @@ public function setHeader($header)
$headers[] = $header;
}

curl_setopt($this->resource, CURLOPT_HTTPHEADER, $headers);
$this->setOpt(CURLOPT_HTTPHEADER, $headers);

return $this;
}
Expand Down
180 changes: 180 additions & 0 deletions tests/Http/CurlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
* @package orcid-php
* @author Sam Wilson <samwilson@purdue.edu>
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/

require_once dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Http' . DIRECTORY_SEPARATOR . 'Curl.php';
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Mock' . DIRECTORY_SEPARATOR . 'CurlInterceptor.php';

use Orcid\Http\Curl;

/**
* Base curl functionality tests tests
*/
class CurlTest extends \PHPUnit_Framework_TestCase
{
/**
* Intercept a native function call so we can inspect its arguments
*
* @param closure $closure The call we will be intercepting a response from
* @return string
**/
private function intercept($closure)
{
// Start output buffer
ob_start();

// Make the calls
call_user_func($closure);

// Grag the contents
$contents = ob_get_contents();

// Close the buffer
ob_end_clean();

// Return the contents
return $contents;
}

/**
* Gets a mock curl object with the setOpt overloaded
*
* @return object
**/
private function curl()
{
return $this->getMockBuilder('Orcid\Http\Curl')
->disableOriginalConstructor()
->setMethods(['setOpt'])
->getMock();
}

/**
* Test to make sure we can init a curl instance
*
* @return void
**/
public function testNewCurlCallsInitialize()
{
$call = function()
{
$curl = new Curl;
};

// We'll expect an "INIT" from the init call and a "1" from setting the CURLOPT_RETURNTRANSFER option
$this->assertRegExp('/INIT/', $this->intercept($call), 'New curl object did not initialize the native curl call');
}

/**
* Test to make sure we close the original curl call when resetting
*
* @return void
**/
public function testCurlResetCallsClose()
{
$call = function()
{
$curl = new Curl;
$curl->reset();
};

// We'll expect an "INIT" from the init call and a "1" from setting the CURLOPT_RETURNTRANSFER option
$this->assertRegExp('/CLOSE/', $this->intercept($call), 'Resetting the curl object did not call curl_close');
}

/**
* Test to make sure curl executing makes native curl call
*
* @return void
**/
public function testCurlExecutes()
{
$call = function()
{
$curl = new Curl;
$curl->execute();
};

// We'll expect an "INIT" from the init call and a "1" from setting the CURLOPT_RETURNTRANSFER option
$this->assertRegExp('/EXEC/', $this->intercept($call), 'Executing the curl object did not call curl_exec');
}

/**
* Tests that setting an option calls the underlying curl_setopt with the given param
*
* @return void
**/
public function testSetOptCallsSetOptWithParameter()
{
$call = function()
{
$curl = new Curl;
$curl->setOpt(CURLOPT_URL, 'http://hubzero.org');
};

$this->assertRegExp('/http:\/\/hubzero\.org/', $this->intercept($call), 'The curl_setopt function was not called as expected');
}

/**
* Test to make sure setting a url calls set opt with the given url
*
* @return void
**/
public function testSetUrlCallsSetOptWithUrl()
{
$curl = $this->curl();

$curl->expects($this->exactly(1))
->method('setOpt');

$curl->setUrl('http://hubzero.org');
}

/**
* Test to make sure setting post fields properly encodes
*
* @return void
**/
public function testSetPostFields()
{
$curl = $this->curl();

$curl->expects($this->exactly(2))
->method('setOpt')
->withConsecutive(
[$this->equalTo(CURLOPT_POST), $this->equalTo(2)],
[$this->equalTo(CURLOPT_POSTFIELDS), $this->equalTo('foo=bar&me=you')]
);

$curl->setPostFields([
'foo' => 'bar',
'me' => 'you'
]);
}

/**
* Test to make sure setting headers properly formats them
*
* @return void
**/
public function testSetHeaders()
{
$curl = $this->curl();

$curl->expects($this->exactly(2))
->method('setOpt')
->withConsecutive(
[$this->equalTo(CURLOPT_HTTPHEADER), $this->equalTo(['foo: bar'])],
[$this->equalTo(CURLOPT_HTTPHEADER), $this->equalTo(['foo: bar', 'me: you'])]
);

$curl->setHeader('foo: bar');
$curl->setHeader([
'foo' => 'bar',
'me' => 'you'
]);
}
}
25 changes: 25 additions & 0 deletions tests/Mock/CurlInterceptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* @package orcid-php
* @author Sam Wilson <samwilson@purdue.edu>
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/

namespace Orcid\Http;

function curl_init()
{
echo 'INIT';
}
function curl_exec($ch)
{
echo 'EXEC';
}
function curl_close($ch)
{
echo 'CLOSE';
}
function curl_setopt($ch, $option, $value)
{
echo $value;
}

0 comments on commit 3a188c0

Please sign in to comment.