You are here

class NestedMatcherTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony-cmf/routing/Tests/NestedMatcher/NestedMatcherTest.php \Symfony\Cmf\Component\Routing\Tests\NestedMatcher\NestedMatcherTest

Hierarchy

  • class \Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase extends \Symfony\Cmf\Component\Routing\Test\PHPUnit_Framework_TestCase

Expanded class hierarchy of NestedMatcherTest

File

vendor/symfony-cmf/routing/Tests/NestedMatcher/NestedMatcherTest.php, line 24

Namespace

Symfony\Cmf\Component\Routing\Tests\NestedMatcher
View source
class NestedMatcherTest extends CmfUnitTestCase {
  private $provider;
  private $routeFilter1;
  private $routeFilter2;
  private $finalMatcher;
  public function setUp() {
    $this->provider = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\RouteProviderInterface');
    $this->routeFilter1 = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\NestedMatcher\\RouteFilterInterface');
    $this->routeFilter2 = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\NestedMatcher\\RouteFilterInterface');
    $this->finalMatcher = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\NestedMatcher\\FinalMatcherInterface');
  }
  public function testNestedMatcher() {
    $request = Request::create('/path/one');
    $routeCollection = new RouteCollection();
    $route = $this
      ->getMockBuilder('Symfony\\Component\\Routing\\Route')
      ->disableOriginalConstructor()
      ->getMock();
    $routeCollection
      ->add('route', $route);
    $this->provider
      ->expects($this
      ->once())
      ->method('getRouteCollectionForRequest')
      ->with($request)
      ->will($this
      ->returnValue($routeCollection));
    $this->routeFilter1
      ->expects($this
      ->once())
      ->method('filter')
      ->with($routeCollection, $request)
      ->will($this
      ->returnValue($routeCollection));
    $this->routeFilter2
      ->expects($this
      ->once())
      ->method('filter')
      ->with($routeCollection, $request)
      ->will($this
      ->returnValue($routeCollection));
    $this->finalMatcher
      ->expects($this
      ->once())
      ->method('finalMatch')
      ->with($routeCollection, $request)
      ->will($this
      ->returnValue(array(
      'foo' => 'bar',
    )));
    $matcher = new NestedMatcher($this->provider, $this->finalMatcher);
    $matcher
      ->addRouteFilter($this->routeFilter1);
    $matcher
      ->addRouteFilter($this->routeFilter2);
    $attributes = $matcher
      ->matchRequest($request);
    $this
      ->assertEquals(array(
      'foo' => 'bar',
    ), $attributes);
  }

  /**
   * Test priorities and exception handling
   */
  public function testNestedMatcherPriority() {
    $request = Request::create('/path/one');
    $routeCollection = new RouteCollection();
    $route = $this
      ->getMockBuilder('Symfony\\Component\\Routing\\Route')
      ->disableOriginalConstructor()
      ->getMock();
    $routeCollection
      ->add('route', $route);
    $wrongProvider = $this
      ->buildMock('Symfony\\Cmf\\Component\\Routing\\RouteProviderInterface');
    $wrongProvider
      ->expects($this
      ->never())
      ->method('getRouteCollectionForRequest');
    $this->provider
      ->expects($this
      ->once())
      ->method('getRouteCollectionForRequest')
      ->with($request)
      ->will($this
      ->returnValue($routeCollection));
    $this->routeFilter1
      ->expects($this
      ->once())
      ->method('filter')
      ->with($routeCollection, $request)
      ->will($this
      ->throwException(new ResourceNotFoundException()));
    $this->routeFilter2
      ->expects($this
      ->never())
      ->method('filter');
    $this->finalMatcher
      ->expects($this
      ->never())
      ->method('finalMatch');
    $matcher = new NestedMatcher($wrongProvider, $this->finalMatcher);
    $matcher
      ->setRouteProvider($this->provider);
    $matcher
      ->addRouteFilter($this->routeFilter2, 10);
    $matcher
      ->addRouteFilter($this->routeFilter1, 20);
    try {
      $matcher
        ->matchRequest($request);
      fail('nested matcher is eating exception');
    } catch (ResourceNotFoundException $e) {

      // expected
    }
  }
  public function testProviderNoMatch() {
    $request = Request::create('/path/one');
    $routeCollection = new RouteCollection();
    $this->provider
      ->expects($this
      ->once())
      ->method('getRouteCollectionForRequest')
      ->with($request)
      ->will($this
      ->returnValue($routeCollection));
    $this->finalMatcher
      ->expects($this
      ->never())
      ->method('finalMatch');
    $matcher = new NestedMatcher($this->provider, $this->finalMatcher);
    $this
      ->setExpectedException('Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException');
    $matcher
      ->matchRequest($request);
  }

}

Members