Skip to content

Commit

Permalink
Reorganize, namespace, composer
Browse files Browse the repository at this point in the history
  • Loading branch information
amouhzi committed Nov 3, 2013
1 parent 02f4d7e commit 8e3569d
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 182 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor/*
*.orig
.buildpath
.project
.settings/*
composer.lock
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "php-mod/curl",
"description": "cURL class for PHP",
"keywords": ["dot", "curl"],
"homepage": "https://github.com/php-mod/curl",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Hassan Amouhzi",
"email": "[email protected]",
"homepage": "http://hassan.amouhzi.com"
}
],
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"Curl": "src/"
}
}
}
4 changes: 4 additions & 0 deletions Curl.class.php → src/Curl/Curl.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace Curl;

class Curl {

const USER_AGENT = 'PHP-Curl-Class/1.0 (+https://github.com/php-curl-class/php-curl-class)';

function __construct() {
Expand Down
183 changes: 183 additions & 0 deletions tests/CurlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
namespace Curl;
include 'helper.inc.php';

class CurlTest extends \PHPUnit_Framework_TestCase {

public function testExtensionLoaded() {

$this->assertTrue(extension_loaded('curl'));
}

public function testUserAgent() {

$test = new \Test();
$test->curl->setUserAgent(Curl::USER_AGENT);
$this->assertEquals(Curl::USER_AGENT, $test->server('GET', array(
'test' => 'server',
'key' => 'HTTP_USER_AGENT',
)));

}

public function testGet() {
$test = new \Test();
$this->assertTrue($test->server('GET', array(
'test' => 'server',
'key' => 'REQUEST_METHOD',
)) === 'GET');
}

public function testPostRequestMethod() {
$test = new \Test();
$this->assertTrue($test->server('POST', array(
'test' => 'server',
'key' => 'REQUEST_METHOD',
)) === 'POST');
}

public function testPostData() {
$test = new \Test();
$this->assertTrue($test->server('POST', array(
'test' => 'post',
'key' => 'test',
)) === 'post');
}

public function testPostMultidimensionalData() {
$test = new \Test();
$this->assertTrue($test->server('POST', array(
'test' => 'post_multidimensional',
'key' => 'file',
'file' => array(
'wibble',
'wubble',
'wobble',
),
)) === 'test=post_multidimensional&key=file&file%5B%5D=wibble&file%5B%5D=wubble&file%5B%5D=wobble');
}

public function testPostFilePathUpload() {
$file_path = get_png();

$test = new \Test();
$this->assertTrue($test->server('POST', array(
'test' => 'post_file_path_upload',
'key' => 'image',
'image' => '@' . $file_path,
)) === 'image/png');

unlink($file_path);
}

public function testPutRequestMethod() {
$test = new \Test();
$this->assertTrue($test->server('PUT', array(
'test' => 'server',
'key' => 'REQUEST_METHOD',
)) === 'PUT');
}

public function testPutData() {
$test = new \Test();
$this->assertTrue($test->server('PUT', array(
'test' => 'put',
'key' => 'test',
)) === 'put');
}

public function testPutFileHandle() {
$png = create_png();
$tmp_file = create_tmp_file($png);

$test = new \Test();
$test->curl->setopt(CURLOPT_PUT, TRUE);
$test->curl->setopt(CURLOPT_INFILE, $tmp_file);
$test->curl->setopt(CURLOPT_INFILESIZE, strlen($png));
$test->curl->put(\Test::TEST_URL, array(
'test' => 'put_file_handle',
));

fclose($tmp_file);

$this->assertTrue($test->curl->response === 'image/png');
}

public function testDelete() {
$test = new \Test();
$this->assertTrue($test->server('DELETE', array(
'test' => 'server',
'key' => 'REQUEST_METHOD',
)) === 'DELETE');

$test = new \Test();
$this->assertTrue($test->server('DELETE', array(
'test' => 'delete',
'key' => 'test',
)) === 'delete');
}

public function testBasicHttpAuth() {
$test = new \Test();
$this->assertTrue($test->server('GET', array(
'test' => 'http_basic_auth',
)) === 'canceled');

$username = 'myusername';
$password = 'mypassword';
$test = new \Test();
$test->curl->setBasicAuthentication($username, $password);
$test->server('GET', array(
'test' => 'http_basic_auth',
));
$json = json_decode($test->curl->response);
$this->assertTrue($json->username === $username);
$this->assertTrue($json->password === $password);
}

public function testReferrer() {
$test = new \Test();
$test->curl->setReferrer('myreferrer');
$this->assertTrue($test->server('GET', array(
'test' => 'server',
'key' => 'HTTP_REFERER',
)) === 'myreferrer');
}

public function testCookies() {
$test = new \Test();
$test->curl->setCookie('mycookie', 'yum');
$this->assertTrue($test->server('GET', array(
'test' => 'cookie',
'key' => 'mycookie',
)) === 'yum');
}

public function testError() {
$test = new \Test();
$test->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000);
$test->curl->get('http://1.2.3.4/');
$this->assertTrue($test->curl->error === TRUE);
$this->assertTrue($test->curl->curl_error === TRUE);
$this->assertTrue($test->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED);
}

public function testHeaders() {
$test = new \Test();
$test->curl->setHeader('Content-Type', 'application/json');
$test->curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$test->curl->setHeader('Accept', 'application/json');
$this->assertTrue($test->server('GET', array(
'test' => 'server',
'key' => 'CONTENT_TYPE',
)) === 'application/json');
$this->assertTrue($test->server('GET', array(
'test' => 'server',
'key' => 'HTTP_X_REQUESTED_WITH',
)) === 'XMLHttpRequest');
$this->assertTrue($test->server('GET', array(
'test' => 'server',
'key' => 'HTTP_ACCEPT',
)) === 'application/json');
}
}
3 changes: 3 additions & 0 deletions tests/helper.inc.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use Curl\Curl;

class Test {
const TEST_URL = 'http://php-curl-test.anezi.net/tests/server.php';

Expand Down
Loading

0 comments on commit 8e3569d

Please sign in to comment.