Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #74 from sbisbee/tests_strict
Browse files Browse the repository at this point in the history
Tests and STRICT mode fixes

Time for an 0.8.1 release, me thinks.
  • Loading branch information
BigBlueHat committed Nov 5, 2013
2 parents 57247a5 + 05975c0 commit 5cd5f66
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 30 deletions.
111 changes: 111 additions & 0 deletions CONTRIBUTE
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Contributing to Sag
===================

First off - you rock! Community contributions are so awesome and have resulted
in some awesome features landing in Sag.

This guide is meant to make it as easy as possible to contribute your code to
Sag.

Sending the Contribution
------------------------

1. Fork the Sag project. If you have a GitHub account this is very easy: just
hit "fork" on the Sag page and you will get your own repo.
http://help.github.com/fork-a-repo/

2. Every feature or bug fix should be in its own branch. Ideally the branch
name will be related to what you're contributing (iss-32, newFeatureName,
etc.).

3. Make your commits to the branch atomic so that there is a clearly defined
commit for each change you make. For example, if you're writing a function in
Sag.php that relies on new functionality in SagHTTPAdapter.php, make one commit
for the SagHTTPAdapter.php work and then commit your work in Sag.php. This is
just git best practices.

4. **Write tests!** One of the reasons Sag is so popular and good is that we
use an automated testing framework to prevent regressions, check for
compatibility against new versions of CouchDB, etc. **Your contribution will
not be accepted without supporting tests.**

5. Your code is not complete until you run `make check` in Sag's root
directory. Your code will not land in Sag if it breaks tests.

6. Once your work is done issue a pull request to the Sag project.
http://help.github.com/send-pull-requests/

Code Style Guide
----------------

It is good form to follow a project's coding style when contributing. Do not
take it upon yourself to rewrite every comment, drop in or delete white spaces
all over the place, etc.

- No hard tabs. Please set your editor to use 2 space soft tabs.

- There is never any reason to use globals or goto statements.

- Don't use single letter variable names. The exception to this rule is if you
are iterating over an array or object and use `$i`.

- Use camel case, not underscores. That's why we have `usingSSL()` instead of
`using_ssl()`.

- Put curly braces on the same line as the function definition, loop statement,
etc.

Example:

function sillyArraySearch($arr, $val) {
foreach($arr as $k => $v) {
if($v == $val) {
return true;
}
}
}

- We use phpdocs to generate documentation. Therefore every function should
have a properly formatted comment block with parameter and return
information.

Example comment for the above sillyArraySearch function:

/**
* This is a naive example of how to search an array for a given value.
*
* @param array $arr The array to search.
* @param mixed $val The value that we are searching for.
* @return bool Returns true if the array contains the value, else false.
*/

- When considering multiple values for a single variable use a switch instead
of multiple if/else-if blocks. This makes it crystal clear that all of your
logic centers around the value of one variable.

What not to do:

if($foo == 'bwah') {
//...
}
elseif($foo == 1) {
//...
}
else {
throw SagException('Unexpected value for $foo.');
}

What you should do:

switch($foo) {
case 'bwah':
//...
break;

case 1:
//...
break;

default:
throw SagException('Unexpected value for $foo.');
}
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.

# Determine version number from README (will include "-UNRELEASED")
VERSION := $(shell sed --expression '/^Version /!d' --expression 's/^Version //' README)
VERSION := $(shell sed -e '/^Version /!d' -e 's/^Version //' README)

# Main directories and files
PREFIX := .
Expand Down Expand Up @@ -44,7 +44,7 @@ TESTS_CONFIG_CURL := ${TESTS_DIR}/phpunitConfig-cURL.xml
TESTS_CONFIG_SSL_CURL := ${TESTS_DIR}/phpunitConfig-SSL-cURL.xml

TESTS_PHPUNIT_OPTS_BASE := -d "include_path=${TESTS_PHP_INCLUDE_PATH}" \
--strict --process-isolation \
--verbose --strict --process-isolation \
-d "error_reporting=\"E_ALL & E_STRICT\""

TESTS_PHPUNIT_OPTS_NATIVE := ${TESTS_PHPUNIT_OPTS_BASE} --configuration=${TESTS_CONFIG_NATIVE_SOCKETS}
Expand Down
27 changes: 17 additions & 10 deletions src/Sag.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public function setHTTPAdapter($type = null) {
}

// remember what was already set (ie., might have called decode() already)
$prevDecode = null;
$prevTimeouts = null;
if($this->httpAdapter) {
$prevDecode = $this->httpAdapter->decodeResp;
$prevTimeouts = $this->httpAdapter->getTimeouts();
Expand Down Expand Up @@ -262,6 +264,7 @@ public function get($url) {
}

//Deal with cached items
$response = null;
if($this->cache) {
$prevResponse = $this->cache->get($url);

Expand Down Expand Up @@ -960,7 +963,7 @@ public function setCookie($key, $value) {
* @see setCookie()
*/
public function getCookie($key) {
return ($this->globalCookies[$key]) ? $this->globalCookies[$key] : null;
return (!empty($this->globalCookies[$key])) ? $this->globalCookies[$key] : null;
}

/**
Expand Down Expand Up @@ -1059,15 +1062,17 @@ private function procPacket($method, $url, $data = null, $headers = array()) {
}

// Filter the use of the Expect header since we don't support Continue headers.
if(strtolower($headers['expect']) === '100-continue' || strtolower($headers['Expect']) === '100-continue') {
throw new SagException('Sag does not support HTTP/1.1\'s Continue.');
}
else if(!$headers['expect'] && !$headers['Expect']) {
/*
$headers['Expect'] = isset($headers['Expect']) ? $headers['Expect'] : null;
if(!$headers['Expect']){
/*
* PHP cURL will set the Expect header to 100-continue if we don't set it
* ourselves. See https://github.com/sbisbee/sag/pull/51
*/
$headers['Expect'] = ' '; //1 char string, so it's == to true
$headers['Expect'] = (isset($headers['expect']) && $headers['expect']) ? $headers['expect'] : ' '; //1 char string, so it's == to true
}

if(strtolower($headers['Expect']) === '100-continue') {
throw new SagException('Sag does not support HTTP/1.1\'s Continue.');
}

// Do some string replacing for HTTP sanity.
Expand Down Expand Up @@ -1107,7 +1112,7 @@ private function procPacket($method, $url, $data = null, $headers = array()) {
* Checking this again because $headers['Cookie'] could be set in two
* different logic paths above.
*/
if($headers['Cookie']) {
if(!empty($headers['Cookie'])) {
$buff = '';

foreach($headers['Cookie'] as $k => $v) {
Expand Down Expand Up @@ -1148,8 +1153,10 @@ private function procPacket($method, $url, $data = null, $headers = array()) {
*/
private function setURLParameter($url, $key, $value) {
$url = parse_url($url);

parse_str($url['query'], $params);

if(!empty($url['query'])) {
parse_str($url['query'], $params);
}
$params[$key] = $value;

return $url = $url['path'].'?'.http_build_query($params);
Expand Down
2 changes: 1 addition & 1 deletion src/httpAdapters/SagCURLHTTPAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
$line[0] = strtolower($line[0]);
$response->headers->$line[0] = ltrim($line[1]);

if($line[0] == 'Set-Cookie') {
if($line[0] == 'set-cookie') {
$response->cookies = $this->parseCookieString($line[1]);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/httpAdapters/SagHTTPAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ protected function parseCookieString($cookieStr) {

foreach(explode('; ', $cookieStr) as $cookie) {
$crumbs = explode('=', $cookie);
if(!isset($crumbs[1])) {
$crumbs[1] = '';
}
$cookies->{trim($crumbs[0])} = trim($crumbs[1]);
}

Expand Down
16 changes: 6 additions & 10 deletions src/httpAdapters/SagNativeHTTPAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
*
* And we can't use a ternary because fgets() wants an int or undefined.
*/
if(!$isHeader && $response->headers->{'transfer-encoding'} !== 'chunked') {
if(!$isHeader && !empty($response->headers->{'transfer-encoding'})
&& $response->headers->{'transfer-encoding'} !== 'chunked') {
//the +1 is because fgets() reads (length - 1) bytes
$line = fgets($sock, $response->headers->{'content-length'} - strlen($response->body) + 1);
}
Expand Down Expand Up @@ -207,13 +208,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe

switch($line[0]) {
case 'set-cookie':
$response->cookies = new stdClass();

foreach(explode('; ', $line[1]) as $cookie) {
$crumbs = explode('=', $cookie);
$response->cookies->{trim($crumbs[0])} = trim($crumbs[1]);
}

$response->cookies = $this->parseCookieString($line[1]);
break;

case 'location':
Expand All @@ -236,7 +231,7 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
}
}
}
else if($response->headers->{'transfer-encoding'}) {
else if(!empty($response->headers->{'transfer-encoding'})) {
/*
* Parse the response's body, which is being sent in chunks. Welcome to
* HTTP/1.1 land.
Expand Down Expand Up @@ -301,7 +296,8 @@ public function procPacket($method, $url, $data = null, $headers = array(), $spe
}

// HTTP/1.1 assumes persisted connections, but proxies might close them.
if(strtolower($response->headers->connection) != 'close') {
if(!empty($response->headers->connection)
&& strtolower($response->headers->connection) != 'close') {
$this->connPool[] = $sock;
}

Expand Down
13 changes: 8 additions & 5 deletions tests/SagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setUp()
$this->couchAdminName = ($GLOBALS['adminName']) ? $GLOBALS['adminName'] : 'admin';
$this->couchAdminPass = ($GLOBALS['adminPass']) ? $GLOBALS['adminPass'] : 'passwd';
$this->couchHTTPAdapter = $GLOBALS['httpAdapter'];
$this->couchSSL = ($GLOBALS['ssl']) ? $GLOBALS['ssl'] : false;
$this->couchSSL = (isset($GLOBALS['ssl'])) ? $GLOBALS['ssl'] : false;

$this->couch = new Sag($this->couchIP, $this->couchPort);
$this->couch->setHTTPAdapter($this->couchHTTPAdapter);
Expand Down Expand Up @@ -135,7 +135,7 @@ public function test_twoPosts() {
*/
$resp = $this->couch->get($resp->body->id);
$this->assertEquals($resp->body->two, $docs[1]['two']);
$this->assertNotEquals($resp->body->one, $docs[0]['one']);
$this->assertFalse(isset($resp->body->one));
}

public function test_getID()
Expand Down Expand Up @@ -220,10 +220,13 @@ public function test_queryEmptyView() {

$this->assertTrue(is_object($result->headers), 'Parsed headers');
$this->assertTrue(is_object($result->headers->_HTTP), 'Parsed first line');
$this->assertEqual($result->headers->_HTTP->status, 200, 'HTTP status code');
$this->assertEquals($result->headers->_HTTP->status, 200, 'HTTP status code');
$this->assertTrue(is_object($result->body), 'Make sure we parsed the JSON object properly');
$this->assertTrue(is_array($result->body->rows), 'Rows is an array');
$this->assertEqual(sizeof($result->body->rows), 0, 'Empty rows array');
$this->assertEquals(sizeof($result->body->rows), 0, 'Empty rows array');

// delete design doc for future use
$this->couch->delete('/_design/app', $ddocResult->body->rev);
}

public function test_getIDNoDecode()
Expand Down Expand Up @@ -498,7 +501,7 @@ public function test_attachments()

public function test_createSession() {
$resp = $this->session_couch->login($this->couchAdminName, $this->couchAdminPass, Sag::$AUTH_COOKIE);
$this->assertTrue(is_string($resp->body), 'Got a string back');
$this->assertTrue(is_string($resp), 'Got a string back');
}

public function test_getSession() {
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ do
done

# Check PHP version number
echo -n "Checking for PHP 5.2, 5.3, or 5.4..."
php -v | grep '^PHP 5.[234]' > /dev/null
echo -n "Checking for PHP 5.2, 5.3, 5.4, or 5.5..."
php -v | grep '^PHP 5.[2345]' > /dev/null
[ $? -eq 0 ] && success || failure

# Check for /tmp/sag/
Expand Down

0 comments on commit 5cd5f66

Please sign in to comment.