public function ControllerResolverTest::testGetArguments

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php, line 88

Class

ControllerResolverTest

Namespace

Symfony\Component\HttpKernel\Tests

Code

public function testGetArguments() {
  $resolver = new ControllerResolver();
  $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\\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 (version_compare(PHP_VERSION, '5.3.16', '==')) {
    $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');
}