SkipOnEmptyTest.php 2.59 KB
Newer Older
1 2 3
<?php

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

5 6
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty;

/**
 * Tests the skip on empty process plugin.
 *
 * @group migrate
 * @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\SkipOnEmpty
 */
class SkipOnEmptyTest extends MigrateProcessTestCase {

  /**
   * @covers ::process
   */
  public function testProcessSkipsOnEmpty() {
    $configuration['method'] = 'process';
22
    $this->expectException(MigrateSkipProcessException::class);
23
    (new SkipOnEmpty($configuration, 'skip_on_empty', []))
24
      ->transform('', $this->migrateExecutable, $this->row, 'destination_property');
25 26 27 28 29 30 31 32
  }

  /**
   * @covers ::process
   */
  public function testProcessBypassesOnNonEmpty() {
    $configuration['method'] = 'process';
    $value = (new SkipOnEmpty($configuration, 'skip_on_empty', []))
33
      ->transform(' ', $this->migrateExecutable, $this->row, 'destination_property');
34
    $this->assertSame(' ', $value);
35 36 37 38 39 40 41
  }

  /**
   * @covers ::row
   */
  public function testRowSkipsOnEmpty() {
    $configuration['method'] = 'row';
42
    $this->expectException(MigrateSkipRowException::class);
43
    (new SkipOnEmpty($configuration, 'skip_on_empty', []))
44
      ->transform('', $this->migrateExecutable, $this->row, 'destination_property');
45 46 47 48 49 50 51 52
  }

  /**
   * @covers ::row
   */
  public function testRowBypassesOnNonEmpty() {
    $configuration['method'] = 'row';
    $value = (new SkipOnEmpty($configuration, 'skip_on_empty', []))
53
      ->transform(' ', $this->migrateExecutable, $this->row, 'destination_property');
54
    $this->assertSame(' ', $value);
55 56
  }

57 58 59 60 61 62 63 64 65 66
  /**
   * Tests that a skip row exception without a message is raised.
   *
   * @covers ::row
   */
  public function testRowSkipWithoutMessage() {
    $configuration = [
      'method' => 'row',
    ];
    $process = new SkipOnEmpty($configuration, 'skip_on_empty', []);
67
    $this->expectException(MigrateSkipRowException::class);
68
    $process->transform('', $this->migrateExecutable, $this->row, 'destination_property');
69 70 71 72 73 74 75 76 77 78 79 80 81
  }

  /**
   * Tests that a skip row exception with a message is raised.
   *
   * @covers ::row
   */
  public function testRowSkipWithMessage() {
    $configuration = [
      'method' => 'row',
      'message' => 'The value is empty',
    ];
    $process = new SkipOnEmpty($configuration, 'skip_on_empty', []);
82 83
    $this->expectException(MigrateSkipRowException::class);
    $this->expectExceptionMessage('The value is empty');
84
    $process->transform('', $this->migrateExecutable, $this->row, 'destination_property');
85 86
  }

87
}