public function UrlMatcherTest::testMatch

File

drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php, line 72

Class

UrlMatcherTest

Namespace

Symfony\Component\Routing\Tests\Matcher

Code

public function testMatch() {

  // test the patterns are matched and parameters are returned
  $collection = new RouteCollection();
  $collection
    ->add('foo', new Route('/foo/{bar}'));
  $matcher = new UrlMatcher($collection, new RequestContext());
  try {
    $matcher
      ->match('/no-match');
    $this
      ->fail();
  } catch (ResourceNotFoundException $e) {
  }
  $this
    ->assertEquals(array(
    '_route' => 'foo',
    'bar' => 'baz',
  ), $matcher
    ->match('/foo/baz'));

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

  // test that route "method" is ignored if no method is given in the context
  $collection = new RouteCollection();
  $collection
    ->add('foo', new Route('/foo', array(), array(
    '_method' => 'GET|head',
  )));
  $matcher = new UrlMatcher($collection, new RequestContext());
  $this
    ->assertInternalType('array', $matcher
    ->match('/foo'));

  // route does not match with POST method context
  $matcher = new UrlMatcher($collection, new RequestContext('', 'post'));
  try {
    $matcher
      ->match('/foo');
    $this
      ->fail();
  } catch (MethodNotAllowedException $e) {
  }

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

  // route with an optional variable as the first segment
  $collection = new RouteCollection();
  $collection
    ->add('bar', new Route('/{bar}/foo', array(
    'bar' => 'bar',
  ), array(
    'bar' => 'foo|bar',
  )));
  $matcher = new UrlMatcher($collection, new RequestContext());
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'bar' => 'bar',
  ), $matcher
    ->match('/bar/foo'));
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'bar' => 'foo',
  ), $matcher
    ->match('/foo/foo'));
  $collection = new RouteCollection();
  $collection
    ->add('bar', new Route('/{bar}', array(
    'bar' => 'bar',
  ), array(
    'bar' => 'foo|bar',
  )));
  $matcher = new UrlMatcher($collection, new RequestContext());
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'bar' => 'foo',
  ), $matcher
    ->match('/foo'));
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'bar' => 'bar',
  ), $matcher
    ->match('/'));

  // route with only optional variables
  $collection = new RouteCollection();
  $collection
    ->add('bar', new Route('/{foo}/{bar}', array(
    'foo' => 'foo',
    'bar' => 'bar',
  ), array()));
  $matcher = new UrlMatcher($collection, new RequestContext());
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'foo' => 'foo',
    'bar' => 'bar',
  ), $matcher
    ->match('/'));
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'foo' => 'a',
    'bar' => 'bar',
  ), $matcher
    ->match('/a'));
  $this
    ->assertEquals(array(
    '_route' => 'bar',
    'foo' => 'a',
    'bar' => 'b',
  ), $matcher
    ->match('/a/b'));
}