Skip to content

Instantly share code, notes, and snippets.

@nanasess
Created August 5, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanasess/225c7f6943059d332e228b85b03e1f84 to your computer and use it in GitHub Desktop.
Save nanasess/225c7f6943059d332e228b85b03e1f84 to your computer and use it in GitHub Desktop.
データ生成スクリプト for EC-CUBE2.17
<?php
require __DIR__.'/require.php';
ini_set('display_errors', 1);
// Here you can initialize variables that will be available to your tests
$config = [
'fixture_customer_num' => 10, // 会員数
'fixture_product_num' => 30000, // 商品数
'fixture_order_num' => 1 // 注文数
];
$faker = Faker\Factory::create('ja_JP');
/** @var SC_Query $objQuery */
$objQuery = SC_Query_Ex::getSingletonInstance();
$objGenerator = new FixtureGenerator($objQuery, 'ja_JP');
$num = $objQuery->count('dtb_customer');
if ($num < $config['fixture_customer_num']) {
$num = $config['fixture_customer_num'] - $num;
echo 'Generating Customers';
for ($i = 0; $i < $num; $i++) {
$objGenerator->createCustomer();
echo '.';
}
$objGenerator->createCustomer(null, ['status' => '1']); // non-active member
echo '.'.PHP_EOL;
}
$num = $objQuery->count('dtb_products');
$product_ids = [];
// 受注生成件数 + 初期データの商品が生成されているはず
if ($num < ($config['fixture_product_num'] + 2)) {
echo 'Generating Products';
// 規格なしも含め $config['fixture_product_num'] の分だけ生成する
for ($i = 0; $i < $config['fixture_product_num'] - 1; $i++) {
$product_ids[] = $objGenerator->createProduct();
echo '.';
}
$product_ids[] = $objGenerator->createProduct('規格なし商品', 0);
echo '.'.PHP_EOL;
$category_ids = $objGenerator->createCategories();
foreach ($product_ids as $product_id) {
$objGenerator->relateProductCategories($product_id, array_rand(array_flip($category_ids), $faker->numberBetween(2, count($category_ids) - 1)));
}
$objDb = new SC_Helper_DB_Ex();
$objDb->sfCountCategory($objQuery);
}
$num = $objQuery->count('dtb_order');
$customer_ids = $objQuery->getCol('customer_id', 'dtb_customer', 'del_flg = 0');
$product_class_ids = $objQuery->getCol('product_class_id', 'dtb_products_class', 'del_flg = 0');
if ($num < $config['fixture_order_num']) {
echo 'Generating Orders';
foreach ($customer_ids as $customer_id) {
$target_product_class_ids = array_rand(array_flip($product_class_ids), $faker->numberBetween(2, count($product_class_ids) - 1));
$charge = $faker->randomNumber(4);
$discount = $faker->numberBetween(0, $charge);
$order_count_per_customer = $objQuery->count('dtb_order', 'customer_id = ?', [$customer_id]);
for ($i = $order_count_per_customer; $i < $config['fixture_order_num'] / count($customer_ids); $i++) {
$objGenerator->createOrder($customer_id, $target_product_class_ids, 1, $charge, $discount, $faker->numberBetween(1, 7));
echo '.';
}
}
echo PHP_EOL;
}
@nanasess
Copy link
Author

nanasess commented Aug 5, 2019

  1. tests ディレクトリ以下に設置する
  2. 以下のように実行する
php tests/generator.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment