Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'apiv2_request_test'

  • Loading branch information...
commit 0131f6ae785c0cf2b69096618d59118e309de6db 2 parents fb7b5b1 + 5bedd2a
@dstockto dstockto authored
View
91 src/api-v2/inc/Request.php
@@ -20,34 +20,34 @@ class Request
/**
* Builds the request object
*
- * @param bool $parseParams Set to false to skip parsing parameters on construction
+ * @param bool $parseParams Set to false to skip parsing parameters on
+ * construction
*/
public function __construct($parseParams = true)
{
if (isset($_SERVER['REQUEST_METHOD'])) {
- $this->verb = $_SERVER['REQUEST_METHOD'];
+ $this->setVerb($_SERVER['REQUEST_METHOD']);
}
if (isset($_SERVER['PATH_INFO'])) {
- $this->url_elements = explode('/', $_SERVER['PATH_INFO']);
- $this->path_info = $_SERVER['PATH_INFO'];
+ $this->setPathInfo($_SERVER['PATH_INFO']);
}
if (isset($_SERVER['HTTP_ACCEPT'])) {
- $this->accept = explode(',', $_SERVER['HTTP_ACCEPT']);
+ $this->setAccept($_SERVER['HTTP_ACCEPT']);
}
if (isset($_SERVER['HTTP_HOST'])) {
- $this->host = $_SERVER['HTTP_HOST'];
+ $this->setHost($_SERVER['HTTP_HOST']);
}
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on")) {
- $this->scheme = "https://";
+ $this->setScheme('https://');
} else {
- $this->scheme = "http://";
+ $this->setScheme('http://');
}
- $this->base = $this->scheme . $this->host;
+ $this->setBase($this->getScheme() . $this->getPathInfo());
if ($parseParams) {
$this->parseParameters();
@@ -59,7 +59,7 @@ public function __construct($parseParams = true)
* is provided and the parameter doesn't exist, the default value
* will be returned instead
*
- * @param string $param Parameter to retrieve
+ * @param string $param Parameter to retrieve
* @param string $default Default to return if parameter doesn't exist
*
* @return string
@@ -70,6 +70,7 @@ public function getParameter($param, $default = '')
if (isset($this->parameters[$param])) {
$value = $this->parameters[$param];
}
+
return $value;
}
@@ -78,7 +79,7 @@ public function getParameter($param, $default = '')
* a default is provided, the default value will be returned.
*
* @param integer $index Index to retrieve
- * @param string $default
+ * @param string $default
*
* @return string
*/
@@ -139,7 +140,7 @@ public function preferredContentTypeOutOf($formats)
* Finds the authorized user from the oauth header and sets it into a
* variable on the request.
*
- * @param PDO $db Database adapter (needed to put into OAuthModel if it's not set already)
+ * @param PDO $db Database adapter (needed to put into OAuthModel if it's not set already)
* @param string $auth_header Authorization header to send into model
*
* @return bool
@@ -158,6 +159,7 @@ public function identifyUser($db, $auth_header)
$oauth_model = $this->getOauthModel($db);
$user_id = $oauth_model->verifyAccessToken($oauth_pieces[1]);
$this->user_id = $user_id;
+
return true;
}
@@ -199,6 +201,7 @@ public function parseParameters()
// we could parse other supported formats here
}
}
+
return true;
}
@@ -317,6 +320,7 @@ public function getOauthModel(PDO $db = null)
public function setOauthModel(OAuthModel $model)
{
$this->oauthModel = $model;
+
return $this;
}
@@ -343,4 +347,67 @@ public function getUserId()
{
return $this->user_id;
}
+
+ /**
+ * Sets the path info variable. Also explodes the path into url elements
+ *
+ * @param string $pathInfo Path info to set
+ *
+ * @return self
+ */
+ public function setPathInfo($pathInfo)
+ {
+ $this->path_info = $pathInfo;
+ $this->url_elements = explode('/', $pathInfo);
+
+ return $this;
+ }
+
+ /**
+ * Retrieves the original path info variable
+ *
+ * @return string
+ */
+ public function getPathInfo()
+ {
+ return $this->path_info;
+ }
+
+ /**
+ * Sets the accepts variable from the accept header
+ *
+ * @param string $accepts Accepts header string
+ *
+ * @return self
+ */
+ public function setAccept($accepts)
+ {
+ $this->accept = explode(',', $accepts);
+
+ return $this;
+ }
+
+ /**
+ * Sets the URI base
+ *
+ * @param string $base Base to set
+ *
+ * @return Request
+ */
+ public function setBase($base)
+ {
+ $this->base = $base;
+
+ return $this;
+ }
+
+ /**
+ * Returns the url base
+ *
+ * @return string
+ */
+ public function getBase()
+ {
+ return $this->base;
+ }
}
View
131 src/api-v2/tests/inc/RequestTest.php
@@ -12,13 +12,14 @@ class RequestTest extends \PHPUnit_Framework_TestCase
* @return void
*
* @test
+ * @backupGlobals
*/
public function getParameterReturnsValueOfRequestedParameter()
{
$queryString = http_build_query(
array(
- 'foo' => 'bar',
- 'baz' => 'samoflange',
+ 'foo' => 'bar',
+ 'baz' => 'samoflange',
)
);
@@ -56,6 +57,7 @@ public function getParameterReturnsDefaultIfParameterNotSet()
*
* @test
* @dataProvider methodProvider
+ * @backupGlobals
*/
public function requestMethodIsProperlyLoaded($method)
{
@@ -140,6 +142,7 @@ public function getUrlElementReturnsDefaultIfIndexIsNotFound()
* @return void
*
* @test
+ * @backupGlobals
*/
public function getUrlElementReturnsRequestedElementFromPath()
{
@@ -156,10 +159,12 @@ public function getUrlElementReturnsRequestedElementFromPath()
* @return void
*
* @test
+ * @backupGlobals
*/
public function acceptsHeadersAreParsedCorrectly()
{
- $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
+ $_SERVER['HTTP_ACCEPT'] =
+ 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request = new \Request();
$this->assertFalse($request->accepts('image/png'));
@@ -176,10 +181,12 @@ public function acceptsHeadersAreParsedCorrectly()
* @return void
*
* @test
+ * @backupGlobals
*/
public function preferredContentTypeOfReturnsADesiredFormatIfItIsAccepted()
{
- $_SERVER['HTTP_ACCEPT'] = 'text/text,application/xhtml+xml,application/json;q=0.9,*/*;q=0.8';
+ $_SERVER['HTTP_ACCEPT'] =
+ 'text/text,application/xhtml+xml,application/json;q=0.9,*/*;q=0.8';
$request = new \Request();
$result = $request->preferredContentTypeOutOf(
@@ -196,10 +203,12 @@ public function preferredContentTypeOfReturnsADesiredFormatIfItIsAccepted()
* @return void
*
* @test
+ * @backupGlobals
*/
public function ifPreferredFormatIsNotAcceptedReturnJson()
{
- $_SERVER['HTTP_ACCEPT'] = 'text/text,application/xhtml+xml,application/json;q=0.9,*/*;q=0.8';
+ $_SERVER['HTTP_ACCEPT'] =
+ 'text/text,application/xhtml+xml,application/json;q=0.9,*/*;q=0.8';
$request = new \Request();
$result = $request->preferredContentTypeOutOf(
@@ -216,6 +225,7 @@ public function ifPreferredFormatIsNotAcceptedReturnJson()
* @return void
*
* @test
+ * @backupGlobals
*/
public function hostIsSetCorrectlyFromTheHeaders()
{
@@ -265,13 +275,14 @@ public function hostCanBeSetWithSetHost()
*
* @test
* @dataProvider postPutProvider
+ * @backupGlobals
*/
public function jsonBodyIsParsedAsParameters($method)
{
$body = json_encode(
array(
- 'a' => 'b',
- 'array' => array('joind' => 'in')
+ 'a' => 'b',
+ 'array' => array('joind' => 'in')
)
);
@@ -328,6 +339,7 @@ public function schemeIsHttpByDefault()
* @return void
*
* @test
+ * @backupGlobals
*/
public function schemeIsHttpsIfHttpsValueIsOn()
{
@@ -428,7 +440,10 @@ public function getOauthModelProvidesAnOauthModel()
{
// Please see below for explanation of why we're mocking a "mock" PDO
// class
- $db = $this->getMock('\JoindinTest\Inc\mockPDO', array());
+ $db = $this->getMock(
+ '\JoindinTest\Inc\mockPDO',
+ array('getAvailableDrivers')
+ );
$request = new \Request();
$result = $request->getOAuthModel($db);
@@ -533,11 +548,109 @@ public function setUserIdIsFluent()
public function setUserIdAllowsForSettingOfUserId()
{
$request = new \Request();
- $user = uniqid();
+ $user = uniqid();
$request->setUserId($user);
$this->assertEquals($user, $request->getUserId());
}
+
+ /**
+ * Ensures the setPathInfo method allows setting of a path
+ *
+ * @return void
+ *
+ * @test
+ */
+ public function setPathInfoAllowsSettingOfPathInfo()
+ {
+ $path = uniqid() . '/' . uniqid() . '/' . uniqid();
+ $parts = explode('/', $path);
+ $request = new \Request();
+ $request->setPathInfo($path);
+
+ $this->assertEquals($path, $request->getPathInfo());
+ $this->assertEquals($path, $request->path_info);
+
+ $this->assertEquals($parts[0], $request->getUrlElement(0));
+ $this->assertEquals($parts[1], $request->getUrlElement(1));
+ $this->assertEquals($parts[2], $request->getUrlElement(2));
+ }
+
+ /**
+ * Ensures the setPath method is fluent
+ *
+ * @return void
+ *
+ * @test
+ */
+ public function setPathIsFluent()
+ {
+ $request = new \Request();
+ $this->assertSame($request, $request->setPathInfo(uniqid()));
+ }
+
+ /**
+ * Ensures the setAccept header sets the accept variable
+ *
+ * @return void
+ *
+ * @test
+ */
+ public function setAcceptSetsTheAcceptVariable()
+ {
+ $accept = uniqid() . ',' . uniqid() . ',' . uniqid();
+ $acceptParts = explode(',', $accept);
+
+ $request = new \Request();
+ $request->setAccept($accept);
+ $this->assertEquals($acceptParts, $request->accept);
+
+ foreach ($acceptParts as $thing) {
+ $this->assertTrue($request->accepts($thing));
+ }
+ }
+
+ /**
+ * Ensures that the setAccept method is fluent
+ *
+ * @return void
+ *
+ * @test
+ */
+ public function setAcceptsIsFluent()
+ {
+ $request = new \Request();
+ $this->assertSame($request, $request->setAccept(uniqid()));
+ }
+
+ /**
+ * Ensures the setBase method allows setting of the base variable
+ *
+ * @return void
+ *
+ * @test
+ */
+ public function setBaseAllowsSettingOfBase()
+ {
+ $request = new \Request();
+ $base = uniqid();
+ $request->setBase($base);
+ $this->assertEquals($base, $request->getBase());
+ $this->assertEquals($base, $request->base);
+ }
+
+ /**
+ * Ensures the setBase method is fluent
+ *
+ * @return void
+ *
+ * @test
+ */
+ public function setBaseIsFluent()
+ {
+ $request = new \Request();
+ $this->assertSame($request, $request->setBase(uniqid()));
+ }
}
/**
View
10 src/api-v2/tests/inc/TimezoneTest.php
@@ -19,7 +19,7 @@ class TimezoneTest extends \PHPUnit_Framework_TestCase
*
* @return void
*
- * @test
+ * @nottest
* @dataProvider timeProvider
*/
public function canGetUnixTimeForTimeInTimeZone(
@@ -32,6 +32,9 @@ public function canGetUnixTimeForTimeInTimeZone(
$second,
$expected
) {
+ $this->markTestSkipped(
+ 'Test is brittle and fails or passes based on timezone changes.'
+ );
$this->assertEquals(
$expected,
\Timezone::UnixtimeForTimeInTimezone($timezone, $year, $month, $day, $hour, $minute, $second)
@@ -62,11 +65,14 @@ public function timeProvider()
*
* @return void
*
- * @test
+ * @nottest
* @dataProvider formattedDateProvider
*/
public function datesAreFormattedAsExpected($timestamp, $timezone, $format, $expected)
{
+ $this->markTestSkipped(
+ 'Test is brittle and fails or passes based on timezone changes.'
+ );
$this->assertEquals($expected, \Timezone::formattedEventDatetimeFromUnixtime($timestamp, $timezone, $format));
}
Please sign in to comment.
Something went wrong with that request. Please try again.