transform example


  protected function loadMenuTree(MenuInterface $menu) : array {
    $parameters = new MenuTreeParameters();
    $parameters->onlyEnabledLinks();
    $parameters->setMinDepth(0);
    $tree = $this->menuTree->load($menu->id()$parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
    ];
    return $this->menuTree->transform($tree$manipulators);
  }

}


    /** * @dataProvider dataProvider */
    public function testTransform($format$output$input)
    {
        $transformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);

        $input = new \DateTime($input);

        $this->assertEquals($output$transformer->transform($input));
    }

    public function testTransformEmpty()
    {
        $transformer = new DateTimeToStringTransformer();

        $this->assertSame('', $transformer->transform(null));
    }

    public function testTransformWithDifferentTimezones()
    {
        

  public function testTransform($value$expected) {
    $executable = $this->prophesize(MigrateExecutableInterface::class)
      ->reveal();
    if (empty($row)) {
      $row = $this->prophesize(Row::class)->reveal();
    }

    $plugin = new BlockSettings([], 'block_settings', []);
    $actual = $plugin->transform($value$executable$row, 'foo');
    $this->assertSame($expected$actual);
  }

  /** * Provides data for testTransform. */
  public function providerTestTransform() {
    return [
      'title set' => [
        [
          'custom',
          

class NullCoalesceTest extends MigrateProcessTestCase {

  /** * Tests that an exception is thrown for a non-array value. * * @covers ::transform */
  public function testExceptionOnInvalidValue() {
    $this->expectException(MigrateException::class);
    (new NullCoalesce([], 'null_coalesce', []))->transform('invalid', $this->migrateExecutable, $this->row, 'destination_property');
  }

  /** * Tests null_coalesce. * * @param array $source * The source value. * @param mixed $expected_result * The expected result. * * @covers ::transform * * @dataProvider transformDataProvider * * @throws \Drupal\migrate\MigrateException */
/** * Tests various default cases. * * @covers ::transform */
  public function testDefaults() {
    $this->row->expects($this->once())
      ->method('getSourceProperty')
      ->willReturn('date');

    // Assert common values are passed through without modification.     $this->assertNull($this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'property'));
    $this->assertEquals('string', $this->plugin->transform('string', $this->migrateExecutable, $this->row, 'property'));
    $this->assertEquals(1234, $this->plugin->transform(1234, $this->migrateExecutable, $this->row, 'property'));
    // Assert that an array checks that this is a date field(above mock assert)     // and returns "datetime_default".     $this->assertEquals('datetime_default', $this->plugin->transform([]$this->migrateExecutable, $this->row, 'property'));
  }

  /** * Tests an exception is thrown when the input is not a date field. * * @covers ::transform */


  /** * Tests Substr plugin based on providerTestSubstr() values. * * @dataProvider providerTestSubstr */
  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);
  }

  /** * Data provider for testSubstr(). */
  public function providerTestSubstr() {
    return [
      // Tests with valid start and length values.       [0, 7, 'Captain'],
      // Tests with valid start > 0 and valid length.
false, '0', false, '1'],
            ['X', 'X', 'X', '2'],
            [true, '1', null, '3'],
        ];
    }

    /** * @dataProvider transformProvider */
    public function testTransform($in$out$inWithNull$outWithNull)
    {
        $this->assertSame($out$this->transformer->transform($in));
        $this->assertSame($outWithNull$this->transformerWithNull->transform($inWithNull));
    }

    public static function reverseTransformProvider()
    {
        return [
            // values are expected to be valid choice keys already and stay             // the same             ['', '', '0', ''],
            ['0', false, '1', false],
            ['X', 'X', '2', 'X'],
            [

    public function transform(mixed $value): mixed
    {
        foreach ($this->transformers as $transformer) {
            $value = $transformer->transform($value);
        }

        return $value;
    }

    /** * Passes the value through the reverseTransform() method of all nested * transformers. * * The transformers receive the value in the reverse order as they were passed * to the constructor. Each transformer receives the result of the previous * transformer as input. The output of the last transformer is returned * by this method. * * @param mixed $value The transformed value * * @throws TransformationFailedException */
'1.23', 1.23],
        ];
    }

    /** * @dataProvider provideTransformations */
    public function testTransform($from$to)
    {
        $transformer = new StringToFloatTransformer();

        $this->assertSame($to$transformer->transform($from));
    }

    public function testFailIfTransformingANonString()
    {
        $this->expectException(TransformationFailedException::class);
        $transformer = new StringToFloatTransformer();
        $transformer->transform(1.0);
    }

    public function testFailIfTransformingANonNumericString()
    {
        

  protected function doTransform($value) {
    $pathValidator = $this->container->get('path.validator');
    $row = new Row();
    $migration = $this->prophesize(MigrationInterface::class)->reveal();
    $executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();

    $plugin = new Route([], 'route', []$migration$pathValidator);
    $actual = $plugin->transform($value$executable$row, 'destination_property');
    return $actual;
  }

}
'UTC', 'UTC', '2018-10-03T10:00:00.000Z', '2018-10-03T10:00:00.000Z'],
        ];
    }

    /** * @dataProvider transformProvider */
    public function testTransform($fromTz$toTz$from$to, bool $withSeconds)
    {
        $transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz$toTz$withSeconds);

        $this->assertSame($to$transformer->transform(null !== $from ? new \DateTime($from) : null));
    }

    /** * @dataProvider transformProvider */
    public function testTransformDateTimeImmutable($fromTz$toTz$from$to, bool $withSeconds)
    {
        $transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz$toTz$withSeconds);

        $this->assertSame($to$transformer->transform(null !== $from ? new \DateTimeImmutable($from) : null));
    }

    

  public function testMigrateExceptionMissingFromFormat() {
    $configuration = [
      'from_format' => '',
      'to_format' => 'Y-m-d',
    ];

    $this->expectException(MigrateException::class);
    $this->expectExceptionMessage('Format date plugin is missing from_format configuration.');
    $this->plugin = new FormatDate($configuration, 'test_format_date', []);
    $this->plugin->transform('01/05/1955', $this->migrateExecutable, $this->row, 'field_date');
  }

  /** * Tests that missing configuration will throw an exception. */
  public function testMigrateExceptionMissingToFormat() {
    $configuration = [
      'from_format' => 'm/d/Y',
      'to_format' => '',
    ];

    

        $this->transformer = new CollectionToArrayTransformer();
    }

    public function testTransform()
    {
        $array = [
            2 => 'foo',
            3 => 'bar',
        ];

        $this->assertSame($array$this->transformer->transform(new ArrayCollection($array)));
    }

    /** * This test is needed for cases when getXxxs() in the entity returns the * result of $collection->toArray(), in order to prevent modifications of * the inner collection. * * See https://github.com/symfony/symfony/pull/9308 */
    public function testTransformArray()
    {
        
$configuration = [
      'delimiter' => ',',
    ];
    $this->plugin = new Explode($configuration, 'map', []);
    parent::setUp();
  }

  /** * Tests explode transform process works. */
  public function testTransform() {
    $value = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destination_property');
    $this->assertSame(['foo', 'bar', 'tik']$value);
  }

  /** * Tests explode transform process works with a limit. */
  public function testTransformLimit() {
    $plugin = new Explode(['delimiter' => '_', 'limit' => 2], 'map', []);
    $value = $plugin->transform('foo_bar_tik', $this->migrateExecutable, $this->row, 'destination_property');
    $this->assertSame(['foo', 'bar_tik']$value);
  }

  

class FieldLinkTest extends UnitTestCase {

  /** * Tests the URL transformations in the FieldLink process plugin. * * @dataProvider canonicalizeUriDataProvider */
  public function testCanonicalizeUri($url$expected$configuration = []) {
    $link_plugin = new FieldLink($configuration, '', []$this->createMock(MigrationInterface::class));
    $transformed = $link_plugin->transform([
      'url' => $url,
      'title' => '',
      'attributes' => serialize([]),
    ]$this->createMock(MigrateExecutableInterface::class)$this->getMockBuilder(Row::class)->disableOriginalConstructor()->getMock(), NULL);
    $this->assertEquals($expected$transformed['uri']);
  }

  /** * Data provider for testCanonicalizeUri. */
  public function canonicalizeUriDataProvider() {
    
Home | Imprint | This part of the site doesn't use cookies.