2010-11-25 02:50:21 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-11-25 02:50:21 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Component\Routing ;
2011-03-21 12:56:53 +00:00
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper ;
2010-11-25 02:50:21 +00:00
use Symfony\Component\Routing\Route ;
use Symfony\Component\Routing\RouteCollection ;
2011-04-20 12:54:39 +01:00
use Symfony\Component\Routing\RequestContext ;
2010-11-25 02:50:21 +00:00
class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump ()
2011-09-30 08:20:51 +01:00
{
$dumper = new PhpMatcherDumper ( $this -> getRouteCollection (), new RequestContext ());
$this -> assertStringEqualsFile ( __DIR__ . '/../../Fixtures/dumper/url_matcher1.php' , $dumper -> dump (), '->dump() dumps basic routes to the correct PHP file.' );
$collection = $this -> getRouteCollection ();
// force HTTPS redirection
$collection -> add ( 'secure' , new Route (
'/secure' ,
array (),
array ( '_scheme' => 'https' )
));
// force HTTP redirection
$collection -> add ( 'nonsecure' , new Route (
'/nonsecure' ,
array (),
array ( '_scheme' => 'http' )
));
$dumper = new PhpMatcherDumper ( $collection , new RequestContext ());
$this -> assertStringEqualsFile ( __DIR__ . '/../../Fixtures/dumper/url_matcher2.php' , $dumper -> dump ( array ( 'base_class' => 'Symfony\Tests\Component\Routing\Fixtures\RedirectableUrlMatcher' )), '->dump() dumps basic routes to the correct PHP file.' );
}
/**
* @ expectedException \LogicException
*/
public function testDumpWhenSchemeIsUsedWithoutAProperDumper ()
{
$collection = new RouteCollection ();
$collection -> add ( 'secure' , new Route (
'/secure' ,
array (),
array ( '_scheme' => 'https' )
));
$dumper = new PhpMatcherDumper ( $collection , new RequestContext ());
$dumper -> dump ();
}
protected function getRouteCollection ()
2010-11-25 02:50:21 +00:00
{
$collection = new RouteCollection ();
2011-09-30 08:20:51 +01:00
$collection -> add ( 'overriden' , new Route ( '/overriden' ));
2011-03-21 12:56:53 +00:00
// defaults and requirements
2010-11-25 14:49:33 +00:00
$collection -> add ( 'foo' , new Route (
2010-11-23 14:11:28 +00:00
'/foo/{bar}' ,
2010-11-25 02:50:21 +00:00
array ( 'def' => 'test' ),
array ( 'bar' => 'baz|symfony' )
));
2011-03-21 12:56:53 +00:00
// method requirement
2010-11-25 14:49:33 +00:00
$collection -> add ( 'bar' , new Route (
2010-11-23 14:11:28 +00:00
'/bar/{foo}' ,
2010-11-25 02:50:21 +00:00
array (),
2010-11-25 14:49:33 +00:00
array ( '_method' => 'GET|head' )
2010-11-25 02:50:21 +00:00
));
2011-06-04 11:28:01 +01:00
// GET method requirement automatically adds HEAD as valid
$collection -> add ( 'barhead' , new Route (
'/barhead/{foo}' ,
array (),
array ( '_method' => 'GET' )
));
2011-03-21 12:56:53 +00:00
// simple
2010-12-20 17:13:02 +00:00
$collection -> add ( 'baz' , new Route (
'/test/baz'
));
2011-03-21 12:56:53 +00:00
// simple with extension
2010-12-22 08:41:53 +00:00
$collection -> add ( 'baz2' , new Route (
'/test/baz.html'
));
2011-03-21 12:56:53 +00:00
// trailing slash
2011-02-10 22:03:19 +00:00
$collection -> add ( 'baz3' , new Route (
'/test/baz3/'
));
2011-03-21 12:56:53 +00:00
// trailing slash with variable
2011-02-10 22:03:19 +00:00
$collection -> add ( 'baz4' , new Route (
'/test/{foo}/'
));
2011-03-21 12:56:53 +00:00
// trailing slash and method
$collection -> add ( 'baz5' , new Route (
'/test/{foo}/' ,
array (),
array ( '_method' => 'post' )
));
2011-03-24 08:03:59 +00:00
// complex name
$collection -> add ( 'baz.baz6' , new Route (
'/test/{foo}/' ,
array (),
array ( '_method' => 'put' )
));
2011-04-05 10:54:15 +01:00
// defaults without variable
$collection -> add ( 'foofoo' , new Route (
'/foofoo' ,
array ( 'def' => 'test' )
));
2011-06-29 02:23:30 +01:00
// pattern with quotes
$collection -> add ( 'quoter' , new Route (
'/{quoter}' ,
array (),
array ( 'quoter' => '[\']+' )
));
2010-12-20 17:13:02 +00:00
2011-05-04 22:29:47 +01:00
// prefixes
$collection1 = new RouteCollection ();
2011-09-30 08:20:51 +01:00
$collection1 -> add ( 'overriden' , new Route ( '/overriden1' ));
$collection1 -> add ( 'foo1' , new Route ( '/{foo}' ));
$collection1 -> add ( 'bar1' , new Route ( '/{bar}' ));
2011-05-04 22:29:47 +01:00
$collection2 = new RouteCollection ();
2011-06-29 02:23:30 +01:00
$collection2 -> addCollection ( $collection1 , '/b\'b' );
2011-09-30 08:20:51 +01:00
$collection2 -> add ( 'overriden' , new Route ( '/overriden2' ));
2011-06-15 08:36:36 +01:00
$collection1 = new RouteCollection ();
2011-09-30 08:20:51 +01:00
$collection1 -> add ( 'foo2' , new Route ( '/{foo1}' ));
$collection1 -> add ( 'bar2' , new Route ( '/{bar1}' ));
2011-06-29 02:23:30 +01:00
$collection2 -> addCollection ( $collection1 , '/b\'b' );
2011-05-04 22:29:47 +01:00
$collection -> addCollection ( $collection2 , '/a' );
2011-06-04 18:44:04 +01:00
// "dynamic" prefix
$collection1 = new RouteCollection ();
2011-09-30 08:20:51 +01:00
$collection1 -> add ( 'foo3' , new Route ( '/{foo}' ));
$collection1 -> add ( 'bar3' , new Route ( '/{bar}' ));
2011-06-04 18:44:04 +01:00
$collection2 = new RouteCollection ();
$collection2 -> addCollection ( $collection1 , '/b' );
$collection -> addCollection ( $collection2 , '/{_locale}' );
2011-06-15 08:22:10 +01:00
$collection -> add ( 'ababa' , new Route ( '/ababa' ));
// some more prefixes
$collection1 = new RouteCollection ();
2011-09-30 08:20:51 +01:00
$collection1 -> add ( 'foo4' , new Route ( '/{foo}' ));
2011-06-15 08:22:10 +01:00
$collection -> addCollection ( $collection1 , '/aba' );
2011-09-30 08:20:51 +01:00
return $collection ;
2010-11-25 02:50:21 +00:00
}
}