Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fixtures generation #51

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 47 additions & 46 deletions DependencyInjection/Configuration/FixtureGeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function getConfigTreeBuilder()
->isRequired()
->children()
->arrayNode('locales')->end()
->arrayNode('association_types')
->children()
->integerNode('count')->min(1)->defaultValue(1)->end()
->end()
->end()
->arrayNode('group_types')->end()
->arrayNode('associations')
->children()
Expand All @@ -40,10 +45,10 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('code')->isRequired()->end()
->scalarNode('label')->isRequired()->end()
->scalarNode('label')->end()
->arrayNode('locales')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->end()
->arrayNode('currencies')
Expand All @@ -53,45 +58,23 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('users')
->arrayNode('user_roles')
->useAttributeAsKey('name')
->defaultValue([
"admin" => [
"username" => "admin",
"password" => "admin",
"email" => "[email protected]",
"firstname" => "Peter",
"lastname" => "Doe",
"roles" => [ "ROLE_ADMINISTRATOR" ],
"groups" => [ "IT support" ],
"enable" => true
'ROLE_ADMINISTRATOR' => [
'label' => 'Administrator'
]
])
->prototype('array')
->children()
->scalarNode('username')->isRequired()->end()
->scalarNode('password')->isRequired()->end()
->scalarNode('email')->isRequired()->end()
->scalarNode('firstname')->isRequired()->end()
->scalarNode('lastname')->isRequired()->end()
->scalarNode('catalog_locale')->end()
->scalarNode('catalog_scope')->end()
->scalarNode('default_tree')->end()
->arrayNode('roles')
->isRequired()
->prototype('scalar')->end()
->end()
->arrayNode('groups')
->isRequired()
->prototype('scalar')->end()
->end()
->booleanNode('enable')->defaultFalse()->end()
->scalarNode('label')->isRequired()->end()
->end()
->end()
->end()
->arrayNode('user_groups')
->defaultValue([
"it_support" => [
"name" => "IT support"
'it_support' => [
'name' => 'IT support'
]
])
->prototype('array')
Expand All @@ -100,16 +83,39 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('user_roles')
->useAttributeAsKey('name')
->arrayNode('users')
->defaultValue([
"ROLE_ADMINISTRATOR" => [
"label" => "Administrator"
'admin' => [
'username' => 'admin',
'password' => 'admin',
'email' => '[email protected]',
'firstname' => 'Peter',
'lastname' => 'Doe',
'roles' => ['ROLE_ADMINISTRATOR'],
'groups' => ['IT support'],
'enable' => true,
'default_tree' => 'master',
]
])
->prototype('array')
->children()
->scalarNode('label')->isRequired()->end()
->scalarNode('username')->isRequired()->end()
->scalarNode('password')->isRequired()->end()
->scalarNode('email')->isRequired()->end()
->scalarNode('firstname')->isRequired()->end()
->scalarNode('lastname')->isRequired()->end()
->scalarNode('catalog_locale')->end()
->scalarNode('catalog_scope')->end()
->scalarNode('default_tree')->end()
->arrayNode('roles')
->isRequired()
->prototype('scalar')->end()
->end()
->arrayNode('groups')
->isRequired()
->prototype('scalar')->end()
->end()
->booleanNode('enable')->defaultFalse()->end()
->end()
->end()
->end()
Expand All @@ -124,7 +130,6 @@ public function getConfigTreeBuilder()
->arrayNode('configuration')
->children()
->booleanNode('uploadAllowed')->end()
->scalarNode('delimiter')->end()
->scalarNode('enclosure')->end()
->scalarNode('escape')->end()
->booleanNode('enabled')->end()
Expand Down Expand Up @@ -161,21 +166,19 @@ public function getConfigTreeBuilder()
->end()
->arrayNode('categories')
->children()
->integerNode('count')->min(1)->isRequired()->end()
->integerNode('count')->min(1)->defaultValue(1)->end()
->integerNode('levels')->min(1)->defaultValue(1)->end()
->scalarNode('delimiter')->defaultValue(';')->end()
->end()
->end()
->arrayNode('attribute_groups')
->children()
->integerNode('count')->min(1)->isRequired()->end()
->integerNode('count')->min(1)->defaultValue(1)->end()
->end()
->end()
->arrayNode('attributes')
->children()
->integerNode('count')->min(1)->isRequired()->end()
->scalarNode('identifier_attribute')->isRequired()->end()
->scalarNode('delimiter')->defaultValue(';')->end()
->floatNode('localizable_probability')->defaultValue(10)->end()
->floatNode('scopable_probability')->defaultValue(5)->end()
->floatNode('localizable_and_scopable_probability')->defaultValue(2)->end()
Expand All @@ -189,7 +192,6 @@ public function getConfigTreeBuilder()
->arrayNode('attribute_options')
->children()
->integerNode('count_per_attribute')->min(1)->isRequired()->end()
->scalarNode('delimiter')->defaultValue(';')->end()
->end()
->end()
->arrayNode('families')
Expand All @@ -199,14 +201,13 @@ public function getConfigTreeBuilder()
->scalarNode('identifier_attribute')->isRequired()->end()
->scalarNode('label_attribute')->isRequired()->end()
->integerNode('requirements_count')->min(1)->defaultValue(5)->end()
->scalarNode('delimiter')->defaultValue(';')->end()
->end()
->end()
->arrayNode('variant_groups')
->children()
->integerNode('count')->min(0)->isRequired()->end()
->integerNode('axes_count')->min(1)->isRequired()->end()
->integerNode('attributes_count')->min(0)->isRequired()->end()
->integerNode('count')->min(0)->defaultValue(0)->end()
->integerNode('axes_count')->min(1)->defaultValue(1)->end()
->integerNode('attributes_count')->min(0)->defaultValue(0)->end()
->end()
->end()
->arrayNode('asset_categories')->end()
Expand Down
83 changes: 50 additions & 33 deletions Generator/AttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public function __construct(
*/
public function generate(array $globalConfig, array $entitiesConfig, ProgressBar $progress, array $options = [])
{
$this->locales = $options['locales'];
$this->attributeGroups = $options['attribute_groups'];
$this->locales = $options['locales'] ?? [];
$this->attributeGroups = $options['attribute_groups'] ?? [];

$count = (int) $entitiesConfig['count'];

$localizableProbability = (float) $entitiesConfig['localizable_probability'];
$scopableProbability = (float) $entitiesConfig['scopable_probability'];
$locScopableProbability = (float) $entitiesConfig['localizable_and_scopable_probability'];
$gridFilterProbability = (float) $entitiesConfig['useable_as_grid_filter_probability'];
$minVariantAxes = (int) $entitiesConfig['min_variant_axes'];
$minVariantAttributes = (int) $entitiesConfig['min_variant_attributes'];
$minVariantAxes = (int) ($entitiesConfig['min_variant_axes'] ?? 0);
$minVariantAttributes = (int) ($entitiesConfig['min_variant_attributes'] ?? 0);
$identifier = $entitiesConfig['identifier_attribute'];

$this->faker = Factory::create();
Expand All @@ -89,19 +89,23 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar

$this->attributes[$identifier] = [
'code' => $identifier,
'type' => 'pim_catalog_identifier',
'type' => AttributeTypes::IDENTIFIER,
'group' => $this->getRandomAttributeGroupCode(),
'useable_as_grid_filter' => 1,
'scopable' => 0,
'localizable' => 0,
];

$forceAttributes = $entitiesConfig['force_attributes'];

foreach ($forceAttributes as $forceAttribute) {
list($code, $type) = explode('=', $forceAttribute);
$this->attributes[trim($code)] = [
'code' => trim($code),
'type' => trim($type),
'group' => $this->getRandomAttributeGroupCode(),
'code' => trim($code),
'type' => trim($type),
'group' => $this->getRandomAttributeGroupCode(),
'scopable' => 0,
'localizable' => 0,
];
}

Expand Down Expand Up @@ -131,12 +135,40 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar
$attribute['scopable'] = (int) $this->faker->boolean($scopableProbability);
}

if ('pim_catalog_metric' === $type) {
if (AttributeTypes::METRIC === $type) {
$attribute = array_merge($attribute, $this->getMetricProperties());
}

if ('pim_catalog_image' === $type || 'pim_catalog_file' === $type) {
$attribute = array_merge($attribute, $this->getMediaProperties());
if (AttributeTypes::IMAGE === $type) {
$attribute['allowed_extensions'] = implode(
',',
$this->faker->randomElements(['gif', 'jpeg', 'jpg', 'png', 'tif', 'tiff'], 2)
);
}

if (AttributeTypes::FILE === $type) {
$attribute['allowed_extensions'] = implode(
',',
$this->faker->randomElements(['csv', 'doc', 'docx', 'mp3', 'pdf', 'ppt', 'pptx', 'rtf', 'txt'], 2)
);
}

if (in_array(
$attribute['type'],
[AttributeTypes::METRIC, AttributeTypes::NUMBER, AttributeTypes::PRICE_COLLECTION]
)) {
$attribute['decimals_allowed'] = (int) $this->faker->boolean(50);
}

if (in_array(
$attribute['type'],
[AttributeTypes::METRIC, AttributeTypes::NUMBER]
)) {
$attribute['negative_allowed'] = (int) $this->faker->boolean(50);
}

if (AttributeTypes::TEXTAREA === $attribute['type']) {
$attribute['wysiwyg_enabled'] = (int) $this->faker->boolean(50);
}

$this->attributes[$attribute['code']] = $attribute;
Expand Down Expand Up @@ -226,13 +258,13 @@ protected function getRandomAttributeType()
{
$attributeType = null;
$typesToAvoid = [
'pim_catalog_identifier',
'pim_reference_data_multiselect',
'pim_reference_data_simpleselect',
AttributeTypes::IDENTIFIER,
AttributeTypes::REFERENCE_DATA_MULTI_SELECT,
AttributeTypes::REFERENCE_DATA_SIMPLE_SELECT,
'pim_assets_collection',
];

while ((null === $attributeType) || in_array($attributeType, $typesToAvoid)) {
while (null === $attributeType || in_array($attributeType, $typesToAvoid)) {
$attributeType = $this->faker->randomElement($this->getAttributeTypeCodes());
}

Expand Down Expand Up @@ -291,7 +323,7 @@ protected function getLocalizedRandomLabels($type)
$smallType = str_replace('pim_catalog_', '', $type);

foreach ($this->locales as $locale) {
$labels[$locale->getCode()] = sprintf("%s %s", $smallType, implode(' ', $this->faker->words(2)));
$labels[$locale->getCode()] = sprintf('%s %s', $smallType, implode(' ', $this->faker->words(2)));
}

return $labels;
Expand All @@ -305,23 +337,8 @@ protected function getLocalizedRandomLabels($type)
protected function getMetricProperties()
{
return [
"metric_family" => "Length",
"default_metric_unit" => "METER"
];
}

/**
* Provide media specific properties
*
* @return array
*/
protected function getMediaProperties()
{
return [
'allowed_extensions' => implode(
',',
$this->faker->randomElements(['png', 'jpg', 'pdf'], 2)
)
'metric_family' => 'Length',
'default_metric_unit' => 'METER',
];
}

Expand Down
6 changes: 3 additions & 3 deletions Generator/AttributeOptionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\Yaml;

/**
* Generate native YML file for attribute option useable as fixtures
* Generate native YML file for attribute option usable as fixtures
*
* @author Claire Fortin <[email protected]>
* @copyright 2015 Akeneo SAS (http://www.akeneo.com)
Expand Down Expand Up @@ -57,8 +57,8 @@ public function __construct(CsvWriter $writer)
*/
public function generate(array $globalConfig, array $entitiesConfig, ProgressBar $progress, array $options = [])
{
$this->locales = $options['locales'];
$this->attributes = $options['attributes'];
$this->locales = $options['locales'] ?? [];
$this->attributes = $options['attributes'] ?? [];

$this->faker = Factory::create();
if (isset($globalConfig['seed'])) {
Expand Down
10 changes: 6 additions & 4 deletions Generator/ChannelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,14 @@ protected function generateChannels(array $channelsConfig)
{
$channels = [];

foreach ($channelsConfig as $channelConfig) {
foreach ($channelsConfig as $channelCode => $channelConfig) {
$channel = new Channel();

$channel->setCode($channelConfig['code']);
$channel->setLabel($channelConfig['label']);
$channel->setCode($channelCode);

if (isset($channelConfig['label'])) {
$channel->setLabel($channelConfig['label']);
}

foreach ($channelConfig['locales'] as $localeCode) {
$locale = $this->locales[$localeCode];
Expand Down Expand Up @@ -244,7 +247,6 @@ protected function writeChannelsFile()

$data[] = [
'code' => $channel->getCode(),
'label' => $channel->getLabel(),
'tree' => self::DEFAULT_TREE,
'locales' => implode(',', $localeCodes),
'currencies' => implode(',', $currencyCodes),
Expand Down
Loading