Skip to content
Browse files

Test closure and __invoke

  • Loading branch information...
1 parent 3296a22 commit c8e8c701340f523c73ec9338dc6aa4f5848dea98 @sniemela sniemela committed Nov 10, 2009
Showing with 61 additions and 30 deletions.
  1. +23 −29 test/core/RackTest.php
  2. +38 −1 test/core/TestHelper.php
View
52 test/core/RackTest.php
@@ -3,41 +3,15 @@
use core\Rack;
use core\App;
-class MyMiddleware extends App
-{
- public function __invoke($env)
- {
- return array(200, array("Content-Type" => "text/html"), array('Hello world!'));
- }
-}
-
-class MyLowercaseMiddleware extends App
-{
- public function __invoke($env)
- {
- return array(200, array("Content-Type" => "text/html"), array('this is in lowercase'));
- }
-}
-
-class MyUppercaseMiddleware extends App
+class RackTest extends PHPUnit_Framework_TestCase
{
protected $app;
- public function __construct($app)
+ public function setUp()
{
- $this->app = $app;
+ $this->app = new MockApp;
}
- public function __invoke($env)
- {
- list($status, $headers, $response) = $this->call($this->app, $env);
- $uppercase_response = array(strtoupper($response[0]));
- return array($status, $headers, $uppercase_response);
- }
-}
-
-class RackTest extends PHPUnit_Framework_TestCase
-{
public function tearDown()
{
Rack::clearMiddleware();
@@ -47,4 +21,24 @@ public function testMiddlewareReturnsEmptyArray()
{
$this->assertEquals(array(), Rack::middleware());
}
+
+ public function testShouldRunApp()
+ {
+ $env = MockRack::run($this->app);
+ $this->assertEquals(200, $env[0]);
+ $this->assertEquals('text/html', $env[1]['Content-Type']);
+ $this->assertEquals(array('Hello World'), $env[2]);
+ }
+
+ public function testShouldRunLambdaApp()
+ {
+ $app = function ($env) {
+ return array(200, array('Content-Type' => 'text/plain'), array('This is from lambda!'));
+ };
+
+ $env = MockRack::run($app);
+ $this->assertEquals(200, $env[0]);
+ $this->assertEquals('text/plain', $env[1]['Content-Type']);
+ $this->assertEquals(array('This is from lambda!'), $env[2]);
+ }
}
View
39 test/core/TestHelper.php
@@ -1,3 +1,40 @@
<?php
require_once "PHPUnit/Framework.php";
-require_once realpath(dirname(__FILE__).'/../../lib/autoload.php');
+require_once realpath(dirname(__FILE__).'/../../lib/autoload.php');
+use core\Rack;
+use core\App;
+
+class MockApp extends App
+{
+ public function __invoke($env)
+ {
+ return array(200, array('Content-Type' => 'text/html'), array('Hello World'));
+ }
+}
+
+class MockRack extends Rack
+{
+ public static function run($app)
+ {
+ $env =& static::get_env();
+ ob_start();
+ $result = self::runMiddleware($app, $env);
+ $output = ob_get_clean();
+
+ if ($output)
+ {
+ $result[1]["X-Output"] = json_encode($output);
+ }
+ return static::execute($result, $env);
+ }
+
+ protected static function execute($result, $env)
+ {
+ list($status, $headers, $body) = $result;
+ fclose($env['rack.input']);
+ fclose($env['rack.errors']);
+ $headers['X-Powered-By'] = "rack-php ".implode('.',$env['rack.version']);
+ $headers['Status'] = $status;
+ return array($status, $headers, $body);
+ }
+}

0 comments on commit c8e8c70

Please sign in to comment.
Something went wrong with that request. Please try again.