/** * Test that routes Match */ public function testRouteMatches() { // create a request $request = Request::create('/blog/42/testing-routes'); // Create a route $r = new Route('blogtest', '/blog/{page}/::{section}', array('controller' => 'example:DemoController:hello', 'method' => 'GET'), array('page' => 'int', 'section' => 'slug'), array('section' => 'fish')); // Check various getters $this->assertEquals($r->getName(), 'blogtest'); $this->assertEquals($r->getActionMethod(), 'helloAction'); $this->assertEquals($r->getControllerClass(), 'example\\controllers\\DemoController'); $this->assertEquals($r->getMethod(), 'GET'); $this->assertEquals($r->getOption('method'), 'GET'); $this->assertEquals($r->getOption('madeupcrap'), null); $this->assertEquals($r->generate(array('page' => 42, 'section' => 'cars')), '/blog/42/cars'); $this->assertEquals($r->generate(array('page' => 102030)), '/blog/102030/fish'); // Check that the regex for the URL matches what we think it should $this->assertEquals($r->getRegex(), '%^/blog/([0-9]+)/(?:([a-zA-Z0-9-]+))?$%u'); // Test that the route matches the request $this->assertTrue($r->isMatch('/blog/42/testing-routes', $request)); // test that the controller is as expected $this->assertEquals($r->getControllerClass(), 'example\\controllers\\DemoController'); // test that the action is a match $this->assertEquals($r->getActionMethod(), 'helloAction'); // test that the arguments are good $arguments = $r->getArguments(); $this->assertTrue(is_array($arguments)); $this->assertEquals($arguments['page'], 42); $this->assertEquals($arguments['section'], 'testing-routes'); // Test that optional arguments are correctly handled $this->assertTrue($r->isMatch('/blog/42/', $request)); $arguments = $r->getArguments(); $this->assertEquals($arguments['section'], 'fish'); // Check a POST request against a routes with a method $request = Request::create('/blog/42/testing-routes', 'POST'); $this->assertFalse($r->isMatch('/blog/42/', $request)); $request = Request::create('/blog/42/testing-routes', 'GET'); $this->assertTrue($r->isMatch('/blog/42/', $request)); }