From 0cc5879a8adce2f13bb8ad0450d2f036c9681a7a Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Fri, 8 Jun 2018 10:38:41 +0100 Subject: [PATCH] Issue #2973366 by Jo Fitzgerald: GetTest.php should test Get plugin, rather than a class the extends it --- .../src/Plugin/migrate/process/Get.php | 4 +- .../tests/src/Unit/process/GetTest.php | 76 +++++++++---------- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/core/modules/migrate/src/Plugin/migrate/process/Get.php b/core/modules/migrate/src/Plugin/migrate/process/Get.php index 26799cb2307c..10b6eca20868 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/Get.php +++ b/core/modules/migrate/src/Plugin/migrate/process/Get.php @@ -70,8 +70,8 @@ * property bar. foo configuration is included for illustration purposes. * * Because of this, if the source or destination property actually starts with a - * @, that character must be escaped with @@. - * The referenced property becomes, for example, @@@foo. + * "@", that character must be escaped with "@@". The referenced property + * becomes, for example, "@@@foo". * * @code * process: diff --git a/core/modules/migrate/tests/src/Unit/process/GetTest.php b/core/modules/migrate/tests/src/Unit/process/GetTest.php index 67f56b0a78b7..df5f98ab8044 100644 --- a/core/modules/migrate/tests/src/Unit/process/GetTest.php +++ b/core/modules/migrate/tests/src/Unit/process/GetTest.php @@ -1,13 +1,8 @@ <?php -/** - * @file - * Contains \Drupal\Tests\migrate\Unit\process\GetTest. - */ - namespace Drupal\Tests\migrate\Unit\process; -use Drupal\migrate\Plugin\migrate\process\TestGet; +use Drupal\migrate\Plugin\migrate\process\Get; /** * Tests the get process plugin. @@ -16,14 +11,6 @@ */ class GetTest extends MigrateProcessTestCase { - /** - * {@inheritdoc} - */ - protected function setUp() { - $this->plugin = new TestGet(); - parent::setUp(); - } - /** * Tests the Get plugin when source is a string. */ @@ -32,7 +19,7 @@ public function testTransformSourceString() { ->method('getSourceProperty') ->with('test') ->will($this->returnValue('source_value')); - $this->plugin->setSource('test'); + $this->plugin = new Get(['source' => 'test'], '', []); $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame('source_value', $value); } @@ -45,7 +32,7 @@ public function testTransformSourceArray() { 'test1' => 'source_value1', 'test2' => 'source_value2', ]; - $this->plugin->setSource(['test1', 'test2']); + $this->plugin = new Get(['source' => ['test1', 'test2']], '', []); $this->row->expects($this->exactly(2)) ->method('getSourceProperty') ->will($this->returnCallback(function ($argument) use ($map) { @@ -63,7 +50,7 @@ public function testTransformSourceStringAt() { ->method('getSourceProperty') ->with('@test') ->will($this->returnValue('source_value')); - $this->plugin->setSource('@@test'); + $this->plugin = new Get(['source' => '@@test'], '', []); $value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame('source_value', $value); } @@ -78,7 +65,7 @@ public function testTransformSourceArrayAt() { '@test3' => 'source_value3', 'test4' => 'source_value4', ]; - $this->plugin->setSource(['test1', '@@test2', '@@test3', 'test4']); + $this->plugin = new Get(['source' => ['test1', '@@test2', '@@test3', 'test4']], '', []); $this->row->expects($this->exactly(4)) ->method('getSourceProperty') ->will($this->returnCallback(function ($argument) use ($map) { @@ -90,36 +77,47 @@ public function testTransformSourceArrayAt() { /** * Tests the Get plugin when source has integer values. + * + * @dataProvider integerValuesDataProvider */ - public function testIntegerValues() { - $this->row->expects($this->exactly(2)) + public function testIntegerValues($source, $expected_value) { + $this->row->expects($this->atMost(2)) ->method('getSourceProperty') ->willReturnOnConsecutiveCalls('val1', 'val2'); - $this->plugin->setSource([0 => 0, 1 => 'test']); + $this->plugin = new Get(['source' => $source], '', []); $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty'); - $this->assertSame([0 => 'val1', 1 => 'val2'], $return); - - $this->plugin->setSource([FALSE]); - $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty'); - $this->assertSame([NULL], $return); - - $this->plugin->setSource([NULL]); - $return = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty'); - $this->assertSame([NULL], $return); + $this->assertSame($expected_value, $return); } -} - -namespace Drupal\migrate\Plugin\migrate\process; - -class TestGet extends Get { - - public function __construct() { + /** + * Provides data for the successful lookup test. + * + * @return array + */ + public function integerValuesDataProvider() { + return [ + [ + 'source' => [0 => 0, 1 => 'test'], + 'expected_value' => [0 => 'val1', 1 => 'val2'], + ], + [ + 'source' => [FALSE], + 'expected_value' => [NULL], + ], + [ + 'source' => [NULL], + 'expected_value' => [NULL], + ], + ]; } - public function setSource($source) { - $this->configuration['source'] = $source; + /** + * Tests the Get plugin for syntax errors, e.g. "Invalid tag_line detected" by + * creating a prophecy of the class. + */ + public function testPluginSyntax() { + $this->assertNotNull($this->prophesize(Get::class)); } } -- GitLab