CallbackTest.php 3.63 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\migrate\Unit\process;
4

5
use Drupal\migrate\MigrateException;
6
7
8
use Drupal\migrate\Plugin\migrate\process\Callback;

/**
9
10
 * Tests the callback process plugin.
 *
11
12
13
14
15
 * @group migrate
 */
class CallbackTest extends MigrateProcessTestCase {

  /**
16
17
18
   * Test callback with valid "callable".
   *
   * @dataProvider providerCallback
19
   */
20
21
22
  public function testCallback($callable) {
    $configuration = ['callable' => $callable];
    $this->plugin = new Callback($configuration, 'map', []);
23
    $value = $this->plugin->transform('FooBar', $this->migrateExecutable, $this->row, 'destination_property');
24
    $this->assertSame('foobar', $value);
25
26
27
  }

  /**
28
   * Data provider for ::testCallback().
29
   */
30
31
32
  public function providerCallback() {
    return [
      'function' => ['strtolower'],
33
      'class method' => [[self::class, 'strtolower']],
34
    ];
35
36
  }

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  /**
   * Test callback with valid "callable" and multiple arguments.
   *
   * @dataProvider providerCallbackArray
   */
  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);
  }

  /**
   * Data provider for ::testCallbackArray().
   */
  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!',
      ],
    ];
  }

72
  /**
73
   * Test callback exceptions.
74
   *
75
76
77
78
79
80
81
82
83
   * @param string $message
   *   The expected exception message.
   * @param array $configuration
   *   The plugin configuration being tested.
   * @param string $class
   *   (optional) The expected exception class.
   * @param mixed $args
   *   (optional) Arguments to pass to the transform() method.
   *
84
85
   * @dataProvider providerCallbackExceptions
   */
86
87
  public function testCallbackExceptions($message, array $configuration, $class = 'InvalidArgumentException', $args = NULL) {
    $this->expectException($class);
88
    $this->expectExceptionMessage($message);
89
    $this->plugin = new Callback($configuration, 'map', []);
90
    $this->plugin->transform($args, $this->migrateExecutable, $this->row, 'destination_property');
91
92
  }

93
94
95
96
97
98
99
  /**
   * Data provider for ::testCallbackExceptions().
   */
  public function providerCallbackExceptions() {
    return [
      'not set' => [
        'message' => 'The "callable" must be set.',
100
        'configuration' => [],
101
102
103
      ],
      'invalid method' => [
        'message' => 'The "callable" must be a valid function or method.',
104
        'configuration' => ['callable' => 'nonexistent_callable'],
105
      ],
106
107
108
109
110
111
      '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.',
      ],
112
    ];
113
114
  }

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  /**
   * Makes a string lowercase for testing purposes.
   *
   * @param string $string
   *   The input string.
   *
   * @return string
   *   The lowercased string.
   *
   * @see \Drupal\Tests\migrate\Unit\process\CallbackTest::providerCallback()
   */
  public static function strToLower($string) {
    return mb_strtolower($string);
  }

130
}