Write test to check if Client send method properly

This commit is contained in:
PJ Dietz 2014-07-25 22:24:55 -04:00
parent 70c2ef1150
commit 5e9980f864
4 changed files with 101 additions and 67 deletions

View File

@ -11,11 +11,18 @@
"email": "pj@pjdietz.com" "email": "pj@pjdietz.com"
} }
], ],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/pjdietz/ShamServer"
}
],
"require": { "require": {
"php": ">=5.3.0" "php": ">=5.3.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "4.1.*" "phpunit/phpunit": "4.1.*",
"pjdietz/shamserver": "dev-master"
}, },
"autoload": { "autoload": {
"psr-0": { "pjdietz\\WellRESTed": "src/" }, "psr-0": { "pjdietz\\WellRESTed": "src/" },

75
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "ff7bc5c6c0cd51153b947901dc3d51af", "hash": "51e65a31711f28ae591ec9f4efb2bfac",
"packages": [], "packages": [],
"packages-dev": [ "packages-dev": [
{ {
@ -257,16 +257,16 @@
}, },
{ {
"name": "phpunit/phpunit", "name": "phpunit/phpunit",
"version": "4.1.3", "version": "4.1.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git", "url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "939cb801b3b2aa253aedd0b279f40bb8f35cec91" "reference": "a71c4842c5fb836d8b200624583b859ec34e8a26"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/939cb801b3b2aa253aedd0b279f40bb8f35cec91", "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a71c4842c5fb836d8b200624583b859ec34e8a26",
"reference": "939cb801b3b2aa253aedd0b279f40bb8f35cec91", "reference": "a71c4842c5fb836d8b200624583b859ec34e8a26",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -327,7 +327,7 @@
"testing", "testing",
"xunit" "xunit"
], ],
"time": "2014-06-11 14:15:47" "time": "2014-07-18 07:15:58"
}, },
{ {
"name": "phpunit/phpunit-mock-objects", "name": "phpunit/phpunit-mock-objects",
@ -386,6 +386,55 @@
], ],
"time": "2014-06-12 07:22:15" "time": "2014-06-12 07:22:15"
}, },
{
"name": "pjdietz/shamserver",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/pjdietz/ShamServer.git",
"reference": "702e5c94bef72d99629cba4f54061ef72e5e638e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pjdietz/ShamServer/zipball/702e5c94bef72d99629cba4f54061ef72e5e638e",
"reference": "702e5c94bef72d99629cba4f54061ef72e5e638e",
"shasum": ""
},
"require": {
"lib-curl": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.1.*"
},
"type": "library",
"autoload": {
"psr-0": {
"pjdietz": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "PJ Dietz",
"email": "pj@pjdietz.com"
}
],
"description": "Build mini web servers in PHP for testing",
"homepage": "https://github.com/pjdietz/ShamServer",
"keywords": [
"curl",
"http",
"webserver"
],
"support": {
"source": "https://github.com/pjdietz/ShamServer/tree/master",
"issues": "https://github.com/pjdietz/ShamServer/issues"
},
"time": "2014-07-26 02:06:19"
},
{ {
"name": "sebastian/comparator", "name": "sebastian/comparator",
"version": "1.0.0", "version": "1.0.0",
@ -656,17 +705,17 @@
}, },
{ {
"name": "symfony/yaml", "name": "symfony/yaml",
"version": "v2.5.1", "version": "v2.5.2",
"target-dir": "Symfony/Component/Yaml", "target-dir": "Symfony/Component/Yaml",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/Yaml.git", "url": "https://github.com/symfony/Yaml.git",
"reference": "1057e87364c0b38b50f5695fc9df9dd189036bec" "reference": "f868ecdbcc0276b6158dfbf08b9e98ce07f014e1"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/Yaml/zipball/1057e87364c0b38b50f5695fc9df9dd189036bec", "url": "https://api.github.com/repos/symfony/Yaml/zipball/f868ecdbcc0276b6158dfbf08b9e98ce07f014e1",
"reference": "1057e87364c0b38b50f5695fc9df9dd189036bec", "reference": "f868ecdbcc0276b6158dfbf08b9e98ce07f014e1",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -701,12 +750,14 @@
], ],
"description": "Symfony Yaml Component", "description": "Symfony Yaml Component",
"homepage": "http://symfony.com", "homepage": "http://symfony.com",
"time": "2014-07-08 12:21:33" "time": "2014-07-09 09:05:48"
} }
], ],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": {
"pjdietz/shamserver": 20
},
"platform": { "platform": {
"php": ">=5.3.0" "php": ">=5.3.0"
}, },

View File

@ -1,82 +1,54 @@
<?php <?php
namespace pjdietz\WellRESTed\Test;
use pjdietz\ShamServer\ShamServer;
use pjdietz\WellRESTed\Client; use pjdietz\WellRESTed\Client;
use pjdietz\WellRESTed\Request;
use pjdietz\WellRESTed\Test;
class ClientTest extends \PHPUnit_Framework_TestCase class ClientTest extends \PHPUnit_Framework_TestCase
{ {
public function testFake()
{
$this->assertTrue(true);
}
/** /**
* @dataProvider curlProvider * @dataProvider httpMethodProvider
*/ */
public function testCurl($method, $uri, $opts, $code) public function testCheckHttpMethod($method)
{ {
$host = "localhost";
$port = 8080;
$script = realpath(__DIR__ . "/sham-routers/method.php");
$server = new ShamServer($host, $port, $script);
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Request')->getMock(); $rqst = $this->getMockBuilder('pjdietz\WellRESTed\Request')->getMock();
$rqst->expects($this->any()) $rqst->expects($this->any())
->method("getUri") ->method("getUri")
->will($this->returnValue($uri)); ->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any()) $rqst->expects($this->any())
->method("getMethod") ->method("getMethod")
->will($this->returnValue($method)); ->will($this->returnValue($method));
$rqst->expects($this->any()) $rqst->expects($this->any())
->method("getPort") ->method("getPort")
->will($this->returnValue(80)); ->will($this->returnValue($port));
$rqst->expects($this->any()) $rqst->expects($this->any())
->method("getHeaders") ->method("getHeaders")
->will($this->returnValue(array( ->will($this->returnValue(array()));
"Cache-control" => "max-age=0"
)));
$client = new Client(array(CURLOPT_HTTPHEADER => array("Cache-control" => "max-age=0")));
$resp = $client->request($rqst, $opts);
$this->assertEquals($code, $resp->getStatusCode());
}
public function curlProvider()
{
return [
["GET", "http://icanhasip.com", [
[CURLOPT_MAXREDIRS => 2]
], 200],
["POST", "http://icanhasip.com", [], 200],
["PUT", "http://icanhasip.com", [], 405],
["DELETE", "http://icanhasip.com", [], 405]
];
}
/**
* @dataProvider curlErrorProvider
* @expectedException \pjdietz\WellRESTed\Exceptions\CurlException
*/
public function testErrorCurl($uri, $opts)
{
$rqst = $this->getMockBuilder('pjdietz\WellRESTed\Request')->getMock();
$rqst->expects($this->any())
->method("getUri")
->will($this->returnValue($uri));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array(
"Cache-control" => "max-age=0"
)));
$rqst = new Request($uri);
$client = new Client(); $client = new Client();
$client->request($rqst, $opts); $resp = $client->request($rqst);
$body = trim($resp->getBody());
$this->assertEquals($method, $body);
$server->stop();
} }
public function curlErrorProvider() public function httpMethodProvider()
{ {
return [ return [
["http://localhost:9991", [ ["GET"],
CURLOPT_FAILONERROR, true, ["POST"],
CURLOPT_TIMEOUT_MS, 10 ["PUT"],
]], ["DELETE"],
["PATCH"],
["OPTIONS"]
]; ];
} }
} }

View File

@ -0,0 +1,4 @@
<?php
print $_SERVER["REQUEST_METHOD"];
exit;