function StateTest::testExistingGetValuesSetDuringRequest

Tests getValuesSetDuringRequest() method with an existing value.

@legacy-covers ::getValuesSetDuringRequest

File

core/tests/Drupal/Tests/Core/State/StateTest.php, line 432

Class

StateTest
Tests Drupal\Core\State\State.

Namespace

Drupal\Tests\Core\State

Code

public function testExistingGetValuesSetDuringRequest() : void {
  // Mock the state system in order to set "value" as the value for the key
  // "existing". This simulates this value already being set before the
  // request.
  $keyValueStorage = $this->getMockBuilder(KeyValueStoreInterface::class)
    ->getMock();
  $keyValueStorage->expects($this->once())
    ->method('get')
    ->with('existing')
    ->willReturn('value');
  $factory = $this->getMockBuilder(KeyValueFactoryInterface::class)
    ->getMock();
  $factory->expects($this->once())
    ->method('get')
    ->with('state')
    ->willReturn($keyValueStorage);
  $lock = $this->getMockBuilder(LockBackendInterface::class)
    ->getMock();
  $cache = $this->getMockBuilder(CacheBackendInterface::class)
    ->getMock();
  $state = new State($factory, $cache, $lock);
  // Confirm that the 'original' property returned by
  // getValuesSetDuringRequest() has the value set before the current request,
  // and that the 'value' property has the new value.
  $state->set('existing', 'new-value');
  $this->assertSame([
    'value' => 'new-value',
    'original' => 'value',
  ], $state->getValuesSetDuringRequest('existing'));
  // Confirm that setting the value again in the same request correctly
  // updates the 'value' property returned by getValuesSetDuringRequest(), but
  // the value for the 'original' property remains the same as what it was
  // set to before the request.
  $state->set('existing', 'newer-value');
  $this->assertSame([
    'value' => 'newer-value',
    'original' => 'value',
  ], $state->getValuesSetDuringRequest('existing'));
}

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