From 1e9e1b346d248b706235370de2d7bd4552e33f38 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 24 Nov 2010 20:50:21 -0600 Subject: [PATCH] [Routing] Adding tests for the ApacheMatcherDumper, PhpMatcherDumper and UrlMatcher. --- .../Matcher/Dumper/ApacheMatcherDumper.php | 4 +- .../Fixtures/dumper/url_matcher1.apache | 7 +++ .../Routing/Fixtures/dumper/url_matcher1.php | 34 ++++++++++++++ .../Dumper/ApacheMatcherDumperTest.php | 46 +++++++++++++++++++ .../Matcher/Dumper/PhpMatcherDumperTest.php | 44 ++++++++++++++++++ .../Routing/Matcher/UrlMatcherTest.php | 34 ++++++++++++++ 6 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.apache create mode 100644 tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php create mode 100644 tests/Symfony/Tests/Component/Routing/Matcher/Dumper/ApacheMatcherDumperTest.php create mode 100644 tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php index f9a53c6b6761..a65e14420f44 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php @@ -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 */ diff --git a/tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.apache b/tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.apache new file mode 100644 index 000000000000..ba9264ccf6f9 --- /dev/null +++ b/tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.apache @@ -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] \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php b/tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php new file mode 100644 index 000000000000..99d08e1e5152 --- /dev/null +++ b/tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php @@ -0,0 +1,34 @@ +context = $context; + $this->defaults = $defaults; + } + + public function match($url) + { + $url = $this->normalizeUrl($url); + + if (0 === strpos($url, '/foo') && preg_match('#^/foo/(?Pbaz|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[^/\.]+?)$#x', $url, $matches)) { + return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'bar')); + } + + return false; + } +} diff --git a/tests/Symfony/Tests/Component/Routing/Matcher/Dumper/ApacheMatcherDumperTest.php b/tests/Symfony/Tests/Component/Routing/Matcher/Dumper/ApacheMatcherDumperTest.php new file mode 100644 index 000000000000..fcc1b4b30564 --- /dev/null +++ b/tests/Symfony/Tests/Component/Routing/Matcher/Dumper/ApacheMatcherDumperTest.php @@ -0,0 +1,46 @@ + + * + * 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.'); + } +} diff --git a/tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php b/tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php new file mode 100644 index 000000000000..fdd8eb8c8748 --- /dev/null +++ b/tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php @@ -0,0 +1,44 @@ + + * + * 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.'); + } +} diff --git a/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php b/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php index 6bd6749ef618..1a5e12c6331b 100644 --- a/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php +++ b/tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php @@ -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