Skip to content
Snippets Groups Projects
Commit 5b949d3c authored by Benji Fisher's avatar Benji Fisher Committed by Lucas Hedding
Browse files

Issue #3218356: Process plugin: service

parent 158c8340
No related branches found
No related tags found
1 merge request!3Issue #3218356: Process plugin: service
<?php
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\migrate\process\Callback;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a plugin to use a callable from a service class.
*
* Example:
*
* @code
* process:
* filemime:
* plugin: service
* service: file.mime_type.guesser
* method: guessMimeType
* source: filename
* @endcode
*
* All options for the callback plugin can be used, except for 'callable',
* which will be ignored.
*
* @see \Drupal\migrate\Plugin\migrate\process\Callback
*
* @MigrateProcessPlugin(
* id = "service"
* )
*/
class Service extends Callback implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
if (!isset($configuration['service'])) {
throw new \InvalidArgumentException('The "service" must be set.');
}
if (!isset($configuration['method'])) {
throw new \InvalidArgumentException('The "method" must be set.');
}
if (!$container->has($configuration['service'])) {
throw new \InvalidArgumentException(sprintf('You have requested the non-existent service "%s".', $configuration['service']));
}
$service = $container->get($configuration['service']);
if (!method_exists($service, $configuration['method'])) {
throw new \InvalidArgumentException(sprintf('The "%s" service has no method "%s".', $configuration['service'], $configuration['method']));
}
$configuration['callable'] = [$service, $configuration['method']];
return new static($configuration, $plugin_id, $plugin_definition);
}
}
<?php
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Tests the service plugin.
*
* @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\Service
* @group migrate_plus
*/
class ServiceTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'migrate',
'migrate_plus',
'system',
];
/**
* The process plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
*/
protected $pluginManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->pluginManager = $this->container->get('plugin.manager.migrate.process');
}
/**
* Tests using a service.
*
* @covers ::create
*/
public function testValidConfig(): void {
/** @var \Drupal\migrate\MigrateExecutableInterface $executable */
$executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
$row = new Row([], []);
$configuration = [
'service' => 'email.validator',
'method' => 'isValid',
];
/** @var \Drupal\migrate_plus\Plugin\migrate\process\Service $service */
$service = $this->pluginManager->createInstance('service', $configuration);
// Test a valid email address.
$value = 'drupal@example.com';
$bool = $service->transform($value, $executable, $row, 'destination_property');
$this->assertEquals(TRUE, $bool);
// Test an invalid email address.
$value = 'drupal_example.com';
$bool = $service->transform($value, $executable, $row, 'destination_property');
$this->assertEquals(FALSE, $bool);
}
/**
* Tests configuration validation.
*
* @param string[] $configuration
* The configuration for the service plugin. The expected keys are 'service'
* and 'method'.
* @param string $message
* The expected exception message.
*
* @covers ::create
*
* @dataProvider providerConfig
*/
public function testInvalidConfig(array $configuration, $message): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($message);
$this->pluginManager->createInstance('service', $configuration);
}
/**
* Data provider for testInvalidConfig.
*/
public function providerConfig() {
return [
'missing service name' => [
'configuration' => [],
'message' => 'The "service" must be set.',
],
'missing method name' => [
'configuration' => ['service' => 'email.validator'],
'message' => 'The "method" must be set.',
],
'invalid service name' => [
'configuration' => [
'service' => 'no shirt no shoes no service',
'method' => 'isValid',
],
'message' => 'You have requested the non-existent service "no shirt no shoes no service".',
],
'invalid method name' => [
'configuration' => [
'service' => 'email.validator',
'method' => 'noSuchMethod',
],
'message' => 'The "email.validator" service has no method "noSuchMethod".',
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment