1 cache.test CacheGetMultipleUnitTest::testCacheMultiple()

Test getMultiple().

File

core/modules/simpletest/tests/cache.test, line 166

Class

CacheGetMultipleUnitTest
Test getMultiple().

Code

function testCacheMultiple() {
  $item1 = $this->randomName(10);
  $item2 = $this->randomName(10);
  $cache = cache($this->default_bin);
  $cache->set('item1', $item1);
  $cache->set('item2', $item2);
  $this->assertTrue($this->checkCacheExists('item1', $item1), 'Item 1 is cached.');
  $this->assertTrue($this->checkCacheExists('item2', $item2), 'Item 2 is cached.');

  // Fetch both records from the database with getMultiple().
  $item_ids = array('item1', 'item2');
  $items = $cache->getMultiple($item_ids);
  $this->assertEqual($items['item1']->data, $item1, 'Item was returned from cache successfully.');
  $this->assertEqual($items['item2']->data, $item2, 'Item was returned from cache successfully.');

  // Remove one item from the cache.
  $cache->delete('item2');

  // Confirm that only one item is returned by getMultiple().
  $item_ids = array('item1', 'item2');
  $items = $cache->getMultiple($item_ids);
  $this->assertEqual($items['item1']->data, $item1, 'Item was returned from cache successfully.');
  $this->assertFalse(isset($items['item2']), 'Item was not returned from the cache.');
  $this->assertTrue(count($items) == 1, 'Only valid cache entries returned.');
}