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"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/pjdietz/ShamServer"
}
],
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "4.1.*"
"phpunit/phpunit": "4.1.*",
"pjdietz/shamserver": "dev-master"
},
"autoload": {
"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",
"This file is @generated automatically"
],
"hash": "ff7bc5c6c0cd51153b947901dc3d51af",
"hash": "51e65a31711f28ae591ec9f4efb2bfac",
"packages": [],
"packages-dev": [
{
@ -257,16 +257,16 @@
},
{
"name": "phpunit/phpunit",
"version": "4.1.3",
"version": "4.1.4",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "939cb801b3b2aa253aedd0b279f40bb8f35cec91"
"reference": "a71c4842c5fb836d8b200624583b859ec34e8a26"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/939cb801b3b2aa253aedd0b279f40bb8f35cec91",
"reference": "939cb801b3b2aa253aedd0b279f40bb8f35cec91",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a71c4842c5fb836d8b200624583b859ec34e8a26",
"reference": "a71c4842c5fb836d8b200624583b859ec34e8a26",
"shasum": ""
},
"require": {
@ -327,7 +327,7 @@
"testing",
"xunit"
],
"time": "2014-06-11 14:15:47"
"time": "2014-07-18 07:15:58"
},
{
"name": "phpunit/phpunit-mock-objects",
@ -386,6 +386,55 @@
],
"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",
"version": "1.0.0",
@ -656,17 +705,17 @@
},
{
"name": "symfony/yaml",
"version": "v2.5.1",
"version": "v2.5.2",
"target-dir": "Symfony/Component/Yaml",
"source": {
"type": "git",
"url": "https://github.com/symfony/Yaml.git",
"reference": "1057e87364c0b38b50f5695fc9df9dd189036bec"
"reference": "f868ecdbcc0276b6158dfbf08b9e98ce07f014e1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Yaml/zipball/1057e87364c0b38b50f5695fc9df9dd189036bec",
"reference": "1057e87364c0b38b50f5695fc9df9dd189036bec",
"url": "https://api.github.com/repos/symfony/Yaml/zipball/f868ecdbcc0276b6158dfbf08b9e98ce07f014e1",
"reference": "f868ecdbcc0276b6158dfbf08b9e98ce07f014e1",
"shasum": ""
},
"require": {
@ -701,12 +750,14 @@
],
"description": "Symfony Yaml Component",
"homepage": "http://symfony.com",
"time": "2014-07-08 12:21:33"
"time": "2014-07-09 09:05:48"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"pjdietz/shamserver": 20
},
"platform": {
"php": ">=5.3.0"
},

View File

@ -1,82 +1,54 @@
<?php
namespace pjdietz\WellRESTed\Test;
use pjdietz\ShamServer\ShamServer;
use pjdietz\WellRESTed\Client;
use pjdietz\WellRESTed\Request;
use pjdietz\WellRESTed\Test;
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->expects($this->any())
->method("getUri")
->will($this->returnValue($uri));
->will($this->returnValue("http://$host:$port"));
$rqst->expects($this->any())
->method("getMethod")
->will($this->returnValue($method));
$rqst->expects($this->any())
->method("getPort")
->will($this->returnValue(80));
->will($this->returnValue($port));
$rqst->expects($this->any())
->method("getHeaders")
->will($this->returnValue(array(
"Cache-control" => "max-age=0"
)));
->will($this->returnValue(array()));
$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->request($rqst, $opts);
$resp = $client->request($rqst);
$body = trim($resp->getBody());
$this->assertEquals($method, $body);
$server->stop();
}
public function curlErrorProvider()
public function httpMethodProvider()
{
return [
["http://localhost:9991", [
CURLOPT_FAILONERROR, true,
CURLOPT_TIMEOUT_MS, 10
]],
["GET"],
["POST"],
["PUT"],
["DELETE"],
["PATCH"],
["OPTIONS"]
];
}
}

View File

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