3 namespace Drupal\KernelTests\Core\KeyValueStore;
5 use Drupal\KernelTests\KernelTestBase;
8 * Base class for testing key-value storages.
10 abstract class StorageTestBase extends KernelTestBase {
13 * An array of random stdClass objects.
17 protected $objects = [];
20 * An array of data collection labels.
24 protected $collections = [];
27 * Whether we are using an expirable key/value store.
31 protected $factory = 'keyvalue';
33 protected function setUp() {
36 // Define two data collections,
37 $this->collections = [0 => 'zero', 1 => 'one'];
39 // Create several objects for testing.
40 for ($i = 0; $i <= 5; $i++) {
41 $this->objects[$i] = $this->randomObject();
46 * Tests CRUD operations.
48 public function testCRUD() {
49 $stores = $this->createStorage();
50 // Verify that each store returns its own collection name.
51 $this->assertIdentical($stores[0]->getCollectionName(), $this->collections[0]);
52 $this->assertIdentical($stores[1]->getCollectionName(), $this->collections[1]);
54 // Verify that an item can be stored.
55 $stores[0]->set('foo', $this->objects[0]);
56 $this->assertTrue($stores[0]->has('foo'));
57 $this->assertIdenticalObject($this->objects[0], $stores[0]->get('foo'));
58 // Verify that the other collection is not affected.
59 $this->assertFalse($stores[1]->has('foo'));
60 $this->assertFalse($stores[1]->get('foo'));
62 // Verify that an item can be updated.
63 $stores[0]->set('foo', $this->objects[1]);
64 $this->assertIdenticalObject($this->objects[1], $stores[0]->get('foo'));
65 // Verify that the other collection is still not affected.
66 $this->assertFalse($stores[1]->get('foo'));
68 // Verify that a collection/name pair is unique.
69 $stores[1]->set('foo', $this->objects[2]);
70 $this->assertIdenticalObject($this->objects[1], $stores[0]->get('foo'));
71 $this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
73 // Verify that an item can be deleted.
74 $stores[0]->delete('foo');
75 $this->assertFalse($stores[0]->has('foo'));
76 $this->assertFalse($stores[0]->get('foo'));
78 // Verify that the other collection is not affected.
79 $this->assertTrue($stores[1]->has('foo'));
80 $this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
81 $stores[1]->delete('foo');
82 $this->assertFalse($stores[1]->get('foo'));
84 // Verify that multiple items can be stored.
86 'foo' => $this->objects[3],
87 'bar' => $this->objects[4],
89 $stores[0]->setMultiple($values);
91 // Verify that multiple items can be retrieved.
92 $result = $stores[0]->getMultiple(['foo', 'bar']);
93 foreach ($values as $j => $value) {
94 $this->assertIdenticalObject($value, $result[$j]);
97 // Verify that the other collection was not affected.
98 $this->assertFalse($stores[1]->get('foo'));
99 $this->assertFalse($stores[1]->get('bar'));
101 // Verify that all items in a collection can be retrieved.
102 // Ensure that an item with the same name exists in the other collection.
103 $stores[1]->set('foo', $this->objects[5]);
104 $result = $stores[0]->getAll();
105 // Not using assertSame(), since the order is not defined for getAll().
106 $this->assertEqual(count($result), count($values));
107 foreach ($result as $key => $value) {
108 $this->assertEqual($values[$key], $value);
110 // Verify that all items in the other collection are different.
111 $result = $stores[1]->getAll();
112 $this->assertEqual($result, ['foo' => $this->objects[5]]);
114 // Verify that multiple items can be deleted.
115 $stores[0]->deleteMultiple(array_keys($values));
116 $this->assertFalse($stores[0]->get('foo'));
117 $this->assertFalse($stores[0]->get('bar'));
118 $this->assertFalse($stores[0]->getMultiple(['foo', 'bar']));
119 // Verify that deleting no items does not cause an error.
120 $stores[0]->deleteMultiple([]);
121 // Verify that the item in the other collection still exists.
122 $this->assertIdenticalObject($this->objects[5], $stores[1]->get('foo'));
127 * Tests expected behavior for non-existing keys.
129 public function testNonExistingKeys() {
131 $stores = $this->createStorage();
133 // Verify that a non-existing key returns NULL as value.
134 $this->assertNull($stores[0]->get('foo'));
136 // Verify that a non-existing key with a default returns the default.
137 $this->assertIdentical($stores[0]->get('foo', 'bar'), 'bar');
139 // Verify that a FALSE value can be stored.
140 $stores[0]->set('foo', FALSE);
141 $this->assertIdentical($stores[0]->get('foo'), FALSE);
143 // Verify that a deleted key returns NULL as value.
144 $stores[0]->delete('foo');
145 $this->assertNull($stores[0]->get('foo'));
147 // Verify that a non-existing key is not returned when getting multiple keys.
148 $stores[0]->set('bar', 'baz');
149 $values = $stores[0]->getMultiple(['foo', 'bar']);
150 $this->assertFalse(isset($values['foo']), "Key 'foo' not found.");
151 $this->assertIdentical($values['bar'], 'baz');
155 * Tests the setIfNotExists() method.
157 public function testSetIfNotExists() {
158 $stores = $this->createStorage();
160 $key = $this->randomMachineName();
161 // Test that setIfNotExists() succeeds only the first time.
162 for ($i = 0; $i <= 1; $i++) {
163 // setIfNotExists() should be TRUE the first time (when $i is 0) and
164 // FALSE the second time (when $i is 1).
165 $this->assertEqual(!$i, $stores[0]->setIfNotExists($key, $this->objects[$i]));
166 $this->assertIdenticalObject($this->objects[0], $stores[0]->get($key));
167 // Verify that the other collection is not affected.
168 $this->assertFalse($stores[1]->get($key));
171 // Remove the item and try to set it again.
172 $stores[0]->delete($key);
173 $stores[0]->setIfNotExists($key, $this->objects[1]);
174 // This time it should succeed.
175 $this->assertIdenticalObject($this->objects[1], $stores[0]->get($key));
176 // Verify that the other collection is still not affected.
177 $this->assertFalse($stores[1]->get($key));
181 * Tests the rename operation.
183 public function testRename() {
184 $stores = $this->createStorage();
187 $store->set('old', 'thing');
188 $this->assertIdentical($store->get('old'), 'thing');
189 $store->rename('old', 'new');
190 $this->assertIdentical($store->get('new'), 'thing');
191 $this->assertNull($store->get('old'));
195 * Creates storage objects for each collection defined for this class.
197 * Storing the storage objects in a class member variable causes a fatal
198 * exception in DatabaseStorageExpirableTest, because in that situation
199 * garbage collection is not triggered until the test class itself is
200 * destructed, after tearDown() has deleted the database tables. Instead,
201 * create the storage objects locally in each test using this method.
203 * @see \Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirable
204 * @see \Drupal\Core\KeyValueStore\DatabaseStorageExpirable::garbageCollection()
206 protected function createStorage() {
208 foreach ($this->collections as $i => $collection) {
209 $stores[$i] = $this->container->get($this->factory)->get($collection);