class UrlGeneratorTraitTest

@coversDefaultClass \Drupal\Core\Routing\UrlGeneratorTrait
@group Routing @group legacy

Hierarchy

Expanded class hierarchy of UrlGeneratorTraitTest

File

core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTraitTest.php, line 12

Namespace

Drupal\Tests\Core\Routing
View source
class UrlGeneratorTraitTest extends UnitTestCase {
  
  /**
   * @covers ::setUrlGenerator
   * @covers ::getUrlGenerator
   *
   * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::setUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/2614344
   * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::getUrlGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the url_generator service instead. See https://www.drupal.org/node/2614344
   */
  public function testGetUrlGenerator() {
    $url_generator = $this->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
    $url_generator_trait_object = $this->getMockForTrait('Drupal\\Core\\Routing\\UrlGeneratorTrait');
    $url_generator_trait_object->setUrlGenerator($url_generator);
    $url_generator_method = new \ReflectionMethod($url_generator_trait_object, 'getUrlGenerator');
    $url_generator_method->setAccessible(TRUE);
    $result = $url_generator_method->invoke($url_generator_trait_object);
    $this->assertEquals($url_generator, $result);
  }
  
  /**
   * @covers ::redirect
   *
   * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::redirect() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use new RedirectResponse(Url::fromRoute()) instead. See https://www.drupal.org/node/2614344
   */
  public function testRedirect() {
    $route_name = 'some_route_name';
    $generated_url = 'some/generated/url';
    $url_generator = $this->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
    $url_generator->expects($this->once())
      ->method('generateFromRoute')
      ->with($route_name, [], [
      'absolute' => TRUE,
    ])
      ->willReturn($generated_url);
    $url_generator_trait_object = $this->getMockForTrait('Drupal\\Core\\Routing\\UrlGeneratorTrait');
    $url_generator_trait_object->setUrlGenerator($url_generator);
    $url_generator_method = new \ReflectionMethod($url_generator_trait_object, 'redirect');
    $url_generator_method->setAccessible(TRUE);
    $result = $url_generator_method->invoke($url_generator_trait_object, $route_name);
    $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $result);
    $this->assertSame($generated_url, $result->getTargetUrl());
  }
  
  /**
   * @covers ::url
   *
   * @expectedDeprecation Drupal\Core\Routing\UrlGeneratorTrait::url() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Url::fromUri() instead. See https://www.drupal.org/node/2614344
   */
  public function testUrl() {
    $route_name = 'some_route_name';
    $generated_url = 'some/generated/url';
    $url_generator = $this->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
    $url_generator->expects($this->once())
      ->method('generateFromRoute')
      ->with($route_name, [], [])
      ->willReturn($generated_url);
    $url_generator_trait_object = $this->getMockForTrait('Drupal\\Core\\Routing\\UrlGeneratorTrait');
    $url_generator_trait_object->setUrlGenerator($url_generator);
    $url_generator_method = new \ReflectionMethod($url_generator_trait_object, 'url');
    $url_generator_method->setAccessible(TRUE);
    $result = $url_generator_method->invoke($url_generator_trait_object, $route_name);
    $this->assertSame($generated_url, $result);
  }

}

Members

Title Sort descending Modifiers Object type Summary
UrlGeneratorTraitTest::testGetUrlGenerator public function @covers ::setUrlGenerator[[api-linebreak]]
@covers ::getUrlGenerator[[api-linebreak]]
UrlGeneratorTraitTest::testRedirect public function @covers ::redirect[[api-linebreak]]
UrlGeneratorTraitTest::testUrl public function @covers ::url[[api-linebreak]]

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.