80%+ test coverage
authorMichael Wallner <mike@php.net>
Sun, 28 Oct 2012 20:21:24 +0000 (20:21 +0000)
committerMichael Wallner <mike@php.net>
Sun, 28 Oct 2012 20:21:24 +0000 (20:21 +0000)
phpunit/ClientRequestTest.php [new file with mode: 0644]
phpunit/DataShareTest.php [new file with mode: 0644]
phpunit/PoolTest.php [new file with mode: 0644]
phpunit/RequestTest.php
tests/envrequestfiles001.phpt [new file with mode: 0644]
tests/envrequestfiles002.phpt [new file with mode: 0644]
tests/envrequestform.phpt [new file with mode: 0644]
tests/envrequestquery.phpt [new file with mode: 0644]
tests/params002.phpt [new file with mode: 0644]
tests/serialize001.phpt [new file with mode: 0644]

diff --git a/phpunit/ClientRequestTest.php b/phpunit/ClientRequestTest.php
new file mode 100644 (file)
index 0000000..f14f49e
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+use http\Client\Request;
+use http\Message\Body;
+
+class ClientRequestTest extends PHPUnit_Framework_TestCase
+{
+    function testStandard() {
+        $r = new Request($m = "POST", $u = "http://localhost/foo", $h = array("header", "value"),
+            $b = new Body(fopen(__FILE__, "r+b"))
+        );
+
+        $this->assertEquals($b, $r->getBody());
+        $this->assertEquals($h, $r->getHeaders());
+        $this->assertEquals($u, $r->getRequestUrl());
+        $this->assertEquals($m, $r->getRequestMethod());
+    }
+
+    function testContentType() {
+        $r = new Request("POST", "http://localhost/");
+        $this->assertEquals($r, $r->setContentType($ct = "text/plain; charset=utf-8"));
+        $this->assertEquals($ct, $r->getContentType());
+    }
+
+    function testQuery() {
+        $r = new Request("GET", "http://localhost/");
+        $this->assertEquals(null, $r->getQuery());
+        $this->assertEquals($r, $r->setQuery($q = "foo=bar"));
+        $this->assertEquals($q, $r->getQuery());
+        $this->assertEquals($r, $r->addQuery("a[]=1&a[]=2"));
+        $this->assertEquals("foo=bar&a%5B0%5D=1&a%5B1%5D=2", $r->getQuery());
+        $this->assertEquals(null, $r->setQuery(null)->getQuery());
+    }
+
+    function testOptions() {
+        $r = new Request("GET", "http://localhost");
+        $this->assertEquals($r, $r->setOptions($o = array("redirect"=>5, "timeout"=>5)));
+        $this->assertEquals($o, $r->getOptions());
+        $this->assertEquals($r, $r->setSslOptions($o = array("verify_peer"=>false)));
+        $this->assertEquals($o, $r->getSslOptions());
+    }
+}
+
diff --git a/phpunit/DataShareTest.php b/phpunit/DataShareTest.php
new file mode 100644 (file)
index 0000000..0a50727
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+class DataShareTest extends PHPUnit_Framework_TestCase
+{
+    function testStandard() {
+        foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+            $f = new http\Client\Factory(compact("driver"));
+            $s = $f->createDataShare();
+
+            $this->assertFalse($s->dns, "dns");
+            $this->assertFalse($s->cookie, "cookie");
+            $this->assertFalse($s->ssl, "ssl");
+
+            $s->dns = true;
+            $s->cookie = true;
+            $s->ssl = true;
+
+            $this->assertTrue($s->dns, "dns");
+            $this->assertTrue($s->cookie, "cookie");
+            $this->assertTrue($s->ssl, "ssl");
+        }
+    }
+
+    function testAttach() {
+        foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+            $f = new http\Client\Factory(compact("driver"));
+            $s = $f->createDataShare();
+            $s->dns = $s->ssl = $s->cookie = true;
+            $c = $f->createClient();
+            $s->attach($c);
+            $c->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+            $s->attach($c);
+            $cc = clone $c;
+            $s->attach($cc);
+
+            $this->assertEquals(3, count($s));
+        }
+    }
+    
+    function testCurl() {
+       $client = new http\Curl\Client;
+       $client->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+       $share = new http\Curl\Client\DataShare;
+       $share->ssl = $share->dns = $share->cookie = true;
+       $share->attach($client);
+       $share->attach($client2 = clone $client);
+       $share->attach($client3 = clone $client);
+       
+       $this->assertEquals(3, count($share));
+       $client->send();
+       $client2->send();
+       $client3->send();
+       
+       $share->detach($client);
+       $share->reset();
+    }
+}
+
diff --git a/phpunit/PoolTest.php b/phpunit/PoolTest.php
new file mode 100644 (file)
index 0000000..33dbf9b
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+class PoolTest extends PHPUnit_Framework_TestCase
+{
+    /*function testStandard() {
+        foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+            $f = new http\Client\Factory(compact("driver"));
+            $p = $f->createPool();
+
+            $this->assertFalse($p->dns, "dns");
+            $this->assertFalse($p->cookie, "cookie");
+            $this->assertFalse($p->ssl, "ssl");
+
+            $p->dns = true;
+            $p->cookie = true;
+            $p->ssl = true;
+
+            $this->assertTrue($p->dns, "dns");
+            $this->assertTrue($p->cookie, "cookie");
+            $this->assertTrue($p->ssl, "ssl");
+        }
+    }*/
+
+    function testAttach() {
+        foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+            $f = new http\Client\Factory(compact("driver"));
+            
+            $p = $f->createPool();
+            $c = $f->createClient();
+            
+            $c->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+            $p->attach($c);
+            
+            try {
+               $p->attach($c);
+            } catch (http\Exception $e) {
+               $this->assertEquals("Could not attach request to pool: Invalid easy handle", $e->getMessage());
+            }
+            
+            $cc = clone $c;
+            $p->attach($cc);
+
+            $this->assertEquals(2, count($p));
+        }
+    }
+    
+    function testCurl() {
+       $client = new http\Curl\Client;
+       $client->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+       $pool = new http\Curl\Client\Pool;
+       $pool->attach($client);
+       $pool->attach($client2 = clone $client);
+       $pool->attach($client3 = clone $client);
+       
+       $this->assertEquals(3, count($pool));
+       $pool->send();
+       
+       $pool->detach($client);
+       $this->assertEquals(2, count($pool));
+       
+       $pool->reset();
+       $this->assertEquals(0, count($pool));
+    }
+    
+    function testCurlEvents() {
+       $client = new http\Curl\Client;
+       $pool = new http\Curl\Client\Pool;
+       
+       $client->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+       
+       $pool->attach($client);
+       $pool->attach(clone $client);
+       $pool->attach(clone $client);
+       
+       $pool->enableEvents();
+       $pool->send();
+    }
+}
+
index 56053ee148d5a487db4ee7d091a5a1f20874ac88..a327f6dff2440eb49a26a118dadeb6a9ae649e5d 100644 (file)
@@ -35,6 +35,18 @@ class RequestTest extends PHPUnit_Framework_TestCase
                 "timeout"           => 300,
             )
         );
+        $this->r->setOptions(
+               array(
+                       "timeout" => 600
+               )
+        );
+        $this->assertEquals(
+               array(
+                       "connecttimeout" => 30,
+                       "timeout" => 600,
+               ),
+               $this->r->getOptions()
+        );
     }
 
     function testClone() {
@@ -43,10 +55,10 @@ class RequestTest extends PHPUnit_Framework_TestCase
     }
 
     function testObserver() {
-        $this->r->attach(new ProgressObserver1);
-        $this->r->attach(new ProgressObserver2);
+        $this->r->attach($o1 = new ProgressObserver1);
+        $this->r->attach($o2 = new ProgressObserver2);
         $this->r->attach(
-            new CallbackObserver(
+            $o3 = new CallbackObserver(
                 function ($r) {
                     $p = (array) $r->getProgress();
                     $this->assertArrayHasKey("started", $p);
@@ -62,6 +74,12 @@ class RequestTest extends PHPUnit_Framework_TestCase
         $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/"))->send(null);
         $this->assertRegexp("/(\.-)+/", $this->r->pi);
         $this->assertCount(3, $this->r->getObservers());
+        $this->r->detach($o1);
+        $this->assertCount(2, $this->r->getObservers());
+        $this->r->detach($o2);
+        $this->assertCount(1, $this->r->getObservers());
+        $this->r->detach($o3);
+        $this->assertCount(0, $this->r->getObservers());
     }
 
     function testCookies() {
@@ -92,6 +110,23 @@ class RequestTest extends PHPUnit_Framework_TestCase
         $this->r->send(null);
         $this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
     }
+    
+    function testSsl() {
+       $this->r->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+       $this->r->setSslOptions(array("verify_peer" => true));
+       $this->r->addSslOptions(array("verify_host" => 2));
+       $this->assertEquals(
+               array(
+                       "verify_peer" => true,
+                       "verify_host" => 2,
+               ),
+               $this->r->getSslOptions()
+       );
+       $this->r->send();
+       $ti = $this->r->getTransferInfo();
+       $this->assertArrayHasKey("ssl_engines", $ti);
+       $this->assertGreaterThan(0, count($ti["ssl_engines"])); 
+    }
 
     function testHistory() {
         $body = new http\Message\Body;
diff --git a/tests/envrequestfiles001.phpt b/tests/envrequestfiles001.phpt
new file mode 100644 (file)
index 0000000..5b1cd2e
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+http\Env\Request grabbing $_FILES
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST_RAW--
+Content-Type: multipart/form-data;boundary=--123
+
+----123
+Content-Disposition: form-data;filename="foo.bar"
+
+foo
+bar
+
+----123
+Content-Disposition: form-data;filename="bar.foo"
+
+bar
+foo
+
+----123--
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+$f = $r->getFiles();
+
+foreach ($_FILES as $i => $file) {
+    foreach (array("name","type","size","error","file") as $key) {
+        if ($file[$key == "file" ? "tmp_name" : $key] != $f[$i][$key]) {
+            printf("%d.%s differs: '%s' != '%s'\n", $i, $key, $f[$i][$key], $file[$key]);
+        }
+    }
+}
+
+?>
+DONE
+--EXPECT--
+TEST
+DONE
diff --git a/tests/envrequestfiles002.phpt b/tests/envrequestfiles002.phpt
new file mode 100644 (file)
index 0000000..85b44db
--- /dev/null
@@ -0,0 +1,47 @@
+--TEST--
+http\Env\Request grabbing $_FILES from array
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST_RAW--
+Content-Type: multipart/form-data;boundary=--123
+
+----123
+Content-Disposition: form-data;filename=file1;name=file[]
+
+first
+----123
+Content-Disposition: form-data;filename=file2;name=file[]
+
+second
+----123
+Content-Disposition: form-data;filename=file3;name=file[]
+
+third
+----123--
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+$f = array();
+
+foreach ($_FILES as $name => $data) {
+    foreach ($data["tmp_name"] as $i => $file) {
+        $f[$name][$i] = array(
+            "file" => $file,
+            "name" => $data["name"][$i],
+            "size" => $data["size"][$i],
+            "type" => $data["type"][$i],
+            "error"=> $data["error"][$i]
+        );
+    }
+}
+
+var_dump($f == $r->getFiles());
+
+?>
+DONE
+--EXPECT--
+TEST
+bool(true)
+DONE
diff --git a/tests/envrequestform.phpt b/tests/envrequestform.phpt
new file mode 100644 (file)
index 0000000..c2086bb
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+http\Env\Request getForm
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST--
+a=b&b=c&r[]=1&r[]=2
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+printf("%s\n", $r->getForm());
+printf("%s\n", $r->getForm("b", "s", null, true));
+printf("%s\n", $r->getForm("x", "s", "nooo"));
+printf("%s\n", $r->getForm());
+?>
+DONE
+--EXPECT--
+TEST
+a=b&b=c&r%5B0%5D=1&r%5B1%5D=2
+c
+nooo
+a=b&r%5B0%5D=1&r%5B1%5D=2
+DONE
diff --git a/tests/envrequestquery.phpt b/tests/envrequestquery.phpt
new file mode 100644 (file)
index 0000000..87f168d
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+http\Env\Request getQuery
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--GET--
+a=b&b=c&r[]=1&r[]=2
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+printf("%s\n", $r->getQuery());
+printf("%s\n", $r->getQuery("b", "s", null, true));
+printf("%s\n", $r->getQuery("x", "s", "nooo"));
+printf("%s\n", $r->getQuery());
+?>
+DONE
+--EXPECT--
+TEST
+a=b&b=c&r%5B0%5D=1&r%5B1%5D=2
+c
+nooo
+a=b&r%5B0%5D=1&r%5B1%5D=2
+DONE
diff --git a/tests/params002.phpt b/tests/params002.phpt
new file mode 100644 (file)
index 0000000..66e514c
--- /dev/null
@@ -0,0 +1,60 @@
+--TEST--
+query parser
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+$p = new http\Params("foo=bar&arr[]=1&arr[]=2", array("&",";"), "", "=", http\Params::PARSE_QUERY);
+
+var_dump($p); 
+
+echo $p, "\n";
+?>
+DONE
+--EXPECTF--
+object(http\Params)#%d (6) {
+  ["errorHandling":protected]=>
+  NULL
+  ["params"]=>
+  array(2) {
+    ["foo"]=>
+    array(2) {
+      ["value"]=>
+      string(3) "bar"
+      ["arguments"]=>
+      array(0) {
+      }
+    }
+    ["arr"]=>
+    array(2) {
+      ["value"]=>
+      array(2) {
+        [0]=>
+        string(1) "1"
+        [1]=>
+        string(1) "2"
+      }
+      ["arguments"]=>
+      array(0) {
+      }
+    }
+  }
+  ["param_sep"]=>
+  array(2) {
+    [0]=>
+    string(1) "&"
+    [1]=>
+    string(1) ";"
+  }
+  ["arg_sep"]=>
+  string(0) ""
+  ["val_sep"]=>
+  string(1) "="
+  ["flags"]=>
+  int(12)
+}
+foo=bar&arr%5B0%5D=1&arr%5B1%5D=2
+DONE
+
diff --git a/tests/serialize001.phpt b/tests/serialize001.phpt
new file mode 100644 (file)
index 0000000..2e06afe
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+serialization
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+
+class Client extends \http\Client\AbstractClient
+{
+       function __construct() {
+               parent::__construct();
+               $this->request = new \http\Client\Request("GET", "http://localhost");
+       }
+       function send($request) {
+       }
+}
+
+$ext = new ReflectionExtension("http");
+foreach ($ext->getClasses() as $class) {
+       if ($class->isInstantiable()) {
+               #printf("%s\n", $class->getName());
+               $instance = $class->newInstance();
+               $serialized = serialize($instance);
+               $unserialized = unserialize($serialized);
+               
+               foreach (["toString", "toArray"] as $m) {
+                       if ($class->hasMethod($m)) {
+                               #printf("%s#%s\n", $class->getName(), $m);
+                               $unserialized->$m();
+                       }
+               }
+               if ($class->hasMethod("attach") && !$class->implementsInterface("\\SplSubject")) {
+                       #printf("%s#%s\n", $class->getName(), "attach");
+                       $unserialized->attach((new http\Curl\Client)->setRequest(new http\Client\Request("GET", "http://localhost")));
+               }
+       }
+}
+?>
+DONE
+--EXPECTF--
+DONE