function KeyValueEntityStorageTest::testSaveUpdate

Tests save update.

@legacy-covers ::save @legacy-covers ::doSave

Attributes

#[\PHPUnit\Framework\Attributes\Depends('testSaveInsert')]

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

File

core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php, line 290

Class

KeyValueEntityStorageTest
Tests Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage.

Namespace

Drupal\Tests\Core\Entity\KeyValueStore

Code

public function testSaveUpdate(EntityInterface $entity) : void {
  $this->entityType
    ->expects($this->once())
    ->method('getClass')
    ->willReturn(get_class($entity));
  $this->setUpKeyValueEntityStorage();
  $expected = [
    'id' => 'foo',
  ];
  $this->keyValueStore
    ->expects($this->exactly(2))
    ->method('has')
    ->with('foo')
    ->willReturn(TRUE);
  $this->keyValueStore
    ->expects($this->once())
    ->method('getMultiple')
    ->with([
    'foo',
  ])
    ->willReturn([
    [
      'id' => 'foo',
    ],
  ]);
  $this->keyValueStore
    ->expects($this->never())
    ->method('delete');
  $hooks = [
    'test_entity_type_presave',
    'entity_presave',
    'test_entity_type_update',
    'entity_update',
  ];
  $this->moduleHandler
    ->expects($this->exactly(count($hooks)))
    ->method('invokeAll')
    ->with($this->callback(function (string $hook) use (&$hooks) : bool {
    return array_shift($hooks) === $hook;
  }));
  $this->keyValueStore
    ->expects($this->once())
    ->method('set')
    ->with('foo', $expected);
  $return = $this->entityStorage
    ->save($entity);
  $this->assertSame(SAVED_UPDATED, $return);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.