diff --git a/DependencyInjection/Configuration/FixtureGeneratorConfiguration.php b/DependencyInjection/Configuration/FixtureGeneratorConfiguration.php index 7297dc5..391fa2a 100644 --- a/DependencyInjection/Configuration/FixtureGeneratorConfiguration.php +++ b/DependencyInjection/Configuration/FixtureGeneratorConfiguration.php @@ -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() @@ -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') @@ -53,45 +58,23 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() - ->arrayNode('users') + ->arrayNode('user_roles') + ->useAttributeAsKey('name') ->defaultValue([ - "admin" => [ - "username" => "admin", - "password" => "admin", - "email" => "admin@example.com", - "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') @@ -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' => 'admin@example.com', + '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() @@ -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() @@ -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() @@ -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') @@ -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() diff --git a/Generator/AttributeGenerator.php b/Generator/AttributeGenerator.php index 5d27021..861cd98 100644 --- a/Generator/AttributeGenerator.php +++ b/Generator/AttributeGenerator.php @@ -67,8 +67,8 @@ 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']; @@ -76,8 +76,8 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar $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(); @@ -89,9 +89,11 @@ 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']; @@ -99,9 +101,11 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar 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, ]; } @@ -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; @@ -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()); } @@ -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; @@ -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', ]; } diff --git a/Generator/AttributeOptionGenerator.php b/Generator/AttributeOptionGenerator.php index 49afd1f..f1bb3d7 100644 --- a/Generator/AttributeOptionGenerator.php +++ b/Generator/AttributeOptionGenerator.php @@ -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 * @copyright 2015 Akeneo SAS (http://www.akeneo.com) @@ -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'])) { diff --git a/Generator/ChannelGenerator.php b/Generator/ChannelGenerator.php index 266dc86..80b903c 100644 --- a/Generator/ChannelGenerator.php +++ b/Generator/ChannelGenerator.php @@ -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]; @@ -244,7 +247,6 @@ protected function writeChannelsFile() $data[] = [ 'code' => $channel->getCode(), - 'label' => $channel->getLabel(), 'tree' => self::DEFAULT_TREE, 'locales' => implode(',', $localeCodes), 'currencies' => implode(',', $currencyCodes), diff --git a/Generator/FamilyGenerator.php b/Generator/FamilyGenerator.php index 8c32c10..5fa057c 100644 --- a/Generator/FamilyGenerator.php +++ b/Generator/FamilyGenerator.php @@ -44,9 +44,6 @@ class FamilyGenerator implements GeneratorInterface /** @var Generator */ protected $faker; - /** @var array */ - protected $families = []; - /** @var array */ protected $attributes = []; @@ -66,9 +63,9 @@ 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->channels = $options['channels']; + $this->locales = $options['locales'] ?? []; + $this->attributes = $options['attributes'] ?? []; + $this->channels = $options['channels'] ?? []; $count = (int) $entitiesConfig['count']; $attributesCount = (int) $entitiesConfig['attributes_count'] - 1; @@ -86,7 +83,7 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar for ($i = 0; $i < $count; $i++) { $family = []; - $family['code'] =self::FAMILY_CODE_PREFIX.$i; + $family['code'] = self::FAMILY_CODE_PREFIX.$i; foreach ($this->getLocalizedRandomLabels() as $localeCode => $label) { $family['label-'.$localeCode] = $label; @@ -112,8 +109,6 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar $progress->advance(); } - $this->families = $families; - $this->writer ->setFilename(sprintf( '%s%s%s', @@ -126,26 +121,6 @@ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar return []; } - /** - * Return the generated families as Family object - * - * @return array - */ - public function getFamilyObjects() - { - $familyObjects = []; - - foreach ($this->families as $code => $family) { - $familyObject = new Family(); - - $familyObject->setCode($code); - - $familyObjects[] = $familyObject; - } - - return $familyObjects; - } - /** * Get localized random labels * diff --git a/Generator/UserGenerator.php b/Generator/UserGenerator.php index 453513b..62bc58c 100644 --- a/Generator/UserGenerator.php +++ b/Generator/UserGenerator.php @@ -65,12 +65,12 @@ public function __construct(CsvWriter $writer) */ public function generate(array $globalConfig, array $entitiesConfig, ProgressBar $progress, array $options = []) { - $this->locales = $options['locales']; - $this->channels = $options['channels']; - $this->categories = $options['categories']; - $this->userRoles = $options['user_roles']; - $this->userGroups = $options['user_groups']; - $this->assetCategoryCodes = isset($options['asset_category_codes']) ? $options['asset_category_codes'] : []; + $this->locales = $options['locales'] ?? []; + $this->channels = $options['channels'] ?? []; + $this->categories = $options['categories'] ?? []; + $this->userRoles = $options['user_roles'] ?? []; + $this->userGroups = $options['user_groups'] ?? []; + $this->assetCategoryCodes = $options['asset_category_codes'] ?? []; $this->faker = Factory::create(); if (isset($globalConfig['seed'])) { @@ -130,11 +130,15 @@ protected function generateUser(array $userConfig) $user->setEnabled($userConfig['enable']); foreach ($userConfig['groups'] as $groupCode) { - $user->addGroup($this->userGroups[$groupCode]); + if (isset($this->userGroups[$groupCode])) { + $user->addGroup($this->userGroups[$groupCode]); + } } foreach ($userConfig['roles'] as $roleCode) { - $user->addRole($this->userRoles[$roleCode]); + if (isset($this->userRoles[$roleCode])) { + $user->addRole($this->userRoles[$roleCode]); + } } if (isset($userConfig['catalog_locale'])) { @@ -154,7 +158,7 @@ protected function generateUser(array $userConfig) if (isset($userConfig['default_tree'])) { $categoryCode = $userConfig['default_tree']; $user->setDefaultTree($this->categories[$categoryCode]); - } else { + } elseif (isset($this->categories[ChannelGenerator::DEFAULT_TREE])) { $user->setDefaultTree($this->categories[ChannelGenerator::DEFAULT_TREE]); } @@ -205,7 +209,7 @@ protected function normalizeUser(UserInterface $user) 'last_name' => $user->getLastname(), 'catalog_locale' => $user->getCatalogLocale()->getCode(), 'catalog_scope' => $user->getCatalogScope()->getCode(), - 'default_tree' => $user->getDefaultTree()->getCode(), + 'default_tree' => $user->getDefaultTree() ? $user->getDefaultTree()->getCode() : null, 'roles' => implode(',', $userRoleCodes), 'groups' => implode(',', $userGroupCodes), 'enabled' => $user->isEnabled() ? '1' : '0', @@ -213,7 +217,7 @@ protected function normalizeUser(UserInterface $user) ]; if (count($this->assetCategoryCodes) > 0) { - $result["default_asset_tree"] = $this->faker->randomElement($this->assetCategoryCodes); + $result['default_asset_tree'] = $this->faker->randomElement($this->assetCategoryCodes); } return $result; diff --git a/README.md b/README.md index 24ca3a9..2997d47 100644 --- a/README.md +++ b/README.md @@ -42,18 +42,43 @@ Configuration file examples --------------------------- Generating base fixtures: +**Be careful! The order of the entries under `entities` is important.** + ```yaml data_generator: - output_dir: /tmp/fixtures/ + output_dir: /path/to/your/pim/src/Pim/Bundle/InstallerBundle/Resources/fixtures/data_generator/ entities: + locales: ~ + channels: + mobile: + locales: [en_US, fr_FR] + currencies: [USD, EUR] + magento: + locales: [en_US, fr_FR, de_DE, es_ES] + currencies: [USD, EUR] + paper: + locales: [en_US, fr_FR, de_DE, es_ES, it_IT] + currencies: [USD, EUR] + website: + locales: [en_US, fr_FR, de_DE, es_ES, it_IT] + currencies: [USD, EUR] + marketplace: + locales: [en_US] + currencies: [USD] + attribute_groups: ~ + categories: ~ attributes: - count: 200 - identifier_attribute: "sku" + count: 2000 + identifier_attribute: sku + force_attributes: [name=pim_catalog_text] + attribute_options: + count_per_attribute: 5 families: - count: 30 - attributes_count: 60 - identifier_attribute: "sku" - label_attribute: "label" + count: 10 + attributes_count: 200 + identifier_attribute: sku + label_attribute: name + ``` Generating products: