You are here

public function StorageTestBase::_testSetIfNotExists in Service Container 7.2

Same name and namespace in other branches
  1. 7 lib/Drupal/service_container/Tests/KeyValue/StorageTestBase.php \Drupal\service_container\Tests\KeyValue\StorageTestBase::_testSetIfNotExists()

Tests the setIfNotExists() method.

1 call to StorageTestBase::_testSetIfNotExists()
StorageTestBase::testKeyValueStore in lib/Drupal/service_container/Tests/KeyValue/StorageTestBase.php

File

lib/Drupal/service_container/Tests/KeyValue/StorageTestBase.php, line 224
Contains Drupal\service_container\Tests\KeyValue\StorageTestBase.

Class

StorageTestBase
Base class for testing key-value storages.

Namespace

Drupal\service_container\Tests\KeyValue

Code

public function _testSetIfNotExists() {
  $stores = $this
    ->createStorage();
  $key = $this
    ->randomName();

  // 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).
    $this
      ->assertEqual(!$i, $stores[0]
      ->setIfNotExists($key, $this->objects[$i]));
    $this
      ->assertIdenticalObject($this->objects[0], $stores[0]
      ->get($key));

    // Verify that the other collection is not affected.
    $this
      ->assertFalse($stores[1]
      ->get($key));
  }

  // Remove the item and try to set it again.
  $stores[0]
    ->delete($key);
  $stores[0]
    ->setIfNotExists($key, $this->objects[1]);

  // This time it should succeed.
  $this
    ->assertIdenticalObject($this->objects[1], $stores[0]
    ->get($key));

  // Verify that the other collection is still not affected.
  $this
    ->assertFalse($stores[1]
    ->get($key));
}