external_entity-1.0.x-dev/tests/src/Concerns/ExternalEntityInstances.php
tests/src/Concerns/ExternalEntityInstances.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\external_entity\Concerns;
use Drupal\external_entity\Contracts\ExternalEntityConnectionInterface;
use Drupal\external_entity\Contracts\ExternalEntityConnectionTypeInterface;
use Drupal\external_entity\Contracts\ExternalEntityAuthenticationTypeInterface;
trait ExternalEntityInstances {
/**
* @return \Drupal\external_entity\Contracts\ExternalEntityConnectionInterface
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function createExternalEntityConnectionInstance(): ExternalEntityConnectionInterface {
return \Drupal::entityTypeManager()
->getStorage('external_entity_connection')
->create([
'id' => 'test_connection',
'label' => 'Test Connection',
'connection_type' => 'external_entity_server',
'connection_settings' => [
'server_domain' => 'https://example.com',
'resources' => ['node' => 'node'],
],
'authentication_type' => 'basic_authentication',
'authentication_settings' => [
'username' => 'admin',
'password' => 'password',
],
]);
}
/**
* @param string $plugin_id
* @param array $configurations
*
* @return \Drupal\external_entity\Contracts\ExternalEntityConnectionTypeInterface
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \PHPUnit\Framework\MockObject\Exception
*/
protected function createConnectionTypeInstance(
string $plugin_id,
array $configurations = []
): ExternalEntityConnectionTypeInterface {
/** @var \Drupal\external_entity\ConnectionTypeManager $connection_manager */
$connection_manager = $this->container->get('plugin.manager.external_entity.connection_type');
$instance = $connection_manager->createInstance($plugin_id, $configurations);
$connection = $this->createMock(ExternalEntityConnectionInterface::class);
$authentication_type = $this->createMock(ExternalEntityAuthenticationTypeInterface::class);
$authentication_type->method('alterRequestOptions')
->willReturnCallback(function(&$options) {
$options['user'] = 'admin';
$options['password'] = 'password';
});
$connection->method('createAuthenticationTypeInstance')->willReturn($authentication_type);
$instance->setConnection($connection);
return $instance;
}
}
