function KeyValueEntityStorageTest::testSaveInsert

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php \Drupal\Tests\Core\Entity\KeyValueStore\KeyValueEntityStorageTest::testSaveInsert()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php \Drupal\Tests\Core\Entity\KeyValueStore\KeyValueEntityStorageTest::testSaveInsert()
  3. 11.x core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php \Drupal\Tests\Core\Entity\KeyValueStore\KeyValueEntityStorageTest::testSaveInsert()

@covers ::save @covers ::doSave

@depends testCreate

Parameters

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

Return value

\Drupal\Core\Entity\EntityInterface

File

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

Class

KeyValueEntityStorageTest
@coversDefaultClass \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage @group Entity

Namespace

Drupal\Tests\Core\Entity\KeyValueStore

Code

public function testSaveInsert(EntityInterface $entity) {
    $this->setUpKeyValueEntityStorage();
    $expected = [
        'id' => 'foo',
    ];
    $this->keyValueStore
        ->expects($this->exactly(2))
        ->method('has')
        ->with('foo')
        ->willReturn(FALSE);
    $this->keyValueStore
        ->expects($this->never())
        ->method('getMultiple');
    $this->keyValueStore
        ->expects($this->never())
        ->method('delete');
    $entity->expects($this->atLeastOnce())
        ->method('toArray')
        ->willReturn($expected);
    $hooks = [
        'test_entity_type_presave',
        'entity_presave',
        'test_entity_type_insert',
        'entity_insert',
    ];
    $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_NEW, $return);
    return $entity;
}

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