Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
migrate_plus
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
migrate_plus
Commits
5b949d3c
Commit
5b949d3c
authored
3 years ago
by
Benji Fisher
Committed by
Lucas Hedding
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3218356
: Process plugin: service
parent
158c8340
No related branches found
No related tags found
1 merge request
!3
Issue #3218356: Process plugin: service
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/migrate/process/Service.php
+56
-0
56 additions, 0 deletions
src/Plugin/migrate/process/Service.php
tests/src/Kernel/Plugin/migrate/process/ServiceTest.php
+118
-0
118 additions, 0 deletions
tests/src/Kernel/Plugin/migrate/process/ServiceTest.php
with
174 additions
and
0 deletions
src/Plugin/migrate/process/Service.php
0 → 100644
+
56
−
0
View file @
5b949d3c
<?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
);
}
}
This diff is collapsed.
Click to expand it.
tests/src/Kernel/Plugin/migrate/process/ServiceTest.php
0 → 100644
+
118
−
0
View file @
5b949d3c
<?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".'
,
],
];
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment