class LinearHistoryTest
@coversDefaultClass \Drupal\Core\Config\Checkpoint\LinearHistory
@group Config
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Config\Checkpoint\LinearHistoryTest implements \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LinearHistoryTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Config/ Checkpoint/ LinearHistoryTest.php, line 25
Namespace
Drupal\Tests\Core\Config\CheckpointView source
class LinearHistoryTest extends UnitTestCase {
/**
* The key used store of all the checkpoint names in state.
*
* @see \Drupal\Core\Config\Checkpoint\Checkpoints::CHECKPOINT_KEY
*/
private const CHECKPOINT_KEY = 'config.checkpoints';
/**
* @covers ::add
* @covers ::count
* @covers ::getActiveCheckpoint
* @covers \Drupal\Core\Config\Checkpoint\Checkpoint
*/
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);
}
}
/**
* @covers ::add
*/
public function testAddException() : 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());
$checkpoints->add('hash1', 'Label');
// Add another checkpoint with the same ID and an exception should be
// triggered.
$this->expectException(CheckpointExistsException::class);
$this->expectExceptionMessage('Cannot create a checkpoint with the ID "hash1" as it already exists');
$checkpoints->add('hash1', 'Label');
}
/**
* @covers ::delete
*/
public function testDeleteAll() : void {
$state = $this->prophesize(StateInterface::class);
$state->get(self::CHECKPOINT_KEY, [])
->willReturn([
'hash1' => new Checkpoint('hash1', 'One', 1701539510, NULL),
'hash2' => new Checkpoint('hash2', 'Two', 1701539520, 'hash1'),
'hash3' => new Checkpoint('hash3', 'Three', 1701539530, 'hash2'),
]);
$state->delete(self::CHECKPOINT_KEY)
->willReturn();
$time = $this->prophesize(TimeInterface::class);
$checkpoints = new LinearHistory($state->reveal(), $time->reveal());
$this->assertCount(3, $checkpoints);
$this->assertSame('hash3', $checkpoints->getActiveCheckpoint()?->id);
$checkpoints->deleteAll();
$this->assertCount(0, $checkpoints);
$this->assertNull($checkpoints->getActiveCheckpoint());
}
/**
* @covers ::delete
*/
public function testDelete() : void {
// Create a real State object so that we can manipulate it.
$state = new State(new KeyValueMemoryFactory(), new NullBackend(''), new NullLockBackend());
$test_data = [
'hash1' => new Checkpoint('hash1', 'One', 1701539510, NULL),
'hash2' => new Checkpoint('hash2', 'Two', 1701539520, 'hash1'),
'hash3' => new Checkpoint('hash3', 'Three', 1701539530, 'hash2'),
];
$state->set(self::CHECKPOINT_KEY, $test_data);
$time = $this->prophesize(TimeInterface::class);
$checkpoints = new LinearHistory($state, $time->reveal());
$this->assertCount(3, $checkpoints);
$this->assertSame('hash3', $checkpoints->getActiveCheckpoint()?->id);
$checkpoints->delete('hash2');
$this->assertCount(1, $checkpoints);
$this->assertSame('hash3', $checkpoints->getActiveCheckpoint()?->id);
// We can call getParents without an exception.
foreach ($checkpoints->getParents('hash3') as $parent) {
$this->fail('Checkpoint should not have a parent ' . $parent->id);
}
$this->assertNull($checkpoints->getActiveCheckpoint()->parent);
// Now if we delete also the third one, the active one will be null.
$checkpoints->delete('hash3');
$this->assertCount(0, $checkpoints);
$this->assertNull($checkpoints->getActiveCheckpoint());
$this->assertNull($state->get(self::CHECKPOINT_KEY));
}
/**
* @covers ::delete
*/
public function testDeleteException() : void {
$state = $this->prophesize(StateInterface::class);
$state->get(self::CHECKPOINT_KEY, [])
->willReturn([]);
$time = $this->prophesize(TimeInterface::class);
$checkpoints = new LinearHistory($state->reveal(), $time->reveal());
$this->expectException(UnknownCheckpointException::class);
$this->expectExceptionMessage('Cannot delete a checkpoint with the ID "foo" as it does not exist');
$checkpoints->delete('foo');
}
/**
* @covers ::getParents
*/
public function testGetParents() : void {
$state = $this->prophesize(StateInterface::class);
$test_data = [
'hash1' => new Checkpoint('hash1', 'One', 1701539510, NULL),
'hash2' => new Checkpoint('hash2', 'Two', 1701539520, 'hash1'),
'hash3' => new Checkpoint('hash3', 'Three', 1701539530, 'hash2'),
];
$state->get(self::CHECKPOINT_KEY, [])
->willReturn($test_data);
$time = $this->prophesize(TimeInterface::class);
$checkpoints = new LinearHistory($state->reveal(), $time->reveal());
$this->assertSame([
'hash2' => $test_data['hash2'],
'hash1' => $test_data['hash1'],
], iterator_to_array($checkpoints->getParents('hash3')));
$this->assertSame([
'hash1' => $test_data['hash1'],
], iterator_to_array($checkpoints->getParents('hash2')));
$this->assertSame([], iterator_to_array($checkpoints->getParents('hash1')));
}
/**
* @covers ::getParents
*/
public function testGetParentsException() : void {
$state = $this->prophesize(StateInterface::class);
$test_data = [
'hash1' => new Checkpoint('hash1', 'One', 1701539510, NULL),
'hash2' => new Checkpoint('hash2', 'Two', 1701539520, 'hash1'),
];
$state->get(self::CHECKPOINT_KEY, [])
->willReturn($test_data);
$time = $this->prophesize(TimeInterface::class);
$checkpoints = new LinearHistory($state->reveal(), $time->reveal());
$this->expectException(UnknownCheckpointException::class);
$this->expectExceptionMessage('The checkpoint "hash3" does not exist');
iterator_to_array($checkpoints->getParents('hash3'));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
LinearHistoryTest::CHECKPOINT_KEY | private | constant | The key used store of all the checkpoint names in state. | |
LinearHistoryTest::testAdd | public | function | @covers ::add[[api-linebreak]] @covers ::count[[api-linebreak]] @covers ::getActiveCheckpoint[[api-linebreak]] @covers \Drupal\Core\Config\Checkpoint\Checkpoint[[api-linebreak]] |
|
LinearHistoryTest::testAddException | public | function | @covers ::add[[api-linebreak]] | |
LinearHistoryTest::testDelete | public | function | @covers ::delete[[api-linebreak]] | |
LinearHistoryTest::testDeleteAll | public | function | @covers ::delete[[api-linebreak]] | |
LinearHistoryTest::testDeleteException | public | function | @covers ::delete[[api-linebreak]] | |
LinearHistoryTest::testGetParents | public | function | @covers ::getParents[[api-linebreak]] | |
LinearHistoryTest::testGetParentsException | public | function | @covers ::getParents[[api-linebreak]] | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
UnitTestCase::$root | protected | property | The app root. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
UnitTestCase::setUp | protected | function | 360 | |
UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.