SchemaXml.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Util\Generate\Fixture;
  7. use Magento\Framework\ObjectManagerInterface;
  8. /**
  9. * Fixture files generator.
  10. */
  11. class SchemaXml
  12. {
  13. /**
  14. * Object manager instance.
  15. *
  16. * @var ObjectManagerInterface
  17. */
  18. protected $objectManager;
  19. /**
  20. * Provider of fields from database.
  21. *
  22. * @var FieldsProvider
  23. */
  24. protected $fieldsProvider;
  25. /**
  26. * The DOMDocument class represents an entire XML.
  27. *
  28. * @var \DOMDocument
  29. */
  30. protected $dom;
  31. /**
  32. * Required fields list.
  33. *
  34. * @var array
  35. */
  36. protected $requiredFields = [
  37. 'name',
  38. 'entity_type',
  39. 'collection',
  40. ];
  41. /**
  42. * @constructor
  43. * @param ObjectManagerInterface $objectManager
  44. */
  45. public function __construct(ObjectManagerInterface $objectManager)
  46. {
  47. $this->objectManager = $objectManager;
  48. $this->fieldsProvider = $this->objectManager->create(\Magento\Mtf\Util\Generate\Fixture\FieldsProvider::class);
  49. $this->dom = new \DOMDocument('1.0');
  50. $this->dom->load(dirname(__FILE__) . '/template.xml');
  51. $this->dom->preserveWhiteSpace = false;
  52. $this->dom->formatOutput = true;
  53. }
  54. /**
  55. * Launch Fixture generators.
  56. *
  57. * @return void
  58. */
  59. public function launch()
  60. {
  61. $options = getopt('', ['type:', 'name:', 'entity_type:', 'collection:', 'help']);
  62. $checkKeyExists = count(array_diff($this->requiredFields, array_keys($options)));
  63. if (empty($options) || isset($options['help']) || $checkKeyExists > 0) {
  64. $this->getHelp();
  65. }
  66. $config['type'] = empty($options['type']) ? 'flat' : $options['type'];
  67. if ($config['type'] === 'composite') {
  68. $options['entities'] = explode(',', $options['entity_type']);
  69. unset($options['entity_type']);
  70. }
  71. $config = array_merge($config, $options);
  72. $this->generate($config);
  73. }
  74. /**
  75. * Generate Fixtures XML.
  76. *
  77. * @param array $config
  78. * @return void
  79. */
  80. public function generate(array $config)
  81. {
  82. if (!$this->fieldsProvider->checkConnection()) {
  83. return;
  84. }
  85. $this->generateFixtureXml($config);
  86. }
  87. /**
  88. * Generate fixtures XML definition files.
  89. *
  90. * @param array $config
  91. * @return void
  92. */
  93. protected function generateFixtureXml(array $config)
  94. {
  95. $classShortName = ucfirst($config['name']);
  96. $fileName = $classShortName . '.xml';
  97. $collection = explode('\\', $config['collection']);
  98. $collection = array_values(array_filter($collection));
  99. $path = $collection[0] . '\\' . $collection[1] . '\Test\Fixture\\';
  100. $module = $collection[0] . '_' . $collection[1];
  101. $repositoryClass = $collection[0] . '\\' . $collection[1] . '\Test\Repository\\' . $classShortName;
  102. $handlerInterface = $collection[0] . '\\' . $collection[1] . '\Test\Handler\\';
  103. $handlerInterface .= $classShortName . '\\' . $classShortName . 'Interface';
  104. $fixtureClass = $path . $classShortName;
  105. $folderName = MTF_TESTS_PATH . $path;
  106. $pathToFile = str_replace('\\', DIRECTORY_SEPARATOR, $folderName . $fileName);
  107. if (file_exists($pathToFile)) {
  108. echo "Fixture with name ($pathToFile) already exists.\n";
  109. return;
  110. }
  111. if (!is_dir($folderName)) {
  112. mkdir($folderName, 0777, true);
  113. }
  114. /** @var \DOMElement $root */
  115. $root = $this->dom->getElementsByTagName('config')->item(0);
  116. $fixture = $this->dom->createElement('fixture');
  117. $fixture->setAttribute('name', $config['name']);
  118. $fixture->setAttribute('module', $module);
  119. $fixture->setAttribute('type', $config['type']);
  120. $fixture->setAttribute('collection', implode('\\', $collection));
  121. $fixture->setAttribute('repository_class', $repositoryClass);
  122. $fixture->setAttribute('handler_interface', $handlerInterface);
  123. $fixture->setAttribute('class', $fixtureClass);
  124. if (isset($config['entity_type'])) {
  125. $fixture->setAttribute('entity_type', $config['entity_type']);
  126. }
  127. $root->appendChild($fixture);
  128. $fields = $this->fieldsProvider->getFields($config);
  129. foreach ($fields as $fieldName => $fieldValue) {
  130. $field = $this->dom->createElement('field');
  131. $field->setAttribute('name', $fieldName);
  132. $field->setAttribute('is_required', (int)$fieldValue['is_required']);
  133. $fixture->appendChild($field);
  134. }
  135. file_put_contents($pathToFile, str_replace(' ', ' ', $this->dom->saveXML()));
  136. }
  137. /**
  138. * Prints help info and stops code execution.
  139. *
  140. * @SuppressWarnings(PHPMD)
  141. */
  142. protected function getHelp()
  143. {
  144. echo <<<TAG
  145. Usage: Magento 2 fixture schema generator.
  146. --type\t\t<flat>|<eav>|<table>|<composite>\t\tTable type for the entity\tDefault: flat
  147. --name\t\t<className>\t\t\t\t\tName of generated class
  148. --entity_type\t<entity_type>|<entity_type1,entity_type2>\tDatabase table name where entity data is stored
  149. --collection\t<path\\\\to\\\\collection>\t\t\t\tCollection to generate data sets\tNOTE: All backslashes must be escaped
  150. --help\t\tThis help
  151. name, entity_type, collection - required fields
  152. TAG;
  153. exit(0);
  154. }
  155. }