Skip to content

Commit

Permalink
[Routing] Adding tests for the ApacheMatcherDumper, PhpMatcherDumper …
Browse files Browse the repository at this point in the history
…and UrlMatcher.
  • Loading branch information
weaverryan authored and fabpot committed Nov 27, 2010
1 parent 547eaa8 commit 1e9e1b3
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 2 deletions.
Expand Up @@ -25,11 +25,11 @@ class ApacheMatcherDumper extends MatcherDumper
*
* Available options:
*
* * script_name: The script name (index.php by default)
* * script_name: The script name (app.php by default)
*
* @param array $options An array of options
*
* @return string A PHP class representing the matcher class
* @return string A string to be used as Apache rewrite rules.
*
* @throws \RuntimeException When a route has more than 9 variables
*/
Expand Down
@@ -0,0 +1,7 @@

RewriteCond %{PATH_INFO} ^/foo/(baz|symfony)$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING_def:test]
RewriteCond %{REQUEST_METHOD} =GET [OR]
RewriteCond %{REQUEST_METHOD} =HEAD
RewriteCond %{PATH_INFO} ^/bar/([^/\.]+?)$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:bar,E=_ROUTING_foo:%1]
@@ -0,0 +1,34 @@
<?php

/**
* ProjectUrlMatcher
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
{
/**
* Constructor.
*/
public function __construct(array $context = array(), array $defaults = array())
{
$this->context = $context;
$this->defaults = $defaults;
}

public function match($url)
{
$url = $this->normalizeUrl($url);

if (0 === strpos($url, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#x', $url, $matches)) {
return array_merge($this->mergeDefaults($matches, array ( 'def' => 'test',)), array('_route' => 'foo'));
}

if (isset($this->context['method']) && in_array(strtolower($this->context['method']), array ( 0 => 'get', 1 => 'head',)) && 0 === strpos($url, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $url, $matches)) {
return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'bar'));
}

return false;
}
}
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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()
{
$collection = new RouteCollection();

$collection->addRoute('foo', new Route(
'/foo/:bar',
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
$collection->addRoute('bar', new Route(
'/bar/:foo',
array(),
array('_method' => array('GET', 'HEAD'))
));

$dumper = new ApacheMatcherDumper($collection);

$this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
}
}
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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\PhpMatcherDumper;

class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
{
static protected $fixturesPath;

static public function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
}

public function testDump()
{
$collection = new RouteCollection();

$collection->addRoute('foo', new Route(
'/foo/:bar',
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
$collection->addRoute('bar', new Route(
'/bar/:foo',
array(),
array('_method' => array('GET', 'HEAD'))
));
$dumper = new PhpMatcherDumper($collection);
$this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');
}
}
34 changes: 34 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php
Expand Up @@ -29,6 +29,40 @@ public function testNormalizeUrl()
$this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string');
$this->assertEquals('/foo/bar', $matcher->normalizeUrl('/foo//bar'), '->normalizeUrl() removes duplicated /');
}

public function testMatch()
{
// test the patterns are matched are parameters are returned
$collection = new RouteCollection();
$collection->addRoute('foo', new Route('/foo/:bar'));
$matcher = new UrlMatcher($collection, array(), array());
$this->assertEquals(false, $matcher->match('/no-match'));
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));

// test that defaults are merged
$collection = new RouteCollection();
$collection->addRoute('foo', new Route('/foo/:bar', array('def' => 'test')));
$matcher = new UrlMatcher($collection, array(), array());
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));

// test that route "metod" is ignore if no method is given in the context
$collection = new RouteCollection();
$collection->addRoute('foo', new Route('/foo', array(), array('_method' => array('GET', 'HEAD'))));

// route matches with no context
$matcher = new UrlMatcher($collection, array(), array());
$this->assertNotEquals(false, $matcher->match('/foo'));

// route does not match with POST method context
$matcher = new UrlMatcher($collection, array('method' => 'POST'), array());
$this->assertEquals(false, $matcher->match('/foo'));

// route does match with GET or HEAD method context
$matcher = new UrlMatcher($collection, array('method' => 'GET'), array());
$this->assertNotEquals(false, $matcher->match('/foo'));
$matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
$this->assertNotEquals(false, $matcher->match('/foo'));
}
}

class UrlMatcherForTests extends UrlMatcher
Expand Down

0 comments on commit 1e9e1b3

Please sign in to comment.