Skip to content

Commit d3fb588

Browse files
author
Aponasenko, Dmytro(daponasenko)
committed
Merge pull request #398 from magento-qmt/develop
[Mavericks] Functional tests maintenance
2 parents f7ad6f5 + 8dd9c0f commit d3fb588

File tree

83 files changed

+2345
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2345
-187
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Mtf\Util\Generate\Fixture;
8+
9+
use Magento\Framework\App\Resource;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Eav\Model\Config;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
14+
/**
15+
* Provider of fields from database.
16+
*/
17+
class FieldsProvider
18+
{
19+
/**
20+
* EAV configuration.
21+
*
22+
* @var Config
23+
*/
24+
protected $eavConfig;
25+
26+
/**
27+
* Resources and connections registry and factory.
28+
*
29+
* @var Resource
30+
*/
31+
protected $resource;
32+
33+
/**
34+
* Magento connection.
35+
*
36+
* @var AdapterInterface
37+
*/
38+
protected $connection;
39+
40+
/**
41+
* @constructor
42+
* @param ObjectManagerInterface $objectManager
43+
*/
44+
public function __construct(ObjectManagerInterface $objectManager)
45+
{
46+
$this->eavConfig = $objectManager->create('Magento\Eav\Model\Config');
47+
$this->resource = $objectManager->create('Magento\Framework\App\Resource');
48+
}
49+
50+
/**
51+
* Check connection to DB.
52+
*
53+
* @return bool
54+
*/
55+
public function checkConnection()
56+
{
57+
$this->connection = $this->getConnection('core_write');
58+
if (!$this->connection || $this->connection instanceof \Zend_Db_Adapter_Exception) {
59+
echo ('Connection to Magento 2 database is absent. Fixture data has not been fetched.' . PHP_EOL);
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
66+
/**
67+
* Collect fields for the entity based on its type.
68+
*
69+
* @param array $fixture
70+
* @return array
71+
*/
72+
public function getFields(array $fixture)
73+
{
74+
$method = $fixture['type'] . 'CollectFields';
75+
if (!method_exists($this, $method)) {
76+
return [];
77+
}
78+
79+
return $this->$method($fixture);
80+
}
81+
82+
/**
83+
* Collect fields for the entity with eav type.
84+
*
85+
* @param array $fixture
86+
* @return array
87+
*/
88+
protected function eavCollectFields(array $fixture)
89+
{
90+
$entityType = $fixture['entity_type'];
91+
$collection = $this->eavConfig->getEntityType($entityType)->getAttributeCollection();
92+
$attributes = [];
93+
foreach ($collection as $attribute) {
94+
if (isset($fixture['product_type'])) {
95+
$applyTo = $attribute->getApplyTo();
96+
if (!empty($applyTo) && !in_array($fixture['product_type'], $applyTo)) {
97+
continue;
98+
}
99+
}
100+
/** @var $attribute \Magento\Eav\Model\Entity\Attribute */
101+
$code = $attribute->getAttributeCode();
102+
$attributes[$code] = [
103+
'attribute_code' => $code,
104+
'backend_type' => $attribute->getBackendType(),
105+
'is_required' => $attribute->getIsRequired(),
106+
'default_value' => $attribute->getDefaultValue(),
107+
'input' => $attribute->getFrontendInput(),
108+
];
109+
}
110+
111+
return $attributes;
112+
}
113+
114+
/**
115+
* Collect fields for the entity with table type.
116+
*
117+
* @param array $fixture
118+
* @return array
119+
*/
120+
protected function tableCollectFields(array $fixture)
121+
{
122+
return $this->flatCollectFields($fixture);
123+
}
124+
125+
/**
126+
* Collect fields for the entity with flat type.
127+
*
128+
* @param array $fixture
129+
* @return array
130+
*/
131+
protected function flatCollectFields(array $fixture)
132+
{
133+
$entityType = $fixture['entity_type'];
134+
135+
/** @var $connection \Magento\Framework\DB\Adapter\AdapterInterface */
136+
$fields = $this->connection->describeTable($entityType);
137+
138+
$attributes = [];
139+
foreach ($fields as $code => $field) {
140+
$attributes[$code] = [
141+
'attribute_code' => $code,
142+
'backend_type' => $field['DATA_TYPE'],
143+
'is_required' => ($field['PRIMARY'] || $field['IDENTITY']),
144+
'default_value' => $field['DEFAULT'],
145+
'input' => '',
146+
];
147+
}
148+
149+
return $attributes;
150+
}
151+
152+
/**
153+
* Collect fields for the entity with composite type.
154+
*
155+
* @param array $fixture
156+
* @return array
157+
*/
158+
protected function compositeCollectFields(array $fixture)
159+
{
160+
$entityTypes = $fixture['entities'];
161+
162+
$fields = [];
163+
foreach ($entityTypes as $entityType) {
164+
$fields = array_merge($fields, $this->connection->describeTable($entityType));
165+
}
166+
167+
$attributes = [];
168+
foreach ($fields as $code => $field) {
169+
$attributes[$code] = [
170+
'attribute_code' => $code,
171+
'backend_type' => $field['DATA_TYPE'],
172+
'is_required' => ($field['PRIMARY'] || $field['IDENTITY']),
173+
'default_value' => $field['DEFAULT'],
174+
'input' => '',
175+
];
176+
}
177+
178+
return $attributes;
179+
}
180+
181+
/**
182+
* Retrieve connection to resource specified by $resourceName.
183+
*
184+
* @param string $resourceName
185+
* @return \Exception|false|\Magento\Framework\DB\Adapter\AdapterInterface|\Zend_Exception
186+
*/
187+
protected function getConnection($resourceName)
188+
{
189+
try {
190+
$connection = $this->resource->getConnection($resourceName);
191+
return $connection;
192+
} catch (\Zend_Exception $e) {
193+
echo $e->getMessage() . PHP_EOL;
194+
return $e;
195+
}
196+
}
197+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Mtf\Util\Generate\Fixture;
8+
9+
use Magento\Framework\ObjectManagerInterface;
10+
11+
/**
12+
* Fixture files generator.
13+
*/
14+
class SchemaXml
15+
{
16+
/**
17+
* Object manager instance.
18+
*
19+
* @var ObjectManagerInterface
20+
*/
21+
protected $objectManager;
22+
23+
/**
24+
* Provider of fields from database.
25+
*
26+
* @var FieldsProvider
27+
*/
28+
protected $fieldsProvider;
29+
30+
/**
31+
* The DOMDocument class represents an entire XML.
32+
*
33+
* @var \DOMDocument
34+
*/
35+
protected $dom;
36+
37+
/**
38+
* Required fields list.
39+
*
40+
* @var array
41+
*/
42+
protected $requiredFields = [
43+
'name',
44+
'entity_type',
45+
'collection',
46+
];
47+
48+
/**
49+
* @constructor
50+
* @param ObjectManagerInterface $objectManager
51+
*/
52+
public function __construct(ObjectManagerInterface $objectManager)
53+
{
54+
$this->objectManager = $objectManager;
55+
$this->fieldsProvider = $this->objectManager->create('Magento\Mtf\Util\Generate\Fixture\FieldsProvider');
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+
62+
/**
63+
* Launch Fixture generators.
64+
*
65+
* @return void
66+
*/
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+
85+
/**
86+
* Generate Fixtures XML.
87+
*
88+
* @param array $config
89+
* @return void
90+
*/
91+
public function generate(array $config)
92+
{
93+
if (!$this->fieldsProvider->checkConnection()) {
94+
return;
95+
}
96+
97+
$this->generateFixtureXml($config);
98+
}
99+
100+
/**
101+
* Generate fixtures XML definition files.
102+
*
103+
* @param array $config
104+
* @return void
105+
*/
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+
128+
/** @var \DOMElement $root */
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+
155+
/**
156+
* Prints help info and stops code execution.
157+
*
158+
* @SuppressWarnings(PHPMD)
159+
*/
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/**
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/mtf/etc/fixture.xsd" />

0 commit comments

Comments
 (0)