setIfNotExists example


  public function setIfOwner($key$value) {
    if ($this->setIfNotExists($key$value)) {
      return TRUE;
    }

    if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) {
      $this->set($key$value);
      return TRUE;
    }

    return FALSE;
  }

  
      // triggered until the test class itself is destructed, after tearDown()       // has deleted the database tables. Store the objects locally instead.       /** @var \Drupal\Core\TempStore\SharedTempStore[] $stores */
      $stores[$i] = $factory->get($collection$users[$i]);
    }

    $key = $this->randomMachineName();
    // 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->assertEquals(!$i$stores[0]->setIfNotExists($key$objects[$i]));
      $metadata = $stores[0]->getMetadata($key);
      $this->assertEquals($users[0]$metadata->getOwnerId());
      $this->assertEquals($objects[0]$stores[0]->get($key));
      // Another user should get the same result.       $metadata = $stores[1]->getMetadata($key);
      $this->assertEquals($users[0]$metadata->getOwnerId());
      $this->assertEquals($objects[0]$stores[1]->get($key));
    }

    // Remove the item and try to set it again.     $stores[0]->delete($key);
    
/** * Tests the setIfNotExists() method. */
  public function testSetIfNotExists() {
    $stores = $this->createStorage();

    $key = $this->randomMachineName();
    // 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->assertEquals(!$i$stores[0]->setIfNotExists($key$this->objects[$i]));
      $this->assertEquals($this->objects[0]$stores[0]->get($key));
      // Verify that the other collection is not affected.       $this->assertNull($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->assertEquals($this->objects[1]$stores[0]->get($key));
    // Verify that the other collection is still not affected.
/** * Tests the setIfNotExists() methods. * * @covers ::setIfNotExists */
  public function testSetIfNotExists() {
    $this->keyValue->expects($this->once())
      ->method('setWithExpireIfNotExists')
      ->with('test', $this->ownObject, 604800)
      ->willReturn(TRUE);

    $this->assertTrue($this->tempStore->setIfNotExists('test', 'test_data'));
  }

  /** * Tests the setIfOwner() method when no key exists. * * @covers ::setIfOwner */
  public function testSetIfOwnerWhenNotExists() {
    $this->keyValue->expects($this->once())
      ->method('setWithExpireIfNotExists')
      ->willReturn(TRUE);

    
Home | Imprint | This part of the site doesn't use cookies.