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 ;
use Symfony\Component\Routing\Route ;
use Symfony\Component\Routing\RouteCollection ;
use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper ;
class ApacheMatcherDumperTest extends \PHPUnit_Framework_TestCase
{
static protected $fixturesPath ;
static public function setUpBeforeClass ()
{
self :: $fixturesPath = realpath ( __DIR__ . '/../../Fixtures/' );
}
public function testDump ()
2012-04-10 13:42:59 +01:00
{
$dumper = new ApacheMatcherDumper ( $this -> getRouteCollection ());
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/dumper/url_matcher1.apache' , $dumper -> dump (), '->dump() dumps basic routes to the correct apache format.' );
}
/**
* @ dataProvider provideEscapeFixtures
*/
2012-04-10 13:53:59 +01:00
public function testEscapePattern ( $src , $dest , $char , $with , $message )
2012-04-10 13:42:59 +01:00
{
$r = new \ReflectionMethod ( new ApacheMatcherDumper ( $this -> getRouteCollection ()), 'escape' );
$r -> setAccessible ( true );
$this -> assertEquals ( $dest , $r -> invoke ( null , $src , $char , $with ), $message );
}
public function provideEscapeFixtures ()
{
return array (
array ( 'foo' , 'foo' , ' ' , '-' , 'Preserve string that should not be escaped' ),
array ( 'fo-o' , 'fo-o' , ' ' , '-' , 'Preserve string that should not be escaped' ),
array ( 'fo o' , 'fo- o' , ' ' , '-' , 'Escape special characters' ),
array ( 'fo-- o' , 'fo--- o' , ' ' , '-' , 'Escape special characters' ),
array ( 'fo- o' , 'fo- o' , ' ' , '-' , 'Do not escape already escaped string' ),
);
}
2012-04-10 13:53:59 +01:00
public function testEscapeScriptName ()
{
$collection = new RouteCollection ();
$collection -> add ( 'foo' , new Route ( '/foo' ));
$dumper = new ApacheMatcherDumper ( $collection );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/dumper/url_matcher2.apache' , $dumper -> dump ( array ( 'script_name' => 'ap p_d\ ev.php' )));
}
2012-04-10 13:42:59 +01:00
private function getRouteCollection ()
2010-11-25 02:50:21 +00:00
{
$collection = new RouteCollection ();
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:46:19 +01:00
// method requirement (again)
$collection -> add ( 'baragain' , new Route (
'/baragain/{foo}' ,
array (),
array ( '_method' => 'get|post' )
));
2011-03-21 12:56:53 +00:00
// simple
2011-02-10 22:03:53 +00:00
$collection -> add ( 'baz' , new Route (
2011-03-21 12:56:53 +00:00
'/test/baz'
2011-02-10 22:03:53 +00:00
));
2011-03-21 12:56:53 +00:00
// simple with extension
2011-02-10 22:03:53 +00:00
$collection -> add ( 'baz2' , new Route (
2011-03-21 12:56:53 +00:00
'/test/baz.html'
));
// trailing slash
$collection -> add ( 'baz3' , new Route (
'/test/baz3/'
));
// trailing slash with variable
$collection -> add ( 'baz4' , new Route (
2011-02-10 22:03:53 +00:00
'/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-06-29 02:07:48 +01:00
// complex
$collection -> add ( 'baz6' , new Route (
'/test/baz' ,
array ( 'foo' => 'bar baz' )
));
2012-04-10 13:42:59 +01:00
// space in path
$collection -> add ( 'baz7' , new Route (
'/te st/baz'
));
2011-02-10 22:03:53 +00:00
2012-04-10 13:42:59 +01:00
return $collection ;
2010-11-25 02:50:21 +00:00
}
2012-04-10 13:42:59 +01:00
}