View source
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\process\Substr;
class SubstrTest extends MigrateProcessTestCase {
protected function setUp() : void {
parent::setUp();
}
public function testSubstr($start = NULL, $length = NULL, $expected = NULL) {
$configuration['start'] = $start;
$configuration['length'] = $length;
$this->plugin = new Substr($configuration, 'map', []);
$value = $this->plugin
->transform('Captain Janeway', $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertSame($expected, $value);
}
public function providerTestSubstr() {
return [
[
0,
7,
'Captain',
],
[
6,
3,
'n J',
],
[
-7,
4,
'Jane',
],
[
NULL,
7,
'Captain',
],
[
1,
NULL,
'aptain Janeway',
],
[
NULL,
NULL,
'Captain Janeway',
],
];
}
public function testSubstrFail() {
$configuration = [];
$this->plugin = new Substr($configuration, 'map', []);
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage('The input value must be a string.');
$this->plugin
->transform([
'Captain Janeway',
], $this->migrateExecutable, $this->row, 'destination_property');
}
public function testStartIsString() {
$configuration['start'] = '2';
$this->plugin = new Substr($configuration, 'map', []);
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
$this->plugin
->transform([
'foo',
], $this->migrateExecutable, $this->row, 'destination_property');
}
public function testLengthIsString() {
$configuration['length'] = '1';
$this->plugin = new Substr($configuration, 'map', []);
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage('The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.');
$this->plugin
->transform([
'foo',
], $this->migrateExecutable, $this->row, 'destination_property');
}
}