You are here

class ControllerResolverTest in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/http-kernel/Tests/Controller/ControllerResolverTest.php \Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest
  2. 8.0 core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php \Drupal\Tests\Core\Controller\ControllerResolverTest
Same name and namespace in other branches
  1. 8 vendor/symfony/http-kernel/Tests/Controller/ControllerResolverTest.php \Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest

Hierarchy

  • class \Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest extends \Symfony\Component\HttpKernel\Tests\Controller\PHPUnit_Framework_TestCase

Expanded class hierarchy of ControllerResolverTest

File

vendor/symfony/http-kernel/Tests/Controller/ControllerResolverTest.php, line 18

Namespace

Symfony\Component\HttpKernel\Tests\Controller
View source
class ControllerResolverTest extends \PHPUnit_Framework_TestCase {
  public function testGetControllerWithoutControllerParameter() {
    $logger = $this
      ->getMock('Psr\\Log\\LoggerInterface');
    $logger
      ->expects($this
      ->once())
      ->method('warning')
      ->with('Unable to look for the controller as the "_controller" parameter is missing.');
    $resolver = $this
      ->createControllerResolver($logger);
    $request = Request::create('/');
    $this
      ->assertFalse($resolver
      ->getController($request), '->getController() returns false when the request has no _controller attribute');
  }
  public function testGetControllerWithLambda() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', $lambda = function () {
    });
    $controller = $resolver
      ->getController($request);
    $this
      ->assertSame($lambda, $controller);
  }
  public function testGetControllerWithObjectAndInvokeMethod() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', $this);
    $controller = $resolver
      ->getController($request);
    $this
      ->assertSame($this, $controller);
  }
  public function testGetControllerWithObjectAndMethod() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', array(
      $this,
      'controllerMethod1',
    ));
    $controller = $resolver
      ->getController($request);
    $this
      ->assertSame(array(
      $this,
      'controllerMethod1',
    ), $controller);
  }
  public function testGetControllerWithClassAndMethod() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', array(
      'Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest',
      'controllerMethod4',
    ));
    $controller = $resolver
      ->getController($request);
    $this
      ->assertSame(array(
      'Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest',
      'controllerMethod4',
    ), $controller);
  }
  public function testGetControllerWithObjectAndMethodAsString() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', 'Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest::controllerMethod1');
    $controller = $resolver
      ->getController($request);
    $this
      ->assertInstanceOf('Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
  }
  public function testGetControllerWithClassAndInvokeMethod() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', 'Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest');
    $controller = $resolver
      ->getController($request);
    $this
      ->assertInstanceOf('Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest', $controller);
  }

  /**
   * @expectedException \InvalidArgumentException
   */
  public function testGetControllerOnObjectWithoutInvokeMethod() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', new \stdClass());
    $resolver
      ->getController($request);
  }
  public function testGetControllerWithFunction() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', 'Symfony\\Component\\HttpKernel\\Tests\\Controller\\some_controller_function');
    $controller = $resolver
      ->getController($request);
    $this
      ->assertSame('Symfony\\Component\\HttpKernel\\Tests\\Controller\\some_controller_function', $controller);
  }

  /**
   * @dataProvider      getUndefinedControllers
   * @expectedException \InvalidArgumentException
   */
  public function testGetControllerOnNonUndefinedFunction($controller) {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', $controller);
    $resolver
      ->getController($request);
  }
  public function getUndefinedControllers() {
    return array(
      array(
        'foo',
      ),
      array(
        'foo::bar',
      ),
      array(
        'stdClass',
      ),
      array(
        'Symfony\\Component\\HttpKernel\\Tests\\Controller\\ControllerResolverTest::bar',
      ),
    );
  }
  public function testGetArguments() {
    $resolver = $this
      ->createControllerResolver();
    $request = Request::create('/');
    $controller = array(
      new self(),
      'testGetArguments',
    );
    $this
      ->assertEquals(array(), $resolver
      ->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $controller = array(
      new self(),
      'controllerMethod1',
    );
    $this
      ->assertEquals(array(
      'foo',
    ), $resolver
      ->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $controller = array(
      new self(),
      'controllerMethod2',
    );
    $this
      ->assertEquals(array(
      'foo',
      null,
    ), $resolver
      ->getArguments($request, $controller), '->getArguments() uses default values if present');
    $request->attributes
      ->set('bar', 'bar');
    $this
      ->assertEquals(array(
      'foo',
      'bar',
    ), $resolver
      ->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $controller = function ($foo) {
    };
    $this
      ->assertEquals(array(
      'foo',
    ), $resolver
      ->getArguments($request, $controller));
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $controller = function ($foo, $bar = 'bar') {
    };
    $this
      ->assertEquals(array(
      'foo',
      'bar',
    ), $resolver
      ->getArguments($request, $controller));
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $controller = new self();
    $this
      ->assertEquals(array(
      'foo',
      null,
    ), $resolver
      ->getArguments($request, $controller));
    $request->attributes
      ->set('bar', 'bar');
    $this
      ->assertEquals(array(
      'foo',
      'bar',
    ), $resolver
      ->getArguments($request, $controller));
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $request->attributes
      ->set('foobar', 'foobar');
    $controller = 'Symfony\\Component\\HttpKernel\\Tests\\Controller\\some_controller_function';
    $this
      ->assertEquals(array(
      'foo',
      'foobar',
    ), $resolver
      ->getArguments($request, $controller));
    $request = Request::create('/');
    $request->attributes
      ->set('foo', 'foo');
    $request->attributes
      ->set('foobar', 'foobar');
    $controller = array(
      new self(),
      'controllerMethod3',
    );
    if (PHP_VERSION_ID === 50316) {
      $this
        ->markTestSkipped('PHP 5.3.16 has a major bug in the Reflection sub-system');
    }
    else {
      try {
        $resolver
          ->getArguments($request, $controller);
        $this
          ->fail('->getArguments() throws a \\RuntimeException exception if it cannot determine the argument value');
      } catch (\Exception $e) {
        $this
          ->assertInstanceOf('\\RuntimeException', $e, '->getArguments() throws a \\RuntimeException exception if it cannot determine the argument value');
      }
    }
    $request = Request::create('/');
    $controller = array(
      new self(),
      'controllerMethod5',
    );
    $this
      ->assertEquals(array(
      $request,
    ), $resolver
      ->getArguments($request, $controller), '->getArguments() injects the request');
  }
  public function testCreateControllerCanReturnAnyCallable() {
    $mock = $this
      ->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver', array(
      'createController',
    ));
    $mock
      ->expects($this
      ->once())
      ->method('createController')
      ->will($this
      ->returnValue('Symfony\\Component\\HttpKernel\\Tests\\Controller\\some_controller_function'));
    $request = Request::create('/');
    $request->attributes
      ->set('_controller', 'foobar');
    $mock
      ->getController($request);
  }
  protected function createControllerResolver(LoggerInterface $logger = null) {
    return new ControllerResolver($logger);
  }
  public function __invoke($foo, $bar = null) {
  }
  public function controllerMethod1($foo) {
  }
  protected function controllerMethod2($foo, $bar = null) {
  }
  protected function controllerMethod3($foo, $bar = null, $foobar) {
  }
  protected static function controllerMethod4() {
  }
  protected function controllerMethod5(Request $request) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerResolverTest::controllerMethod1 public function
ControllerResolverTest::controllerMethod2 protected function
ControllerResolverTest::controllerMethod3 protected function
ControllerResolverTest::controllerMethod4 protected static function
ControllerResolverTest::controllerMethod5 protected function
ControllerResolverTest::createControllerResolver protected function
ControllerResolverTest::getUndefinedControllers public function
ControllerResolverTest::testCreateControllerCanReturnAnyCallable public function
ControllerResolverTest::testGetArguments public function
ControllerResolverTest::testGetControllerOnNonUndefinedFunction public function @dataProvider getUndefinedControllers @expectedException \InvalidArgumentException
ControllerResolverTest::testGetControllerOnObjectWithoutInvokeMethod public function @expectedException \InvalidArgumentException
ControllerResolverTest::testGetControllerWithClassAndInvokeMethod public function
ControllerResolverTest::testGetControllerWithClassAndMethod public function
ControllerResolverTest::testGetControllerWithFunction public function
ControllerResolverTest::testGetControllerWithLambda public function
ControllerResolverTest::testGetControllerWithObjectAndInvokeMethod public function
ControllerResolverTest::testGetControllerWithObjectAndMethod public function
ControllerResolverTest::testGetControllerWithObjectAndMethodAsString public function
ControllerResolverTest::testGetControllerWithoutControllerParameter public function
ControllerResolverTest::__invoke public function