uri = ''; $route = new ezcMvcRegexpRoute( '@^$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array(), $request->variables ); } public function testNoMatchEmpty() { $request = new ezcMvcRequest; $request->uri = 'notEmpty'; $route = new ezcMvcRegexpRoute( '@^$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( null, $routeInfo ); } public function testMatchEmptyDefaultVars() { $request = new ezcMvcRequest; $request->uri = ''; $route = new ezcMvcRegexpRoute( '@^$@', 'testController', 'action', array( 'default1' => 'Reality is merely an illusion, albeit a very persistent one.' ) ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( 'action', $routeInfo->action ); self::assertSame( array( 'default1' => 'Reality is merely an illusion, albeit a very persistent one.' ), $request->variables ); } public function testsMatchNonEmptyNoVars() { $request = new ezcMvcRequest; $request->uri = 'people/einstein'; $route = new ezcMvcRegexpRoute( '@^people/(.*)$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array(), $request->variables ); } public function testsMatchNonEmptyDefaultVar() { $request = new ezcMvcRequest; $request->uri = 'people/einstein'; $route = new ezcMvcRegexpRoute( '@^people/(.*)$@', 'testController', 'action', array( 'name' => 'rethans' ) ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array( 'name' => 'rethans' ), $request->variables ); } public function testsMatchNonEmptyOneVar() { $request = new ezcMvcRequest; $request->uri = 'people/einstein'; $route = new ezcMvcRegexpRoute( '@^people/(?P.*)$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array( 'name' => 'einstein' ), $request->variables ); } public function testsMatchNonEmptyDefaultVarReused() { $request = new ezcMvcRequest; $request->uri = 'people/einstein'; $route = new ezcMvcRegexpRoute( '@^people/(?P.*)$@', 'testController', array( 'name' => 'rethans' ) ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array( 'name' => 'einstein' ), $request->variables ); } public function testsMatchNonEmptyTwoVars() { $request = new ezcMvcRequest; $request->uri = 'people/hawking'; $route = new ezcMvcRegexpRoute( '@^(?Ppeople)/(?P.*)$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array( 'group' => 'people', 'name' => 'hawking' ), $request->variables ); } public function testsNoMatchNonEmptyTwoVars() { $request = new ezcMvcRequest; $request->uri = 'folks/hawking'; $route = new ezcMvcRegexpRoute( '@^(?Ppeople)/(?P.*)$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( null, $routeInfo ); } public function testsMatchComplex() { $route = new ezcMvcRegexpRoute( '@^people(/((?P[0-9]+)|(?P.+)))?$@', 'testController', 'action', array( 'nr' => '', 'name' => '' ) ); $request = new ezcMvcRequest; $request->uri = 'people/hawking'; $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertEquals( array( 'nr' => '', 'name' => 'hawking' ), $request->variables ); $request = new ezcMvcRequest; $request->uri = 'people/42'; $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertEquals( array( 'nr' => '42', 'name' => '' ), $request->variables ); $request = new ezcMvcRequest; $request->uri = 'people'; $routeInfo = $route->matches( $request ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertEquals( array( 'nr' => '', 'name' => '' ), $request->variables ); $request = new ezcMvcRequest; $request->uri = 'people/'; $routeInfo = $route->matches( $request ); self::assertEquals( null, $routeInfo ); } public function testPrefixSameDelims() { $route = new testRegexpRoute( '@^entry/(?P[0-9]+)@', 'ignored' ); $route->prefix( '@^blog/@' ); self::assertEquals( '@^blog/entry/(?P[0-9]+)@', $route->getPattern() ); } public function testPrefixDifferentDelims() { $route = new testRegexpRoute( '@^entry/(?P[0-9]+)@', 'ignored' ); $route->prefix( '#^blog/#' ); self::assertEquals( '@^blog/entry/(?P[0-9]+)@', $route->getPattern() ); } public function testPrefixSameDelimsSameSpecifiers() { $route = new testRegexpRoute( '@^entry/(?P[0-9]+)@i', 'ignored' ); $route->prefix( '@^blog/@i' ); self::assertEquals( '@^blog/entry/(?P[0-9]+)@i', $route->getPattern() ); } public function testPrefixSameDelimsDifferentSpecifiers() { $route = new testRegexpRoute( '@^entry/(?P[0-9]+)@i', 'ignored' ); try { $route->prefix( '@^blog/@' ); self::fail( "Expected exception not thrown." ); } catch ( ezcMvcRegexpRouteException $e ) { self::assertEquals( "The pattern modifiers of the prefix '@^blog/@' and pattern '@^entry/(?P[0-9]+)@i' do not match.", $e->getMessage() ); } } public function testPrefixDifferentDelimsSameSpecifiers() { $route = new testRegexpRoute( '@^entry/(?P[0-9]+)@i', 'ignored' ); $route->prefix( '#^blog/#i' ); self::assertEquals( '@^blog/entry/(?P[0-9]+)@i', $route->getPattern() ); } public function testPrefixDifferentDelimsDifferentSpecifiers() { $route = new testRegexpRoute( '@^entry/(?P[0-9]+)@i', 'ignored' ); try { $route->prefix( '#^blog/#' ); self::fail( "Expected exception not thrown." ); } catch ( ezcMvcRegexpRouteException $e ) { self::assertEquals( "The pattern modifiers of the prefix '#^blog/#' and pattern '@^entry/(?P[0-9]+)@i' do not match.", $e->getMessage() ); } } public function testMatchWithDifferentUriMatch() { $request = new ezcMvcRequest; $request->host = 'test.host'; $request->uri = '/people/hawking'; $request->requestId = $request->host . $request->uri; $route = new testRegexpRouteForFullUri( '@^(?P[a-z]+).host/(?P[a-z]+)/(?P[a-z]+)$@', 'testController' ); $routeInfo = $route->matches( $request ); self::assertSame( '@^(?P[a-z]+).host/(?P[a-z]+)/(?P[a-z]+)$@', $routeInfo->matchedRoute ); self::assertSame( 'testController', $routeInfo->controllerClass ); self::assertSame( array( 'site' => 'test', 'group' => 'people', 'name' => 'hawking' ), $request->variables ); } static public function generateUrlProvder() { return array( array( // bare minimum '@(?P.*)@', 'val1' ), array( // with $ suffix '@/bar/(?P[^/]*)/test/$@', '/bar/val1/test/' ), array( // with ^ prefix '@^/bar/(?P[^/]*)/test/@', '/bar/val1/test/' ), array( // with ^ prefix and $ suffix '@^/bar/(?P[^/]*)/test/$@', '/bar/val1/test/' ), array( // with prefix and suffix '@/bar/(?P[^/]*)/test/@', '/bar/val1/test/' ), array( // with nested parentesis '@/bar/(?P(foo|bar)[^/]*)/test/@', '/bar/val1/test/' ), array( // with 2 arguments '@/bar/(?P[^/]*)/(?P[^/]*)/@', '/bar/val1/val2/', ), ); } /** * @dataProvider generateUrlProvder */ public function testGenerateUrl( $pattern, $expected ) { $fixture = new ezcMvcRegexpRoute( $pattern, 'testController' ); $result = $fixture->generateUrl( array( 'arg1' => 'val1', 'arg2' => 'val2', ) ); $this->assertEquals( $expected, $result ); } public static function suite() { return new PHPUnit_Framework_TestSuite( "ezcMvcToolsRegexpRouteTest" ); } } ?>