function StateTest::testGetEmpty

Tests both get() & getMultiple() method.

Here testing a not existing variable and set and get the default value of a key.

@covers ::get
@covers ::getMultiple

File

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

Class

StateTest
@coversDefaultClass \Drupal\Core\State\State[[api-linebreak]] @group State

Namespace

Drupal\Tests\Core\State

Code

public function testGetEmpty() : void {
  $values = [
    'key1' => 'value1',
    'key2' => 'value2',
    'not-existing-value',
  ];
  $this->keyValueStorage
    ->expects($this->once())
    ->method('setMultiple')
    ->with($values);
  $this->state
    ->setMultiple($values);
  $this->assertNull($this->state
    ->get('not-existing-value'));
  $this->assertEquals([
    "not-existing-value" => NULL,
  ], $this->state
    ->getMultiple([
    'not-existing-value',
  ]));
  $this->assertNull($this->state
    ->get('not-existing'));
  $this->assertEquals([
    "not-existing" => NULL,
  ], $this->state
    ->getMultiple([
    'not-existing',
  ]));
  $this->assertEquals('default', $this->state
    ->get('default-value', 'default'));
  $this->assertEquals([
    "default-value" => NULL,
  ], $this->state
    ->getMultiple([
    'default-value',
  ]));
}

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