function SqlContentEntityStorageTest::testOnEntityTypeCreate

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

Tests ContentEntityDatabaseStorage::onEntityTypeCreate().

@covers ::__construct @covers ::onEntityTypeCreate @covers ::getTableMapping

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php, line 365

Class

SqlContentEntityStorageTest
@coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorage @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testOnEntityTypeCreate() : void {
    $columns = [
        'value' => [
            'type' => 'int',
        ],
    ];
    $this->fieldDefinitions = $this->mockFieldDefinitions([
        'id',
    ]);
    $this->fieldDefinitions['id']
        ->expects($this->any())
        ->method('getColumns')
        ->willReturn($columns);
    $this->fieldDefinitions['id']
        ->expects($this->once())
        ->method('getSchema')
        ->willReturn([
        'columns' => $columns,
    ]);
    $this->entityType
        ->expects($this->once())
        ->method('getKeys')
        ->willReturn([
        'id' => 'id',
    ]);
    $this->entityType
        ->expects($this->any())
        ->method('hasKey')
        ->willReturnMap([
        // SqlContentEntityStorageSchema::initializeBaseTable()
[
            'revision',
            FALSE,
        ],
        [
            'id',
            TRUE,
        ],
    ]);
    $this->entityType
        ->expects($this->any())
        ->method('getKey')
        ->willReturnMap([
        // EntityStorageBase::__construct()
[
            'id',
            'id',
        ],
        // ContentEntityStorageBase::__construct()
[
            'uuid',
            NULL,
        ],
        [
            'bundle',
            NULL,
        ],
        // SqlContentEntityStorageSchema::initializeBaseTable()
[
            'id' => 'id',
        ],
        [
            'id' => 'id',
        ],
    ]);
    $this->setUpEntityStorage();
    $expected = [
        'description' => 'The base table for entity_test entities.',
        'fields' => [
            'id' => [
                'type' => 'serial',
                'not null' => TRUE,
            ],
        ],
        'primary key' => [
            'id',
        ],
        'unique keys' => [],
        'indexes' => [],
        'foreign keys' => [],
    ];
    $schema_handler = $this->getMockBuilder('Drupal\\Core\\Database\\Schema')
        ->disableOriginalConstructor()
        ->getMock();
    $schema_handler->expects($this->any())
        ->method('createTable')
        ->with($this->equalTo('entity_test'), $this->equalTo($expected));
    $this->connection
        ->expects($this->once())
        ->method('schema')
        ->willReturn($schema_handler);
    $storage = $this->getMockBuilder('Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage')
        ->setConstructorArgs([
        $this->entityType,
        $this->connection,
        $this->entityFieldManager
            ->reveal(),
        $this->cache,
        $this->languageManager,
        new MemoryCache(new Time()),
        $this->entityTypeBundleInfo,
        $this->entityTypeManager
            ->reveal(),
    ])
        ->onlyMethods([
        'getStorageSchema',
    ])
        ->getMock();
    $key_value = $this->createMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreInterface');
    $schema_handler = $this->getMockBuilder('Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorageSchema')
        ->setConstructorArgs([
        $this->entityTypeManager
            ->reveal(),
        $this->entityType,
        $storage,
        $this->connection,
        $this->entityFieldManager
            ->reveal(),
    ])
        ->onlyMethods([
        'installedStorageSchema',
        'createSharedTableSchema',
    ])
        ->getMock();
    $schema_handler->expects($this->any())
        ->method('installedStorageSchema')
        ->willReturn($key_value);
    $storage->expects($this->any())
        ->method('getStorageSchema')
        ->willReturn($schema_handler);
    $storage->onEntityTypeCreate($this->entityType);
}

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