Loading tests/data/simple_xml_current_url1.xml→tests/data/xml_current_url1.xml File moved. View file tests/data/simple_xml_current_url2.xml→tests/data/xml_current_url2.xml File moved. View file tests/data/simple_xml_reduce_single_value.xml→tests/data/xml_reduce_single_value.xml File moved. View file tests/src/Kernel/Plugin/migrate_plus/data_parser/BaseXmlTest.php +174 Changes for tests/src/Kernel/Plugin/migrate_plus/data_parser/BaseXmlTest.php: 174 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types = 1); namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser; use Drupal\KernelTests\KernelTestBase; use Drupal\migrate_plus\DataParserPluginInterface; use Drupal\migrate_plus\DataParserPluginManager; /** * Test of the data_parser SimpleXml migrate_plus plugin. * * @group migrate_plus */ abstract class BaseXmlTest extends KernelTestBase { /** * {@inheritdoc} */ protected static $modules = ['migrate', 'migrate_plus']; /** * Path for the xml file. */ protected ?string $path; /** * The plugin manager. */ protected ?DataParserPluginManager $pluginManager = NULL; /** * The plugin configuration. */ protected ?array $configuration; /** * The expected result. */ protected ?array $expected; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->path = $this->container->get('module_handler') ->getModule('migrate_plus')->getPath(); $this->pluginManager = $this->container ->get('plugin.manager.migrate_plus.data_parser'); $this->configuration = [ 'plugin' => 'url', 'data_fetcher_plugin' => 'file', 'data_parser_plugin' => 'simple_xml', 'destination' => 'node', 'urls' => [], 'ids' => ['id' => ['type' => 'integer']], 'fields' => [ [ 'name' => 'id', 'label' => 'Id', 'selector' => '@id', ], [ 'name' => 'values', 'label' => 'Values', 'selector' => 'values', ], ], 'item_selector' => '/items/item', ]; $this->expected = [ [ 'Value 1', 'Value 2', ], [ 'Value 1 (single)', ], ]; } /** * Tests current URL of parsed XML item. */ public function testCurrentUrl(): void { $urls = [ $this->path . '/tests/data/xml_current_url1.xml', $this->path . '/tests/data/xml_current_url2.xml', ]; $this->configuration['urls'] = $urls; $parser = $this->getParser(); // First 2 items available in the first URL. $parser->rewind(); $this->assertEquals($urls[0], $parser->currentUrl()); $parser->next(); $this->assertEquals($urls[0], $parser->currentUrl()); // Third item available in the second URL. $parser->next(); $this->assertEquals($urls[1], $parser->currentUrl()); } /** * Tests reducing single values. */ public function testReduceSingleValue(): void { $url = $this->path . '/tests/data/xml_reduce_single_value.xml'; $this->configuration['urls'][0] = $url; $this->assertResults($this->expected, $this->getParser()); } /** * Tests retrieving single value from element with attributes. */ public function testSingleValueWithAttributes() { $urls = [ $this->path . '/tests/data/xml_data_parser.xml', ]; $this->configuration['urls'] = $urls; $this->configuration['item_selector'] = '/persons/person'; $this->configuration['fields'] = [ [ 'name' => 'id', 'label' => 'Id', 'selector' => 'id', ], [ 'name' => 'child', 'label' => 'child', 'selector' => 'children/child', ], ]; $names = []; foreach ($this->getParser() as $item) { $names[] = (string) $item['child']->name; } $expected_names = ['Elizabeth Junior', 'George Junior', 'Lucy']; $this->assertEquals($expected_names, $names); } /** * Parses and asserts the results match expectations. * * @param array|string $expected * The expected results. * @param \Traversable $parser * An iterable data result to parse. */ protected function assertResults($expected, \Traversable $parser): void { $data = []; foreach ($parser as $item) { $values = []; foreach ($item['values'] as $value) { $values[] = (string) $value; } $data[] = $values; } $this->assertEquals($expected, $data); } /** * Returns a parse object with active configuration. * * @return \Drupal\migrate_plus\DataParserPluginInterface * Data parser object. */ abstract protected function getParser(): DataParserPluginInterface; } tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php +14 −144 Changes for tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php: 14 added lines, 144 removed lines. Original line number Diff line number Diff line Loading @@ -4,146 +4,40 @@ declare(strict_types = 1); namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser; use Drupal\KernelTests\KernelTestBase; use Drupal\migrate\MigrateException; use Drupal\migrate_plus\DataParserPluginManager; use Drupal\migrate_plus\DataParserPluginInterface; /** * Test of the data_parser SimpleXml migrate_plus plugin. * * @group migrate_plus */ final class SimpleXmlTest extends KernelTestBase { class SimpleXmlTest extends BaseXmlTest { /** * {@inheritdoc} */ protected static $modules = ['migrate', 'migrate_plus']; /** * Path for the xml file. */ protected ?string $path; /** * The plugin manager. */ protected ?DataParserPluginManager $pluginManager = NULL; /** * The plugin configuration. */ protected ?array $configuration; /** * The expected result. */ protected ?array $expected; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->path = $this->container->get('module_handler') ->getModule('migrate_plus')->getPath(); $this->pluginManager = $this->container ->get('plugin.manager.migrate_plus.data_parser'); $this->configuration = [ 'plugin' => 'url', 'data_fetcher_plugin' => 'file', 'data_parser_plugin' => 'simple_xml', 'destination' => 'node', 'urls' => [], 'ids' => ['id' => ['type' => 'integer']], 'fields' => [ [ 'name' => 'id', 'label' => 'Id', 'selector' => '@id', ], [ 'name' => 'values', 'label' => 'Values', 'selector' => 'values', ], ], 'item_selector' => '/items/item', ]; $this->expected = [ [ 'Value 1', 'Value 2', ], [ 'Value 1 (single)', ], ]; } /** * Tests current URL of parsed XML item. */ public function testCurrentUrl(): void { $urls = [ $this->path . '/tests/data/simple_xml_current_url1.xml', $this->path . '/tests/data/simple_xml_current_url2.xml', ]; $this->configuration['urls'] = $urls; /** @var \Drupal\migrate_plus\DataParserPluginInterface $parser */ $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); // First 2 items available in the first URL. $parser->rewind(); $this->assertEquals($urls[0], $parser->currentUrl()); $parser->next(); $this->assertEquals($urls[0], $parser->currentUrl()); // Third item available in the second URL. $parser->next(); $this->assertEquals($urls[1], $parser->currentUrl()); } /** * Tests reducing single values. */ public function testReduceSingleValue(): void { $url = $this->path . '/tests/data/simple_xml_reduce_single_value.xml'; $this->configuration['urls'][0] = $url; $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $this->assertResults($this->expected, $parser); } /** * Test reading non standard conforming XML. * Test reading non-standard conforming XML. * * XML file with lots of different white spaces before the starting tag. */ public function testReadNonStandardXmlWhitespace(): void { $url = $this->path . '/tests/data/simple_xml_invalid_multi_whitespace.xml'; $this->configuration['urls'][0] = $url; $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $this->assertResults($this->expected, $parser); $this->assertResults($this->expected, $this->getParser()); } /** * Test reading non standard conforming XML . * Test reading non-standard conforming XML . * * XML file with one empty line before the starting tag. */ public function testReadNonStandardXml2(): void { $url = $this->path . '/tests/data/simple_xml_invalid_single_line.xml'; $this->configuration['urls'][0] = $url; $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $this->assertResults($this->expected, $parser); $this->assertResults($this->expected, $this->getParser()); } /** * Test reading broken XML (missing closing tag). * * @throws \Drupal\Migrate\MigrateException */ public function testReadBrokenXmlMissingTag(): void { $url = $this->path . '/tests/data/simple_xml_broken_missing_tag.xml'; Loading @@ -151,14 +45,11 @@ final class SimpleXmlTest extends KernelTestBase { $this->expectException(MigrateException::class); // Newer versions of libxml mark it as an error 76, older ones as 73. $this->expectExceptionMessageMatches('/^Fatal Error 7[0-9]/'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Test reading broken XML (tag mismatch). * * @throws \Drupal\Migrate\MigrateException */ public function testReadBrokenXmlTagMismatch(): void { $url = $this->path . '/tests/data/simple_xml_broken_tag_mismatch.xml'; Loading @@ -166,15 +57,11 @@ final class SimpleXmlTest extends KernelTestBase { $this->expectException(MigrateException::class); $this->expectExceptionMessageMatches('/^Fatal Error 76/'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Test reading non XML. * * @throws \Drupal\Migrate\MigrateException */ public function testReadNonXml(): void { $url = $this->path . '/tests/data/simple_xml_non_xml.xml'; Loading @@ -182,43 +69,26 @@ final class SimpleXmlTest extends KernelTestBase { $this->expectException(MigrateException::class); $this->expectExceptionMessageMatches('/^Fatal Error 46/'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Tests reading non-existing XML. * * @throws \Drupal\Migrate\MigrateException */ public function testReadNonExistingXml(): void { $url = $this->path . '/tests/data/simple_xml_non_existing.xml'; $url = $this->path . '/tests/data/xml_non_existing.xml'; $this->configuration['urls'][0] = $url; $this->expectException(MigrateException::class); $this->expectExceptionMessage('file parser plugin: could not retrieve data from'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Parses and asserts the results match expectations. * * @param array|string $expected * The expected results. * @param \Traversable $parser * An iterable data result to parse. * {@inheritdoc} */ protected function assertResults($expected, \Traversable $parser): void { $data = []; foreach ($parser as $item) { $values = []; foreach ($item['values'] as $value) { $values[] = (string) $value; } $data[] = $values; } $this->assertEquals($expected, $data); protected function getParser(): DataParserPluginInterface { return $this->pluginManager->createInstance('simple_xml', $this->configuration); } } Loading
tests/data/simple_xml_reduce_single_value.xml→tests/data/xml_reduce_single_value.xml File moved. View file
tests/src/Kernel/Plugin/migrate_plus/data_parser/BaseXmlTest.php +174 Changes for tests/src/Kernel/Plugin/migrate_plus/data_parser/BaseXmlTest.php: 174 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types = 1); namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser; use Drupal\KernelTests\KernelTestBase; use Drupal\migrate_plus\DataParserPluginInterface; use Drupal\migrate_plus\DataParserPluginManager; /** * Test of the data_parser SimpleXml migrate_plus plugin. * * @group migrate_plus */ abstract class BaseXmlTest extends KernelTestBase { /** * {@inheritdoc} */ protected static $modules = ['migrate', 'migrate_plus']; /** * Path for the xml file. */ protected ?string $path; /** * The plugin manager. */ protected ?DataParserPluginManager $pluginManager = NULL; /** * The plugin configuration. */ protected ?array $configuration; /** * The expected result. */ protected ?array $expected; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->path = $this->container->get('module_handler') ->getModule('migrate_plus')->getPath(); $this->pluginManager = $this->container ->get('plugin.manager.migrate_plus.data_parser'); $this->configuration = [ 'plugin' => 'url', 'data_fetcher_plugin' => 'file', 'data_parser_plugin' => 'simple_xml', 'destination' => 'node', 'urls' => [], 'ids' => ['id' => ['type' => 'integer']], 'fields' => [ [ 'name' => 'id', 'label' => 'Id', 'selector' => '@id', ], [ 'name' => 'values', 'label' => 'Values', 'selector' => 'values', ], ], 'item_selector' => '/items/item', ]; $this->expected = [ [ 'Value 1', 'Value 2', ], [ 'Value 1 (single)', ], ]; } /** * Tests current URL of parsed XML item. */ public function testCurrentUrl(): void { $urls = [ $this->path . '/tests/data/xml_current_url1.xml', $this->path . '/tests/data/xml_current_url2.xml', ]; $this->configuration['urls'] = $urls; $parser = $this->getParser(); // First 2 items available in the first URL. $parser->rewind(); $this->assertEquals($urls[0], $parser->currentUrl()); $parser->next(); $this->assertEquals($urls[0], $parser->currentUrl()); // Third item available in the second URL. $parser->next(); $this->assertEquals($urls[1], $parser->currentUrl()); } /** * Tests reducing single values. */ public function testReduceSingleValue(): void { $url = $this->path . '/tests/data/xml_reduce_single_value.xml'; $this->configuration['urls'][0] = $url; $this->assertResults($this->expected, $this->getParser()); } /** * Tests retrieving single value from element with attributes. */ public function testSingleValueWithAttributes() { $urls = [ $this->path . '/tests/data/xml_data_parser.xml', ]; $this->configuration['urls'] = $urls; $this->configuration['item_selector'] = '/persons/person'; $this->configuration['fields'] = [ [ 'name' => 'id', 'label' => 'Id', 'selector' => 'id', ], [ 'name' => 'child', 'label' => 'child', 'selector' => 'children/child', ], ]; $names = []; foreach ($this->getParser() as $item) { $names[] = (string) $item['child']->name; } $expected_names = ['Elizabeth Junior', 'George Junior', 'Lucy']; $this->assertEquals($expected_names, $names); } /** * Parses and asserts the results match expectations. * * @param array|string $expected * The expected results. * @param \Traversable $parser * An iterable data result to parse. */ protected function assertResults($expected, \Traversable $parser): void { $data = []; foreach ($parser as $item) { $values = []; foreach ($item['values'] as $value) { $values[] = (string) $value; } $data[] = $values; } $this->assertEquals($expected, $data); } /** * Returns a parse object with active configuration. * * @return \Drupal\migrate_plus\DataParserPluginInterface * Data parser object. */ abstract protected function getParser(): DataParserPluginInterface; }
tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php +14 −144 Changes for tests/src/Kernel/Plugin/migrate_plus/data_parser/SimpleXmlTest.php: 14 added lines, 144 removed lines. Original line number Diff line number Diff line Loading @@ -4,146 +4,40 @@ declare(strict_types = 1); namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_parser; use Drupal\KernelTests\KernelTestBase; use Drupal\migrate\MigrateException; use Drupal\migrate_plus\DataParserPluginManager; use Drupal\migrate_plus\DataParserPluginInterface; /** * Test of the data_parser SimpleXml migrate_plus plugin. * * @group migrate_plus */ final class SimpleXmlTest extends KernelTestBase { class SimpleXmlTest extends BaseXmlTest { /** * {@inheritdoc} */ protected static $modules = ['migrate', 'migrate_plus']; /** * Path for the xml file. */ protected ?string $path; /** * The plugin manager. */ protected ?DataParserPluginManager $pluginManager = NULL; /** * The plugin configuration. */ protected ?array $configuration; /** * The expected result. */ protected ?array $expected; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->path = $this->container->get('module_handler') ->getModule('migrate_plus')->getPath(); $this->pluginManager = $this->container ->get('plugin.manager.migrate_plus.data_parser'); $this->configuration = [ 'plugin' => 'url', 'data_fetcher_plugin' => 'file', 'data_parser_plugin' => 'simple_xml', 'destination' => 'node', 'urls' => [], 'ids' => ['id' => ['type' => 'integer']], 'fields' => [ [ 'name' => 'id', 'label' => 'Id', 'selector' => '@id', ], [ 'name' => 'values', 'label' => 'Values', 'selector' => 'values', ], ], 'item_selector' => '/items/item', ]; $this->expected = [ [ 'Value 1', 'Value 2', ], [ 'Value 1 (single)', ], ]; } /** * Tests current URL of parsed XML item. */ public function testCurrentUrl(): void { $urls = [ $this->path . '/tests/data/simple_xml_current_url1.xml', $this->path . '/tests/data/simple_xml_current_url2.xml', ]; $this->configuration['urls'] = $urls; /** @var \Drupal\migrate_plus\DataParserPluginInterface $parser */ $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); // First 2 items available in the first URL. $parser->rewind(); $this->assertEquals($urls[0], $parser->currentUrl()); $parser->next(); $this->assertEquals($urls[0], $parser->currentUrl()); // Third item available in the second URL. $parser->next(); $this->assertEquals($urls[1], $parser->currentUrl()); } /** * Tests reducing single values. */ public function testReduceSingleValue(): void { $url = $this->path . '/tests/data/simple_xml_reduce_single_value.xml'; $this->configuration['urls'][0] = $url; $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $this->assertResults($this->expected, $parser); } /** * Test reading non standard conforming XML. * Test reading non-standard conforming XML. * * XML file with lots of different white spaces before the starting tag. */ public function testReadNonStandardXmlWhitespace(): void { $url = $this->path . '/tests/data/simple_xml_invalid_multi_whitespace.xml'; $this->configuration['urls'][0] = $url; $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $this->assertResults($this->expected, $parser); $this->assertResults($this->expected, $this->getParser()); } /** * Test reading non standard conforming XML . * Test reading non-standard conforming XML . * * XML file with one empty line before the starting tag. */ public function testReadNonStandardXml2(): void { $url = $this->path . '/tests/data/simple_xml_invalid_single_line.xml'; $this->configuration['urls'][0] = $url; $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $this->assertResults($this->expected, $parser); $this->assertResults($this->expected, $this->getParser()); } /** * Test reading broken XML (missing closing tag). * * @throws \Drupal\Migrate\MigrateException */ public function testReadBrokenXmlMissingTag(): void { $url = $this->path . '/tests/data/simple_xml_broken_missing_tag.xml'; Loading @@ -151,14 +45,11 @@ final class SimpleXmlTest extends KernelTestBase { $this->expectException(MigrateException::class); // Newer versions of libxml mark it as an error 76, older ones as 73. $this->expectExceptionMessageMatches('/^Fatal Error 7[0-9]/'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Test reading broken XML (tag mismatch). * * @throws \Drupal\Migrate\MigrateException */ public function testReadBrokenXmlTagMismatch(): void { $url = $this->path . '/tests/data/simple_xml_broken_tag_mismatch.xml'; Loading @@ -166,15 +57,11 @@ final class SimpleXmlTest extends KernelTestBase { $this->expectException(MigrateException::class); $this->expectExceptionMessageMatches('/^Fatal Error 76/'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Test reading non XML. * * @throws \Drupal\Migrate\MigrateException */ public function testReadNonXml(): void { $url = $this->path . '/tests/data/simple_xml_non_xml.xml'; Loading @@ -182,43 +69,26 @@ final class SimpleXmlTest extends KernelTestBase { $this->expectException(MigrateException::class); $this->expectExceptionMessageMatches('/^Fatal Error 46/'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Tests reading non-existing XML. * * @throws \Drupal\Migrate\MigrateException */ public function testReadNonExistingXml(): void { $url = $this->path . '/tests/data/simple_xml_non_existing.xml'; $url = $this->path . '/tests/data/xml_non_existing.xml'; $this->configuration['urls'][0] = $url; $this->expectException(MigrateException::class); $this->expectExceptionMessage('file parser plugin: could not retrieve data from'); $parser = $this->pluginManager->createInstance('simple_xml', $this->configuration); $parser->next(); $this->getParser()->next(); } /** * Parses and asserts the results match expectations. * * @param array|string $expected * The expected results. * @param \Traversable $parser * An iterable data result to parse. * {@inheritdoc} */ protected function assertResults($expected, \Traversable $parser): void { $data = []; foreach ($parser as $item) { $values = []; foreach ($item['values'] as $value) { $values[] = (string) $value; } $data[] = $values; } $this->assertEquals($expected, $data); protected function getParser(): DataParserPluginInterface { return $this->pluginManager->createInstance('simple_xml', $this->configuration); } }