Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SchemaXml.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class SchemaXml
15 {
21  protected $objectManager;
22 
28  protected $fieldsProvider;
29 
35  protected $dom;
36 
42  protected $requiredFields = [
43  'name',
44  'entity_type',
45  'collection',
46  ];
47 
53  {
54  $this->objectManager = $objectManager;
55  $this->fieldsProvider = $this->objectManager->create(\Magento\Mtf\Util\Generate\Fixture\FieldsProvider::class);
56  $this->dom = new \DOMDocument('1.0');
57  $this->dom->load(dirname(__FILE__) . '/template.xml');
58  $this->dom->preserveWhiteSpace = false;
59  $this->dom->formatOutput = true;
60  }
61 
67  public function launch()
68  {
69  $options = getopt('', ['type:', 'name:', 'entity_type:', 'collection:', 'help']);
70  $checkKeyExists = count(array_diff($this->requiredFields, array_keys($options)));
71 
72  if (empty($options) || isset($options['help']) || $checkKeyExists > 0) {
73  $this->getHelp();
74  }
75  $config['type'] = empty($options['type']) ? 'flat' : $options['type'];
76  if ($config['type'] === 'composite') {
77  $options['entities'] = explode(',', $options['entity_type']);
78  unset($options['entity_type']);
79  }
80  $config = array_merge($config, $options);
81 
82  $this->generate($config);
83  }
84 
91  public function generate(array $config)
92  {
93  if (!$this->fieldsProvider->checkConnection()) {
94  return;
95  }
96 
97  $this->generateFixtureXml($config);
98  }
99 
106  protected function generateFixtureXml(array $config)
107  {
108  $classShortName = ucfirst($config['name']);
109  $fileName = $classShortName . '.xml';
110  $collection = explode('\\', $config['collection']);
111  $collection = array_values(array_filter($collection));
112  $path = $collection[0] . '\\' . $collection[1] . '\Test\Fixture\\';
113  $module = $collection[0] . '_' . $collection[1];
114  $repositoryClass = $collection[0] . '\\' . $collection[1] . '\Test\Repository\\' . $classShortName;
115  $handlerInterface = $collection[0] . '\\' . $collection[1] . '\Test\Handler\\';
116  $handlerInterface .= $classShortName . '\\' . $classShortName . 'Interface';
117  $fixtureClass = $path . $classShortName;
118  $folderName = MTF_TESTS_PATH . $path;
119  $pathToFile = str_replace('\\', DIRECTORY_SEPARATOR, $folderName . $fileName);
120  if (file_exists($pathToFile)) {
121  echo "Fixture with name ($pathToFile) already exists.\n";
122  return;
123  }
124  if (!is_dir($folderName)) {
125  mkdir($folderName, 0777, true);
126  }
127 
129  $root = $this->dom->getElementsByTagName('config')->item(0);
130 
131  $fixture = $this->dom->createElement('fixture');
132  $fixture->setAttribute('name', $config['name']);
133  $fixture->setAttribute('module', $module);
134  $fixture->setAttribute('type', $config['type']);
135  $fixture->setAttribute('collection', implode('\\', $collection));
136  $fixture->setAttribute('repository_class', $repositoryClass);
137  $fixture->setAttribute('handler_interface', $handlerInterface);
138  $fixture->setAttribute('class', $fixtureClass);
139  if (isset($config['entity_type'])) {
140  $fixture->setAttribute('entity_type', $config['entity_type']);
141  }
142  $root->appendChild($fixture);
143 
144  $fields = $this->fieldsProvider->getFields($config);
145  foreach ($fields as $fieldName => $fieldValue) {
146  $field = $this->dom->createElement('field');
147  $field->setAttribute('name', $fieldName);
148  $field->setAttribute('is_required', intval($fieldValue['is_required']));
149  $fixture->appendChild($field);
150  }
151 
152  file_put_contents($pathToFile, str_replace(' ', ' ', $this->dom->saveXML()));
153  }
154 
160  protected function getHelp()
161  {
162  echo <<<TAG
163 Usage: Magento 2 fixture schema generator.
164 
165  --type\t\t<flat>|<eav>|<table>|<composite>\t\tTable type for the entity\tDefault: flat
166  --name\t\t<className>\t\t\t\t\tName of generated class
167  --entity_type\t<entity_type>|<entity_type1,entity_type2>\tDatabase table name where entity data is stored
168  --collection\t<path\\\\to\\\\collection>\t\t\t\tCollection to generate data sets\tNOTE: All backslashes must be escaped
169  --help\t\tThis help
170 
171  name, entity_type, collection - required fields
172 
173 TAG;
174  exit(0);
175  }
176 }
$config
Definition: fraud_order.php:17
defined('MTF_BOOT_FILE')||define('MTF_BOOT_FILE' __FILE__
Definition: bootstrap.php:7
$fields
Definition: details.phtml:14
$fileName
Definition: translate.phtml:15
exit
Definition: redirect.phtml:12
const MTF_TESTS_PATH
Definition: bootstrap.php:11
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
__construct(ObjectManagerInterface $objectManager)
Definition: SchemaXml.php:52