Commit f522823e authored by Nazar Velychenko's avatar Nazar Velychenko Committed by Ivan Doroshenko
Browse files

Issue #3261276: Throw consistent exceptions on...

Issue #3261276: Throw consistent exceptions on \Drupal\migrate_plus\Plugin\migrate\process\StrReplace process plugin 
parent ca621bc3
Loading
Loading
Loading
Loading
+13 −5
Changes for src/Plugin/migrate/process/StrReplace.php: 13 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -81,13 +81,21 @@ class StrReplace extends ProcessPluginBase {
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (!isset($this->configuration['search'])) {
      throw new MigrateException('"search" must be configured.');
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    if (!isset($configuration['search'])) {
      throw new \InvalidArgumentException('The "search" must be set.');
    }
    if (!isset($configuration['replace'])) {
      throw new \InvalidArgumentException('The "replace" must be set.');
    }
    if (!isset($this->configuration['replace'])) {
      throw new MigrateException('"replace" must be configured.');

    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $this->multiple = is_array($value);
    $this->configuration += [
      'case_insensitive' => FALSE,
+10 −14
Changes for tests/src/Unit/process/StrReplaceTest.php: 10 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -55,27 +55,23 @@ class StrReplaceTest extends MigrateProcessTestCase {
  }

  /**
   * Test for MigrateException for "search" configuration.
   * Test for InvalidArgumentException for "search" configuration.
   */
  public function testSearchMigrateException(): void {
    $value = 'vero eos et accusam et justo vero';
  public function testSearchInvalidArgumentException(): void {
    $configuration['replace'] = 'that';
    $plugin = new StrReplace($configuration, 'str_replace', []);
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('"search" must be configured.');
    $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('The "search" must be set.');
    new StrReplace($configuration, 'str_replace', []);
  }

  /**
   * Test for MigrateException for "replace" configuration.
   * Test for InvalidArgumentException for "replace" configuration.
   */
  public function testReplaceMigrateException(): void {
    $value = 'vero eos et accusam et justo vero';
  public function testReplaceInvalidArgumentException(): void {
    $configuration['search'] = 'et';
    $plugin = new StrReplace($configuration, 'str_replace', []);
    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('"replace" must be configured.');
    $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('The "replace" must be set.');
    new StrReplace($configuration, 'str_replace', []);
  }

  /**