class CurrentPathStackTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Path/CurrentPathStackTest.php \Drupal\Tests\Core\Path\CurrentPathStackTest

Tests Drupal\Core\Path\CurrentPathStack.

Attributes

#[CoversClass(CurrentPathStack::class)] #[Group('Path')]

Hierarchy

Expanded class hierarchy of CurrentPathStackTest

File

core/tests/Drupal/Tests/Core/Path/CurrentPathStackTest.php, line 17

Namespace

Drupal\Tests\Core\Path
View source
class CurrentPathStackTest extends UnitTestCase {
  
  /**
   * Tests that a path is kept per request, defaulting to the path info.
   */
  public function testPathIsKeptPerRequest() : void {
    $request_stack = new RequestStack();
    $current = Request::create('/node/1');
    $request_stack->push($current);
    $stack = new CurrentPathStack($request_stack);
    $other = Request::create('/node/2');
    $this->assertSame('/node/1', $stack->getPath());
    $stack->setPath('/system/path', $other);
    $this->assertSame('/system/path', $stack->getPath($other));
    $this->assertSame('/node/1', $stack->getPath($current));
  }
  
  /**
   * Tests that a request nothing else references is released.
   *
   * Code that matches many synthetic requests against the router in one
   * process, such as re-tracking entity usage from drush, would otherwise keep
   * every one of them for the life of the process.
   */
  public function testRequestNothingElseReferencesIsReleased() : void {
    $stack = new CurrentPathStack(new RequestStack());
    $request = Request::create('/node/1');
    $stack->setPath('/node/1', $request);
    $reference = \WeakReference::create($request);
    unset($request);
    $this->assertNull($reference->get());
  }

}

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