You are here

public function KeyValueEntityStorageTest::testSaveInsert in Drupal 8

@covers ::save @covers ::doSave

@depends testCreate

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

Return value

\Drupal\Core\Entity\EntityInterface

File

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

Class

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

Namespace

Drupal\Tests\Core\Entity\KeyValueStore

Code

public function testSaveInsert(EntityInterface $entity) {
  $this->entityType
    ->expects($this
    ->once())
    ->method('getClass')
    ->will($this
    ->returnValue(get_class($entity)));
  $this
    ->setUpKeyValueEntityStorage();
  $expected = [
    'id' => 'foo',
  ];
  $this->keyValueStore
    ->expects($this
    ->exactly(2))
    ->method('has')
    ->with('foo')
    ->will($this
    ->returnValue(FALSE));
  $this->keyValueStore
    ->expects($this
    ->never())
    ->method('getMultiple');
  $this->keyValueStore
    ->expects($this
    ->never())
    ->method('delete');
  $entity
    ->expects($this
    ->atLeastOnce())
    ->method('toArray')
    ->will($this
    ->returnValue($expected));
  $this->moduleHandler
    ->expects($this
    ->at(0))
    ->method('invokeAll')
    ->with('test_entity_type_presave');
  $this->moduleHandler
    ->expects($this
    ->at(1))
    ->method('invokeAll')
    ->with('entity_presave');
  $this->moduleHandler
    ->expects($this
    ->at(2))
    ->method('invokeAll')
    ->with('test_entity_type_insert');
  $this->moduleHandler
    ->expects($this
    ->at(3))
    ->method('invokeAll')
    ->with('entity_insert');
  $this->keyValueStore
    ->expects($this
    ->once())
    ->method('set')
    ->with('foo', $expected);
  $return = $this->entityStorage
    ->save($entity);
  $this
    ->assertSame(SAVED_NEW, $return);
  return $entity;
}