Permalink
Browse files

added functional tests

  • Loading branch information...
1 parent 5a8971b commit 0d20aa29ae57283613618eb1ef113c070216b905 @kriswallsmith committed Nov 15, 2011
Showing with 189 additions and 10 deletions.
  1. +1 −0 .gitignore
  2. +14 −10 phpunit.xml.dist
  3. +165 −0 test/Buzz/Test/Client/FunctionalTest.php
  4. +9 −0 test/server.php
View
1 .gitignore
@@ -0,0 +1 @@
+phpunit.xml
View
24 phpunit.xml.dist
@@ -1,15 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./test/bootstrap.php" colors="true">
- <testsuites>
- <testsuite name="Buzz Test Suite">
- <directory suffix="Test.php">./test/Buzz/</directory>
- </testsuite>
- </testsuites>
+ <testsuites>
+ <testsuite name="Buzz Test Suite">
+ <directory suffix="Test.php">./test/Buzz/</directory>
+ </testsuite>
+ </testsuites>
- <filter>
- <whitelist>
- <directory suffix=".php">./lib/Buzz/</directory>
- </whitelist>
- </filter>
+ <php>
+ <!-- <server name="TEST_SERVER" value="http://localhost/buzz/test/server.php" /> -->
+ </php>
+
+ <filter>
+ <whitelist>
+ <directory suffix=".php">./lib/Buzz/</directory>
+ </whitelist>
+ </filter>
</phpunit>
View
165 test/Buzz/Test/Client/FunctionalTest.php
@@ -0,0 +1,165 @@
+<?php
+
+namespace Buzz\Test\Client;
+
+use Buzz\Client\Curl;
+use Buzz\Client\FileGetContents;
+use Buzz\Message\FormRequest;
+use Buzz\Message\FormUpload;
+use Buzz\Message\Request;
+use Buzz\Message\Response;
+
+class FunctionalTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!isset($_SERVER['TEST_SERVER'])) {
+ $this->markTestSkipped('The test server is not configured.');
+ }
+ }
+
+ /**
+ * @dataProvider provideClientAndMethod
+ */
+ public function testRequestMethods($client, $method)
+ {
+ $request = new Request($method);
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertEquals($method, $data['SERVER']['REQUEST_METHOD']);
+ }
+
+ /**
+ * @dataProvider provideClient
+ */
+ public function testFormPost($client)
+ {
+ $request = new FormRequest();
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $request->setField('company[name]', 'Google');
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertStringStartsWith('application/x-www-form-urlencoded', $data['SERVER']['CONTENT_TYPE']);
+ $this->assertEquals('Google', $data['POST']['company']['name']);
+ }
+
+ /**
+ * @dataProvider provideClientAndUpload
+ */
+ public function testFileUpload($client, $upload)
+ {
+ $request = new FormRequest();
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $request->setField('company[name]', 'Google');
+ $request->setField('company[logo]', $upload);
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertStringStartsWith('multipart/form-data', $data['SERVER']['CONTENT_TYPE']);
+ $this->assertEquals('Google', $data['POST']['company']['name']);
+ $this->assertEquals('google.png', $data['FILES']['company']['name']['logo']);
+ }
+
+ /**
+ * @dataProvider provideClient
+ */
+ public function testJsonPayload($client)
+ {
+ $request = new Request(Request::METHOD_POST);
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $request->addHeader('Content-Type: application/json');
+ $request->setContent(json_encode(array('foo' => 'bar')));
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertEquals('application/json', $data['SERVER']['CONTENT_TYPE']);
+ $this->assertEquals('{"foo":"bar"}', $data['INPUT']);
+ }
+
+ /**
+ * @dataProvider provideClient
+ */
+ public function testConsecutiveRequests($client)
+ {
+ // request 1
+ $request = new Request(Request::METHOD_PUT);
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $request->addHeader('Content-Type: application/json');
+ $request->setContent(json_encode(array('foo' => 'bar')));
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertEquals('PUT', $data['SERVER']['REQUEST_METHOD']);
+ $this->assertEquals('application/json', $data['SERVER']['CONTENT_TYPE']);
+ $this->assertEquals('{"foo":"bar"}', $data['INPUT']);
+
+ // request 2
+ $request = new Request(Request::METHOD_GET);
+ $request->fromUrl($_SERVER['TEST_SERVER']);
+ $response = new Response();
+ $client->send($request, $response);
+
+ $data = json_decode($response->getContent(), true);
+
+ $this->assertEquals('GET', $data['SERVER']['REQUEST_METHOD']);
+ $this->assertEmpty($data['INPUT']);
+ }
+
+ public function provideClient()
+ {
+ return array(
+ array(new Curl()),
+ array(new FileGetContents()),
+ );
+ }
+
+ public function provideClientAndMethod()
+ {
+ // HEAD is intentionally omitted
+ // http://stackoverflow.com/questions/2603104/does-mod-php-honor-head-requests-properly
+
+ $methods = array('GET', 'POST', 'PUT', 'DELETE');
+ $clients = $this->provideClient();
+
+ $data = array();
+ foreach ($clients as $client) {
+ foreach ($methods as $method) {
+ $data[] = array($client[0], $method);
+ }
+ }
+
+ return $data;
+ }
+
+ public function provideClientAndUpload()
+ {
+ $stringUpload = new FormUpload();
+ $stringUpload->setFilename('google.png');
+ $stringUpload->setContent(file_get_contents(__DIR__.'/../Message/Fixtures/google.png'));
+
+ $uploads = array($stringUpload, new FormUpload(__DIR__.'/../Message/Fixtures/google.png'));
+ $clients = $this->provideClient();
+
+ $data = array();
+ foreach ($clients as $client) {
+ foreach ($uploads as $upload) {
+ $data[] = array($client[0], $upload);
+ }
+ }
+
+ return $data;
+ }
+}
View
9 test/server.php
@@ -0,0 +1,9 @@
+<?php
+
+echo json_encode(array(
+ 'SERVER' => $_SERVER,
+ 'GET' => $_GET,
+ 'POST' => $_POST,
+ 'FILES' => $_FILES,
+ 'INPUT' => file_get_contents('php://input'),
+));

0 comments on commit 0d20aa2

Please sign in to comment.