function LinearHistoryTest::testAdd

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php \Drupal\Tests\Core\Config\Checkpoint\LinearHistoryTest::testAdd()

@covers ::add @covers ::count @covers ::getActiveCheckpoint @covers \Drupal\Core\Config\Checkpoint\Checkpoint

File

core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php, line 36

Class

LinearHistoryTest
@coversDefaultClass \Drupal\Core\Config\Checkpoint\LinearHistory @group Config

Namespace

Drupal\Tests\Core\Config\Checkpoint

Code

public function testAdd() : void {
    $state = $this->prophesize(StateInterface::class);
    $state->get(self::CHECKPOINT_KEY, [])
        ->willReturn([]);
    $state->set(self::CHECKPOINT_KEY, Argument::any())
        ->willReturn(NULL);
    $time = $this->prophesize(TimeInterface::class);
    $time->getCurrentTime()
        ->willReturn(1701539520, 1701539994);
    $checkpoints = new LinearHistory($state->reveal(), $time->reveal());
    $this->assertCount(0, $checkpoints);
    $this->assertNull($checkpoints->getActiveCheckpoint());
    $checkpoint = $checkpoints->add('hash1', 'Label');
    $this->assertSame('hash1', $checkpoint->id);
    $this->assertSame('Label', $checkpoint->label);
    $this->assertNull($checkpoint->parent);
    $this->assertSame(1701539520, $checkpoint->timestamp);
    $this->assertCount(1, $checkpoints);
    $this->assertSame('hash1', $checkpoints->getActiveCheckpoint()?->id);
    // Test that on the second call to add the ancestor is set correctly.
    $checkpoint2 = $checkpoints->add('hash2', new FormattableMarkup('Another label', []));
    $this->assertSame('hash2', $checkpoint2->id);
    $this->assertSame('Another label', (string) $checkpoint2->label);
    $this->assertSame($checkpoint->id, $checkpoint2->parent);
    $this->assertSame(1701539994, $checkpoint2->timestamp);
    $this->assertCount(2, $checkpoints);
    $this->assertSame('hash2', $checkpoints->getActiveCheckpoint()?->id);
    // Test that the checkpoints object can be iterated over.
    $i = 0;
    foreach ($checkpoints as $value) {
        $i++;
        $this->assertInstanceOf(Checkpoint::class, $value);
        $this->assertSame('hash' . $i, $value->id);
    }
}

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