class LinearHistoryTest
Same name in other branches
- 10 core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php \Drupal\Tests\Core\Config\Checkpoint\LinearHistoryTest
@coversDefaultClass \Drupal\Core\Config\Checkpoint\LinearHistory @group Config
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Config\Checkpoint\LinearHistoryTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LinearHistoryTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Config/ Checkpoint/ LinearHistoryTest.php, line 21
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 {
$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);
unset($test_data['hash1'], $test_data['hash2']);
$state->set(self::CHECKPOINT_KEY, $test_data)
->willReturn();
$time = $this->prophesize(TimeInterface::class);
$checkpoints = new LinearHistory($state->reveal(), $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);
}
/**
* @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::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
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 @covers ::count @covers ::getActiveCheckpoint @covers \Drupal\Core\Config\Checkpoint\Checkpoint |
|
LinearHistoryTest::testAddException | public | function | @covers ::add | |
LinearHistoryTest::testDelete | public | function | @covers ::delete | |
LinearHistoryTest::testDeleteAll | public | function | @covers ::delete | |
LinearHistoryTest::testDeleteException | public | function | @covers ::delete | |
LinearHistoryTest::testGetParents | public | function | @covers ::getParents | |
LinearHistoryTest::testGetParentsException | public | function | @covers ::getParents | |
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::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |
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::setUp | protected | function | 367 | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.