From 3d44d1a3f5d48ab35d7d82abef019938fa4d2812 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Fri, 25 Jul 2014 23:08:27 -0400 Subject: [PATCH] Test sending body with Client --- composer.json | 1 + composer.lock | 53 +++++++++++++++++++++++++++++- src/pjdietz/WellRESTed/Request.php | 2 +- test/ClientTest.php | 52 ++++++++++++++++++++++++++--- test/sham-routers/body.php | 4 +++ 5 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 test/sham-routers/body.php diff --git a/composer.json b/composer.json index 01d1fd7..8f0ba21 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "php": ">=5.3.0" }, "require-dev": { + "fzaninotto/faker": "1.5.*@dev", "phpunit/phpunit": "4.1.*", "pjdietz/shamserver": "dev-master" }, diff --git a/composer.lock b/composer.lock index 8f7671e..d406abc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,9 +4,59 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "51e65a31711f28ae591ec9f4efb2bfac", + "hash": "5f2174a14a32c9abc7bca48bc8557cb1", "packages": [], "packages-dev": [ + { + "name": "fzaninotto/faker", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/fzaninotto/Faker.git", + "reference": "c48f30549b1ec508a74141a833757ca1e0d9ca53" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/c48f30549b1ec508a74141a833757ca1e0d9ca53", + "reference": "c48f30549b1ec508a74141a833757ca1e0d9ca53", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~1.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5.x-dev" + } + }, + "autoload": { + "psr-0": { + "Faker": "src/", + "Faker\\PHPUnit": "test/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "François Zaninotto" + } + ], + "description": "Faker is a PHP library that generates fake data for you.", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "time": "2014-07-17 07:25:43" + }, { "name": "phpunit/php-code-coverage", "version": "2.0.9", @@ -756,6 +806,7 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { + "fzaninotto/faker": 20, "pjdietz/shamserver": 20 }, "platform": { diff --git a/src/pjdietz/WellRESTed/Request.php b/src/pjdietz/WellRESTed/Request.php index 6ef078e..293e118 100644 --- a/src/pjdietz/WellRESTed/Request.php +++ b/src/pjdietz/WellRESTed/Request.php @@ -97,7 +97,7 @@ class Request extends Message implements RequestInterface } // http://www.php.net/manual/en/function.getallheaders.php#84262 - $headers = ''; + $headers = []; foreach ($_SERVER as $name => $value) { if (substr($name, 0, 5) === 'HTTP_') { $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; diff --git a/test/ClientTest.php b/test/ClientTest.php index 2512f8b..19d9a57 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -2,6 +2,7 @@ namespace pjdietz\WellRESTed\Test; +use Faker\Factory; use pjdietz\ShamServer\ShamServer; use pjdietz\WellRESTed\Client; @@ -10,7 +11,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase /** * @dataProvider httpMethodProvider */ - public function testCheckHttpMethod($method) + public function testSendHttpMethod($method) { $host = "localhost"; $port = 8080; @@ -18,7 +19,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $server = new ShamServer($host, $port, $script); - $rqst = $this->getMockBuilder('pjdietz\WellRESTed\Request')->getMock(); + $rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock(); $rqst->expects($this->any()) ->method("getUri") ->will($this->returnValue("http://$host:$port")); @@ -55,7 +56,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase /** * @dataProvider httpHeaderProvider */ - public function testCheckHttpHeaders($headerKey, $headerValue) + public function testSendHttpHeaders($headerKey, $headerValue) { $host = "localhost"; $port = 8080; @@ -63,7 +64,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $server = new ShamServer($host, $port, $script); - $rqst = $this->getMockBuilder('pjdietz\WellRESTed\Request')->getMock(); + $rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock(); $rqst->expects($this->any()) ->method("getUri") ->will($this->returnValue("http://$host:$port")); @@ -93,4 +94,47 @@ class ClientTest extends \PHPUnit_Framework_TestCase ["Accept-Charset", "utf-8"] ]; } + + /** + * @dataProvider bodyProvider + */ + public function testSendBody($body) + { + $host = "localhost"; + $port = 8080; + $script = realpath(__DIR__ . "/sham-routers/body.php"); + $server = new ShamServer($host, $port, $script); + + $rqst = $this->getMockBuilder('pjdietz\WellRESTed\Interfaces\RequestInterface')->getMock(); + $rqst->expects($this->any()) + ->method("getUri") + ->will($this->returnValue("http://$host:$port")); + $rqst->expects($this->any()) + ->method("getMethod") + ->will($this->returnValue("POST")); + $rqst->expects($this->any()) + ->method("getPort") + ->will($this->returnValue($port)); + $rqst->expects($this->any()) + ->method("getHeaders") + ->will($this->returnValue(array())); + $rqst->expects($this->any()) + ->method("getBody") + ->will($this->returnValue($body)); + + $client = new Client(); + $resp = $client->request($rqst); + $this->assertEquals($body, $resp->getBody()); + $server->stop(); + } + + public function bodyProvider() + { + $faker = Factory::create(); + return [ + [$faker->text()], + [$faker->text()], + [$faker->text()] + ]; + } } diff --git a/test/sham-routers/body.php b/test/sham-routers/body.php new file mode 100644 index 0000000..5e5992e --- /dev/null +++ b/test/sham-routers/body.php @@ -0,0 +1,4 @@ +