/** * Generate number data * * @param AbstractAttribute attribute * * @return string */ protected function generateNumberData(AbstractAttribute $attribute) { $min = $attribute->getNumberMin() != null ? $attribute->getNumberMin() : self::DEFAULT_NUMBER_MIN; $max = $attribute->getNumberMax() != null ? $attribute->getNumberMax() : self::DEFAULT_NUMBER_MAX; $decimals = $attribute->isDecimalsAllowed() ? self::DEFAULT_NB_DECIMALS : 0; $number = $this->faker->randomFloat($decimals, $min, $max); return (string) $number; }
/** * @param FactoryInterface $taxRateFactory * @param RepositoryInterface $zoneRepository * @param RepositoryInterface $taxCategoryRepository */ public function __construct(FactoryInterface $taxRateFactory, RepositoryInterface $zoneRepository, RepositoryInterface $taxCategoryRepository) { $this->taxRateFactory = $taxRateFactory; $this->faker = \Faker\Factory::create(); $this->optionsResolver = (new OptionsResolver())->setDefault('name', function (Options $options) { return $this->faker->words(3, true); })->setDefault('code', function (Options $options) { return StringInflector::nameToCode($options['name']); })->setDefault('amount', function (Options $options) { return $this->faker->randomFloat(2, 0, 1); })->setAllowedTypes('amount', 'float')->setDefault('included_in_price', function (Options $options) { return $this->faker->boolean(); })->setAllowedTypes('included_in_price', 'bool')->setDefault('calculator', 'default')->setDefault('zone', LazyOption::randomOne($zoneRepository))->setAllowedTypes('zone', ['null', 'string', ZoneInterface::class])->setNormalizer('zone', LazyOption::findOneBy($zoneRepository, 'code'))->setDefault('tax_category', LazyOption::randomOne($taxCategoryRepository))->setAllowedTypes('tax_category', ['null', 'string', TaxCategoryInterface::class])->setNormalizer('tax_category', LazyOption::findOneBy($taxCategoryRepository, 'code')); }
/** * @param ProductAttributeInterface $productAttribute * * @return mixed */ private function getRandomValueForProductAttribute(ProductAttributeInterface $productAttribute) { switch ($productAttribute->getStorageType()) { case ProductAttributeValueInterface::STORAGE_BOOLEAN: return $this->faker->boolean; case ProductAttributeValueInterface::STORAGE_INTEGER: return $this->faker->numberBetween(0, 10000); case ProductAttributeValueInterface::STORAGE_FLOAT: return $this->faker->randomFloat(4, 0, 10000); case ProductAttributeValueInterface::STORAGE_TEXT: return $this->faker->sentence; case ProductAttributeValueInterface::STORAGE_DATE: case ProductAttributeValueInterface::STORAGE_DATETIME: return $this->faker->dateTimeThisCentury; default: throw new \BadMethodCallException(); } }
/** * Generate value content based on backend type * * @param AbstractAttribute $attribute * @param string $key * * @return string */ protected function generateValueData(AbstractAttribute $attribute, $key) { $data = ""; if (isset($this->forcedValues[$attribute->getCode()])) { return $this->forcedValues[$attribute->getCode()]; } switch ($attribute->getBackendType()) { case "varchar": $validationRule = $attribute->getValidationRule(); switch ($validationRule) { case 'url': $data = $this->faker->url(); break; default: $data = $this->faker->sentence(); break; } break; case "text": $data = $this->faker->sentence(); break; case "date": $data = $this->faker->dateTimeBetween($attribute->getDateMin(), $attribute->getDateMax()); $data = $data->format('Y-m-d'); break; case "metric": case "decimal": case "prices": if ($attribute->getBackendType() && preg_match('/-' . self::METRIC_UNIT . '$/', $key)) { $data = $attribute->getDefaultMetricUnit(); } else { $min = $attribute->getNumberMin() != null ? $attribute->getNumberMin() : self::DEFAULT_NUMBER_MIN; $max = $attribute->getNumberMax() != null ? $attribute->getNumberMax() : self::DEFAULT_NUMBER_MAX; $decimals = $attribute->isDecimalsAllowed() ? self::DEFAULT_NB_DECIMALS : 0; $data = $this->faker->randomFloat($decimals, $min, $max); } break; case "boolean": $data = $this->faker->boolean() ? "1" : "0"; break; case "option": case "options": $options = []; foreach ($attribute->getOptions() as $option) { $options[] = $option; } $option = $this->faker->randomElement($options); if (is_object($option)) { $data = $option->getCode(); } break; default: $data = ''; break; } return (string) $data; }
/** * {@inheritdoc} */ protected function configureOptions(OptionsResolver $resolver) { $resolver->setDefault('code', function (Options $options) { return StringInflector::nameToCode($options['name']); })->setDefault('name', function (Options $options) { return $this->faker->words(3, true); })->setDefault('amount', function (Options $options) { return $this->faker->randomFloat(2, 0, 0.4); })->setAllowedTypes('amount', 'float')->setDefault('included_in_price', function (Options $options) { return $this->faker->boolean(); })->setAllowedTypes('included_in_price', 'bool')->setDefault('calculator', 'default')->setDefault('zone', LazyOption::randomOne($this->zoneRepository))->setAllowedTypes('zone', ['null', 'string', ZoneInterface::class])->setNormalizer('zone', LazyOption::findOneBy($this->zoneRepository, 'code'))->setDefault('category', LazyOption::randomOne($this->taxCategoryRepository))->setAllowedTypes('category', ['null', 'string', TaxCategoryInterface::class])->setNormalizer('category', LazyOption::findOneBy($this->taxCategoryRepository, 'code')); }
private function fillItems(AcceptanceTester $I, $max = 2) { for ($i = 1; $i <= $max; $i++) { $row_selector = sprintf('table.invoice-table tbody tr:nth-child(%d) ', $i); $product_key = $this->faker->text(10); $description = $this->faker->text(80); $unit_cost = $this->faker->randomFloat(2, 0, 100); $quantity = $this->faker->randomDigitNotNull; $I->fillField($row_selector . '#product_key', $product_key); $I->fillField($row_selector . 'textarea', $description); $I->fillField($row_selector . 'td:nth-child(4) input', $unit_cost); $I->fillField($row_selector . 'td:nth-child(5) input', $quantity); } }
/** * Only seed shops * * @param Shop $owner * @return void */ private function seedSales($owner) { if (!$owner instanceof Shop) { return; } $products = $owner->products()->get(); foreach ($products as $product) { if ($this->faker->boolean(50)) { continue; } /** @var Product $product */ $product->sales()->save(factory(Sale::class)->make(['price' => $this->faker->randomFloat(2, 0.01, $product->price)])); } }
/** * Get a random number, float or integer * * @param stdClass $schema * @param bool $float * @return float|int * @throws InvalidArgumentException */ private function getRandomNumber(stdClass $schema, $float = false) { $multipleOf = $this->provider->getProperty($schema, 'multipleOf', $this->config->getMultipleOf()); $maximum = $this->provider->getProperty($schema, 'maximum', $this->config->getMaximum()); $minimum = $this->provider->getProperty($schema, 'minimum', $this->config->getMinimum()); $exclusiveMaximum = $this->provider->getProperty($schema, 'exclusiveMaximum', false); $exclusiveMinimum = $this->provider->getProperty($schema, 'exclusiveMinimum', false); if (true === $exclusiveMaximum) { $maximum--; } if (true === $exclusiveMinimum) { $maximum++; } $decimals = $float ? $this->generator->numberBetween(0, 4) : 0; $modifier = 1 / pow(10, $decimals); $number = $float ? $this->generator->randomFloat($decimals, $minimum, $maximum) : $this->generator->numberBetween($minimum, $maximum); while ($number % $multipleOf !== 0) { $number += $modifier; } return $number; }
/** * @return Order */ protected function getRandomOrder() { $order = new Order(); $order->setId($this->faker->numerify('####'))->setAmount($this->faker->randomFloat(2, 1, 100))->setCurrency('CHF')->setOrderText($this->faker->sentence(4)); return $order; }