示例#1
0
 public function testDispatchWithMiddlware()
 {
     $this->expectOutputString('First! Second! Hello josh');
     Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/hello/josh', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim'));
     $env = Slim_Environment::getInstance();
     $req = new Slim_Http_Request($env);
     $res = new Slim_Http_Response();
     $router = new Slim_Router($req, $res);
     $route = new Slim_Route('/hello/:name', function ($name) {
         echo "Hello {$name}";
     });
     $route->setMiddleware(function () {
         echo "First! ";
     });
     $route->setMiddleware(function () {
         echo "Second! ";
     });
     $route->setRouter($router);
     $route->matches($req->getResourceUri());
     //<-- Extracts params from resource URI
     $route->dispatch();
 }
示例#2
0
 /**
  * Test route sets and gets middleware
  *
  * Pre-conditions:
  * Route instantiated
  *
  * Post-conditions:
  * Case A: Middleware set as callable, not array
  * Case B: Middleware set after other middleware already set
  * Case C: Middleware set as array of callables
  * Case D: Middleware set as a callable array
  * Case E: Middleware is invalid; throws InvalidArgumentException
  */
 public function testRouteMiddleware()
 {
     $callable1 = function () {
     };
     $callable2 = function () {
     };
     //Case A
     $r1 = new Slim_Route('/foo', function () {
     });
     $r1->setMiddleware($callable1);
     $mw = $r1->getMiddleware();
     $this->assertInternalType('array', $mw);
     $this->assertEquals(1, count($mw));
     //Case B
     $r1->setMiddleware($callable2);
     $mw = $r1->getMiddleware();
     $this->assertEquals(2, count($mw));
     //Case C
     $r2 = new Slim_Route('/foo', function () {
     });
     $r2->setMiddleware(array($callable1, $callable2));
     $mw = $r2->getMiddleware();
     $this->assertInternalType('array', $mw);
     $this->assertEquals(2, count($mw));
     //Case D
     $r3 = new Slim_Route('/foo', function () {
     });
     $r3->setMiddleware(array($this, 'callableTestFunction'));
     $mw = $r3->getMiddleware();
     $this->assertInternalType('array', $mw);
     $this->assertEquals(1, count($mw));
     //Case E
     try {
         $r3->setMiddleware('sdjfsoi788');
         $this->fail('Did not catch InvalidArgumentException when setting invalid route middleware');
     } catch (InvalidArgumentException $e) {
     }
 }
示例#3
0
 public function testRouteMiddlwareArguments()
 {
     $this->expectOutputString('foobar');
     Slim_Environment::mock(array('SCRIPT_NAME' => '', 'PATH_INFO' => '/foo'));
     $env = Slim_Environment::getInstance();
     $req = new Slim_Http_Request($env);
     $router = new Slim_Router();
     $router->setResourceUri($req->getResourceUri());
     $route = new Slim_Route('/foo', function () {
         echo "bar";
     });
     $route->setName('foo');
     $route->setMiddleware(function ($route) {
         echo $route->getName();
     });
     $route->matches($req->getResourceUri());
     //<-- Extracts params from resource URI
     $router->dispatch($route);
 }