01ab2da74d2db11391940aa75164877400cd7ad7
[yaffs-website] / web / core / modules / migrate / tests / src / Unit / process / GetTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\migrate\Unit\process\GetTest.
6  */
7
8 namespace Drupal\Tests\migrate\Unit\process;
9
10 use Drupal\migrate\Plugin\migrate\process\TestGet;
11
12 /**
13  * Tests the get process plugin.
14  *
15  * @group migrate
16  */
17 class GetTest extends MigrateProcessTestCase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     $this->plugin = new TestGet();
24     parent::setUp();
25   }
26
27   /**
28    * Tests the Get plugin when source is a string.
29    */
30   public function testTransformSourceString() {
31     $this->row->expects($this->once())
32       ->method('getSourceProperty')
33       ->with('test')
34       ->will($this->returnValue('source_value'));
35     $this->plugin->setSource('test');
36     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
37     $this->assertSame($value, 'source_value');
38   }
39
40   /**
41    * Tests the Get plugin when source is an array.
42    */
43   public function testTransformSourceArray() {
44     $map = [
45       'test1' => 'source_value1',
46       'test2' => 'source_value2',
47     ];
48     $this->plugin->setSource(['test1', 'test2']);
49     $this->row->expects($this->exactly(2))
50       ->method('getSourceProperty')
51       ->will($this->returnCallback(function ($argument)  use ($map) { return $map[$argument]; } ));
52     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
53     $this->assertSame($value, ['source_value1', 'source_value2']);
54   }
55
56   /**
57    * Tests the Get plugin when source is a string pointing to destination.
58    */
59   public function testTransformSourceStringAt() {
60     $this->row->expects($this->once())
61       ->method('getSourceProperty')
62       ->with('@test')
63       ->will($this->returnValue('source_value'));
64     $this->plugin->setSource('@@test');
65     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
66     $this->assertSame($value, 'source_value');
67   }
68
69   /**
70    * Tests the Get plugin when source is an array pointing to destination.
71    */
72   public function testTransformSourceArrayAt() {
73     $map = [
74       'test1' => 'source_value1',
75       '@test2' => 'source_value2',
76       '@test3' => 'source_value3',
77       'test4' => 'source_value4',
78     ];
79     $this->plugin->setSource(['test1', '@@test2', '@@test3', 'test4']);
80     $this->row->expects($this->exactly(4))
81       ->method('getSourceProperty')
82       ->will($this->returnCallback(function ($argument)  use ($map) { return $map[$argument]; } ));
83     $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
84     $this->assertSame($value, ['source_value1', 'source_value2', 'source_value3', 'source_value4']);
85   }
86
87   /**
88    * Tests the Get plugin when source has integer values.
89    */
90   public function testIntegerValues() {
91     $this->row->expects($this->exactly(2))
92       ->method('getSourceProperty')
93       ->willReturnOnConsecutiveCalls('val1', 'val2');
94
95     $this->plugin->setSource([0 => 0, 1 => 'test']);
96     $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
97     $this->assertSame([0 => 'val1', 1 => 'val2'], $return);
98
99     $this->plugin->setSource([FALSE]);
100     $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
101     $this->assertSame([NULL], $return);
102
103     $this->plugin->setSource([NULL]);
104     $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
105     $this->assertSame([NULL], $return);
106   }
107
108 }
109
110 namespace Drupal\migrate\Plugin\migrate\process;
111
112 class TestGet extends Get {
113   public function __construct() {
114   }
115   public function setSource($source) {
116     $this->configuration['source'] = $source;
117   }
118
119 }