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

3
namespace Drupal\Tests\migrate\Unit\process;
4 5 6 7

use Drupal\migrate\Plugin\migrate\process\Callback;

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

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

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

36
  /**
37
   * Test callback exceptions.
38 39 40 41
   *
   * @dataProvider providerCallbackExceptions
   */
  public function testCallbackExceptions($message, $configuration) {
42 43
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage($message);
44
    $this->plugin = new Callback($configuration, 'map', []);
45 46
  }

47 48 49 50 51 52 53
  /**
   * Data provider for ::testCallbackExceptions().
   */
  public function providerCallbackExceptions() {
    return [
      'not set' => [
        'message' => 'The "callable" must be set.',
54
        'configuration' => [],
55 56 57
      ],
      'invalid method' => [
        'message' => 'The "callable" must be a valid function or method.',
58
        'configuration' => ['callable' => 'nonexistent_callable'],
59 60
      ],
    ];
61 62
  }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * 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);
  }

78
}