StaticMapTest.php 3.17 KB
Newer Older
1 2
<?php

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

5
use Drupal\Component\Utility\Variable;
6 7
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateSkipRowException;
8 9 10
use Drupal\migrate\Plugin\migrate\process\StaticMap;

/**
11 12
 * Tests the static map process plugin.
 *
13 14 15 16 17 18 19
 * @group migrate
 */
class StaticMapTest extends MigrateProcessTestCase {

  /**
   * {@inheritdoc}
   */
20
  protected function setUp() {
21
    $configuration['map']['foo']['bar'] = 'baz';
22
    $this->plugin = new StaticMap($configuration, 'map', []);
23 24 25 26 27 28
    parent::setUp();
  }

  /**
   * Tests map when the source is a string.
   */
29
  public function testMapWithSourceString() {
30
    $value = $this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destination_property');
31
    $this->assertSame(['bar' => 'baz'], $value);
32 33 34 35 36
  }

  /**
   * Tests map when the source is a list.
   */
37
  public function testMapWithSourceList() {
38
    $value = $this->plugin->transform(['foo', 'bar'], $this->migrateExecutable, $this->row, 'destination_property');
39
    $this->assertSame('baz', $value);
40 41 42 43 44
  }

  /**
   * Tests when the source is empty.
   */
45
  public function testMapwithEmptySource() {
46
    $this->expectException(MigrateException::class);
47
    $this->plugin->transform([], $this->migrateExecutable, $this->row, 'destination_property');
48 49 50 51 52
  }

  /**
   * Tests when the source is invalid.
   */
53
  public function testMapwithInvalidSource() {
54
    $this->expectException(MigrateSkipRowException::class);
55 56
    $this->expectExceptionMessage(sprintf("No static mapping found for '%s' and no default value provided for destination '%s'.", Variable::export(['bar']), 'destination_property'));
    $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destination_property');
57 58 59 60 61 62 63 64
  }

  /**
   * Tests when the source is invalid but there's a default.
   */
  public function testMapWithInvalidSourceWithADefaultValue() {
    $configuration['map']['foo']['bar'] = 'baz';
    $configuration['default_value'] = 'test';
65
    $this->plugin = new StaticMap($configuration, 'map', []);
66
    $value = $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destination_property');
67
    $this->assertSame('test', $value);
68 69
  }

70 71 72 73 74 75 76
  /**
   * Tests when the source is invalid but there's a default value of NULL.
   */
  public function testMapWithInvalidSourceWithANullDefaultValue() {
    $configuration['map']['foo']['bar'] = 'baz';
    $configuration['default_value'] = NULL;
    $this->plugin = new StaticMap($configuration, 'map', []);
77
    $value = $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destination_property');
78 79 80
    $this->assertNull($value);
  }

81 82 83 84 85 86 87
  /**
   * Tests when the source is invalid and bypass is enabled.
   */
  public function testMapWithInvalidSourceAndBypass() {
    $configuration['map']['foo']['bar'] = 'baz';
    $configuration['default_value'] = 'test';
    $configuration['bypass'] = TRUE;
88
    $this->plugin = new StaticMap($configuration, 'map', []);
89 90
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('Setting both default_value and bypass is invalid.');
91
    $this->plugin->transform(['bar'], $this->migrateExecutable, $this->row, 'destination_property');
92 93 94
  }

}