CallbackTest.php

Same filename in other branches
  1. 9 core/modules/migrate/tests/src/Unit/process/CallbackTest.php
  2. 10 core/modules/migrate/tests/src/Unit/process/CallbackTest.php
  3. 11.x core/modules/migrate/tests/src/Unit/process/CallbackTest.php

Namespace

Drupal\Tests\migrate\Unit\process

File

core/modules/migrate/tests/src/Unit/process/CallbackTest.php

View source
<?php

namespace Drupal\Tests\migrate\Unit\process;

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

/**
 * Tests the callback process plugin.
 *
 * @group migrate
 */
class CallbackTest extends MigrateProcessTestCase {
    
    /**
     * Test callback with valid "callable".
     *
     * @dataProvider providerCallback
     */
    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);
    }
    
    /**
     * Data provider for ::testCallback().
     */
    public function providerCallback() {
        return [
            'function' => [
                'strtolower',
            ],
            'class method' => [
                [
                    self::class,
                    'strtolower',
                ],
            ],
        ];
    }
    
    /**
     * Test callback exceptions.
     *
     * @dataProvider providerCallbackExceptions
     */
    public function testCallbackExceptions($message, $configuration) {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage($message);
        $this->plugin = new Callback($configuration, 'map', []);
    }
    
    /**
     * Data provider for ::testCallbackExceptions().
     */
    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',
                ],
            ],
        ];
    }
    
    /**
     * 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);
    }

}

Classes

Title Deprecated Summary
CallbackTest Tests the callback process plugin.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.