View source
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\process\Callback;
class CallbackTest extends MigrateProcessTestCase {
public function testCallback($callable) {
$configuration = [
'callable' => $callable,
];
$this->plugin = new Callback($configuration, 'map', []);
$value = $this->plugin
->transform('FooBar', $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertSame('foobar', $value);
}
public function providerCallback() {
return [
'function' => [
'strtolower',
],
'class method' => [
[
self::class,
'strtolower',
],
],
];
}
public function testCallbackArray($callable, $args, $result) {
$configuration = [
'callable' => $callable,
'unpack_source' => TRUE,
];
$this->plugin = new Callback($configuration, 'map', []);
$value = $this->plugin
->transform($args, $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertSame($result, $value);
}
public function providerCallbackArray() {
return [
'date format' => [
'date',
[
'Y-m-d',
995328000,
],
'2001-07-17',
],
'rtrim' => [
'rtrim',
[
'https://www.example.com/',
'/',
],
'https://www.example.com',
],
'str_replace' => [
'str_replace',
[
[
'One',
'two',
],
[
'1',
'2',
],
'One, two, three!',
],
'1, 2, three!',
],
'pi' => [
'pi',
[],
pi(),
],
];
}
public function testCallbackExceptions($message, array $configuration, $class = 'InvalidArgumentException', $args = NULL) {
$this
->expectException($class);
$this
->expectExceptionMessage($message);
$this->plugin = new Callback($configuration, 'map', []);
$this->plugin
->transform($args, $this->migrateExecutable, $this->row, 'destination_property');
}
public function providerCallbackExceptions() {
return [
'not set' => [
'message' => 'The "callable" must be set.',
'configuration' => [],
],
'invalid method' => [
'message' => 'The "callable" must be a valid function or method.',
'configuration' => [
'callable' => 'nonexistent_callable',
],
],
'array required' => [
'message' => "When 'unpack_source' is set, the source must be an array. Instead it was of type 'string'",
'configuration' => [
'callable' => 'count',
'unpack_source' => TRUE,
],
'class' => MigrateException::class,
'args' => 'This string is not an array.',
],
];
}
public static function strToLower($string) {
return mb_strtolower($string);
}
}