View source
<?php
namespace Drupal\supercache\Tests\Generic\KeyValue;
use Drupal\Component\Utility\Unicode;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Drupal\Core\Site\Settings;
abstract class KeyValueTests extends KernelTestBase {
protected $factory;
public function testKeyValue() {
$col1 = [
'array' => [
'B' => 23,
],
'string' => 'asdgsd',
'object' => (object) [
'property1' => 'value1',
],
'float' => 7.0E-10,
'int' => 58,
];
$col2 = [
'array' => [
'B' => 65,
],
'string' => 'this is a string',
'object' => (object) [
'propertyA' => 'valueA',
],
'float' => 3.0E-10,
'int' => 21,
];
$storeA = $this->factory
->get('col1');
$storeB = $this->factory
->get('col2');
$storeA
->deleteAll();
$storeB
->deleteAll();
$storeA
->setMultiple($col1);
$this
->assertEquals(count($col1), count($storeA
->getAll()), 'Number of items match.');
$this
->assertEquals($col1, $storeA
->getAll(), 'Elements are properly stored and retrieved.');
$this
->assertFalse($storeA
->setIfNotExists('array', [
'asd',
]));
$this
->assertEquals($col1['array'], $storeA
->get('array'));
$this
->assertEqual($storeA
->get('does not exist', 'default'), 'default', 'Default values working');
$storeA
->delete('array');
$this
->assertEquals(count($col1) - 1, count($storeA
->getAll()), 'Item deleted.');
$this
->assertFalse($storeA
->get('array'));
$storeA
->set('Hi', 'value');
$this
->assertEquals('value', $storeA
->get('Hi'), 'Setting a value works.');
$storeA
->deleteAll();
$this
->assertEquals(0, count($storeA
->getAll()), 'Items removed.');
$storeA
->setMultiple($col1);
$this
->assertEquals(count($col1), count($storeA
->getAll()), 'Number of items match.');
$storeA
->deleteMultiple(array_keys($col1));
$this
->assertEquals(0, count($storeA
->getAll()), 'Items removed.');
$this
->assertEquals(0, count($storeB
->getAll()), 'Unused collection is empty.');
$this
->assertEquals('col1', $storeA
->getCollectionName(), 'Collection name matches.');
$this
->assertEquals('col2', $storeB
->getCollectionName(), 'Collection name matches.');
$storeA
->setMultiple($col1);
$storeB
->setMultiple($col2);
$storeA = $this->factory
->get('col1');
$storeB = $this->factory
->get('col2');
$this
->assertEquals($col1, $storeA
->getAll(), 'Elements are properly stored and retrieved.');
$this
->assertEquals($col2, $storeB
->getAll(), 'Elements are properly stored and retrieved.');
try {
$storeA
->rename('array', 'string');
$this
->fail("Trying to rename to a key that exists should throw an exception.");
} catch (\Exception $e) {
}
$storeA
->delete('string');
$storeA
->rename('array', 'string');
$this
->assertEquals($col1['array'], $storeA
->get('string'), 'Rename works.');
$this
->assertFalse($storeA
->has('array'), 'Rename removed old key.');
}
}