StorageTestBase.php 7.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains Drupal\system\Tests\KeyValueStore\StorageTestBase.
 */

namespace Drupal\system\Tests\KeyValueStore;

10
use Drupal\simpletest\KernelTestBase;
11
12
13
14

/**
 * Base class for testing key-value storages.
 */
15
abstract class StorageTestBase extends KernelTestBase {
16

17
18
19
20
21
22
23
  /**
   * An array of random stdClass objects.
   *
   * @var array
   */
  protected $objects = array();

24
25
26
27
28
29
30
  /**
   * An array of data collection labels.
   *
   * @var array
   */
  protected $collections = array();

31
32
33
34
35
36
37
  /**
   * Whether we are using an expirable key/value store.
   *
   * @var boolean
   */
  protected $factory = 'keyvalue';

38
39
40
  protected function setUp() {
    parent::setUp();

41
42
    // Define two data collections,
    $this->collections = array(0 => 'zero', 1 => 'one');
43
44
45
46
47

    // Create several objects for testing.
    for ($i = 0; $i <= 5; $i++) {
      $this->objects[$i] = $this->randomObject();
    }
48
49
50
51
52
53
  }

  /**
   * Tests CRUD operations.
   */
  public function testCRUD() {
54
    $stores = $this->createStorage();
55
    // Verify that each store returns its own collection name.
56
57
    $this->assertIdentical($stores[0]->getCollectionName(), $this->collections[0]);
    $this->assertIdentical($stores[1]->getCollectionName(), $this->collections[1]);
58
59

    // Verify that an item can be stored.
60
    $stores[0]->set('foo', $this->objects[0]);
61
    $this->assertTrue($stores[0]->has('foo'));
62
    $this->assertIdenticalObject($this->objects[0], $stores[0]->get('foo'));
63
    // Verify that the other collection is not affected.
64
    $this->assertFalse($stores[1]->has('foo'));
65
    $this->assertFalse($stores[1]->get('foo'));
66
67

    // Verify that an item can be updated.
68
69
    $stores[0]->set('foo', $this->objects[1]);
    $this->assertIdenticalObject($this->objects[1], $stores[0]->get('foo'));
70
    // Verify that the other collection is still not affected.
71
    $this->assertFalse($stores[1]->get('foo'));
72
73

    // Verify that a collection/name pair is unique.
74
75
76
    $stores[1]->set('foo', $this->objects[2]);
    $this->assertIdenticalObject($this->objects[1], $stores[0]->get('foo'));
    $this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
77
78

    // Verify that an item can be deleted.
79
    $stores[0]->delete('foo');
80
    $this->assertFalse($stores[0]->has('foo'));
81
    $this->assertFalse($stores[0]->get('foo'));
82
83

    // Verify that the other collection is not affected.
84
    $this->assertTrue($stores[1]->has('foo'));
85
86
87
    $this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
    $stores[1]->delete('foo');
    $this->assertFalse($stores[1]->get('foo'));
88
89
90

    // Verify that multiple items can be stored.
    $values = array(
91
92
      'foo' => $this->objects[3],
      'bar' => $this->objects[4],
93
    );
94
    $stores[0]->setMultiple($values);
95
96

    // Verify that multiple items can be retrieved.
97
    $result = $stores[0]->getMultiple(array('foo', 'bar'));
98
99
100
    foreach ($values as $j => $value) {
      $this->assertIdenticalObject($value, $result[$j]);
    }
101
102

    // Verify that the other collection was not affected.
103
104
    $this->assertFalse($stores[1]->get('foo'));
    $this->assertFalse($stores[1]->get('bar'));
105
106
107

    // Verify that all items in a collection can be retrieved.
    // Ensure that an item with the same name exists in the other collection.
108
109
    $stores[1]->set('foo', $this->objects[5]);
    $result = $stores[0]->getAll();
110
111
112
113
114
115
    // Not using assertIdentical(), since the order is not defined for getAll().
    $this->assertEqual(count($result), count($values));
    foreach ($result as $key => $value) {
      $this->assertEqual($values[$key], $value);
    }
    // Verify that all items in the other collection are different.
116
    $result = $stores[1]->getAll();
117
    $this->assertEqual($result, array('foo' => $this->objects[5]));
118
119

    // Verify that multiple items can be deleted.
120
121
122
123
    $stores[0]->deleteMultiple(array_keys($values));
    $this->assertFalse($stores[0]->get('foo'));
    $this->assertFalse($stores[0]->get('bar'));
    $this->assertFalse($stores[0]->getMultiple(array('foo', 'bar')));
124
125
    // Verify that deleting no items does not cause an error.
    $stores[0]->deleteMultiple(array());
126
    // Verify that the item in the other collection still exists.
127
    $this->assertIdenticalObject($this->objects[5], $stores[1]->get('foo'));
128

129
130
131
132
133
134
  }

  /**
   * Tests expected behavior for non-existing keys.
   */
  public function testNonExistingKeys() {
135

136
137
    $stores = $this->createStorage();

138
    // Verify that a non-existing key returns NULL as value.
139
    $this->assertNull($stores[0]->get('foo'));
140

141
142
143
    // Verify that a non-existing key with a default returns the default.
    $this->assertIdentical($stores[0]->get('foo', 'bar'), 'bar');

144
    // Verify that a FALSE value can be stored.
145
146
    $stores[0]->set('foo', FALSE);
    $this->assertIdentical($stores[0]->get('foo'), FALSE);
147
148

    // Verify that a deleted key returns NULL as value.
149
150
    $stores[0]->delete('foo');
    $this->assertNull($stores[0]->get('foo'));
151
152

    // Verify that a non-existing key is not returned when getting multiple keys.
153
154
    $stores[0]->set('bar', 'baz');
    $values = $stores[0]->getMultiple(array('foo', 'bar'));
155
156
    $this->assertFalse(isset($values['foo']), "Key 'foo' not found.");
    $this->assertIdentical($values['bar'], 'baz');
157
  }
158
159
160
161
162

  /**
   * Tests the setIfNotExists() method.
   */
  public function testSetIfNotExists() {
163
164
    $stores = $this->createStorage();

165
    $key = $this->randomMachineName();
166
167
168
169
    // Test that setIfNotExists() succeeds only the first time.
    for ($i = 0; $i <= 1; $i++) {
      // setIfNotExists() should be TRUE the first time (when $i is 0) and
      // FALSE the second time (when $i is 1).
170
171
      $this->assertEqual(!$i, $stores[0]->setIfNotExists($key, $this->objects[$i]));
      $this->assertIdenticalObject($this->objects[0], $stores[0]->get($key));
172
      // Verify that the other collection is not affected.
173
      $this->assertFalse($stores[1]->get($key));
174
175
176
    }

    // Remove the item and try to set it again.
177
178
    $stores[0]->delete($key);
    $stores[0]->setIfNotExists($key, $this->objects[1]);
179
    // This time it should succeed.
180
    $this->assertIdenticalObject($this->objects[1], $stores[0]->get($key));
181
    // Verify that the other collection is still not affected.
182
183
184
    $this->assertFalse($stores[1]->get($key));
  }

185
186
187
188
189
190
191
192
193
194
195
196
197
198
  /**
   * Tests the rename operation.
   */
  public function testRename() {
    $stores = $this->createStorage();
    $store = $stores[0];

    $store->set('old', 'thing');
    $this->assertIdentical($store->get('old'), 'thing');
    $store->rename('old', 'new');
    $this->assertIdentical($store->get('new'), 'thing');
    $this->assertNull($store->get('old'));
  }

199
200
201
202
203
204
205
206
207
  /**
   * Creates storage objects for each collection defined for this class.
   *
   * Storing the storage objects in a class member variable causes a fatal
   * exception in DatabaseStorageExpirableTest, because in that situation
   * garbage collection is not triggered until the test class itself is
   * destructed, after tearDown() has deleted the database tables. Instead,
   * create the storage objects locally in each test using this method.
   *
208
209
   * @see \Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirable
   * @see \Drupal\Core\KeyValueStore\DatabaseStorageExpirable::garbageCollection()
210
211
212
213
   */
  protected function createStorage() {
    $stores = array();
    foreach ($this->collections as $i => $collection) {
214
      $stores[$i] = $this->container->get($this->factory)->get($collection);
215
216
217
    }

    return $stores;
218
219
  }

220
}