expect(ob_get_clean())->to_be(''); }); it("should not flush the response if \$options->flush is false", function () { pipes\get('/foo', function () { return 'bar'; }); ob_start(); pipes\run(array('flush' => false)); expect(ob_get_clean())->to_be(''); ob_start(); pipes\response()->flush(); expect(ob_get_clean())->to_be('bar'); }); it("should use the 'path' sub-pattern, if matched", function () { $_SERVER['REQUEST_URI'] = '/toe/foo'; $request = pipes\request(new pipes\Request()); pipes\get('/toe/(?<path>\\w+)', array('paths' => array(__DIR__ . '/mock/path1', __DIR__ . '/mock/path2'))); ob_start(); pipes\run(); expect(ob_get_clean())->to_be('foo1foo2'); }); }); describe("php", function () { it("should return true if the file was included", function () { ob_start(); $filename = __DIR__ . '/mock/path1/foo.php'; expect(pipes\php($filename))->to_be_true(); expect(ob_get_clean())->to_be('foo1'); }); it("should return false if the file was not included", function () { ob_start();
<?php describe("request()", function () { it("should return the current pipes\\Request instance", function () { $request = pipes\request(); expect($request)->to_be_a('pipes\\Request'); expect(pipes\request())->to_be($request); }); it("should allow you to override the current instance", function () { $oldRequest = pipes\request(); $newRequest = new pipes\Request(); expect(pipes\request($newRequest))->to_be($newRequest); expect(pipes\request())->to_be($newRequest); expect(pipes\request($oldRequest))->to_be($oldRequest); expect(pipes\request())->to_be($oldRequest); }); }); describe("Request", function () { before_each(function ($context) { $oldServer = $_SERVER; $oldRequest = $_REQUEST; $_SERVER['REQUEST_URI'] = '/foo/bar/baz.biff'; $_SERVER['REQUEST_METHOD'] = 'PUT'; $_REQUEST['a'] = 1; $_REQUEST['b'] = 2; $request = new pipes\Request(); return array_merge($context, compact('oldServer', 'oldRequest', 'request')); }); after_each(function ($context) { $_SERVER = $context['oldServer']; $_REQUEST = $context['oldRequest'];
}); it("should fail if neither callback or paths are set", function () { $route = new pipes\Route('/', function () { }); unset($route->options->callback); expect(function () use($route) { $route->run('/', array()); })->to_throw('Exception', 'paths or callback required for route'); }); it("should move all named matches into \$request->params", function () { $route = new pipes\Route('/', function () { return 'xyzzy'; }); $matches = array(0 => 'x', 'a' => 'x', 1 => 'y', 'b' => 'y'); expect($route->run('/foo', $matches))->to_equal('xyzzy'); expect(pipes\request()->params->toArray())->to_be(array('format' => 'html', 'captures' => array(0 => 'y'), 'a' => 'x', 'b' => 'y')); }); it("should catch a HaltException", function () { $route = new pipes\Route('/', function () { pipes\halt(); }); expect(function () use($route) { $route->run('/', array()); })->not_to_throw('Exception'); }); }); describe("matches()", function () { it("should automatically compile the pattern if needed", function () { $route = new pipes\Route('/foo/:bar', function () { }); expect($route->pattern)->to_be_null();