EC-CUBE /
ec-cube
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | use Codeception\Util\Fixtures; |
||
| 4 | use Eccube\Common\EccubeConfig; |
||
| 5 | use Eccube\Entity\Customer; |
||
| 6 | use Eccube\Entity\Master\CustomerStatus; |
||
| 7 | use Eccube\Kernel; |
||
| 8 | use Faker\Factory as Faker; |
||
| 9 | |||
| 10 | |||
| 11 | $config = parse_ini_file(__DIR__.'/config.ini', true); |
||
| 12 | |||
| 13 | /** |
||
| 14 | * create fixture |
||
| 15 | * このデータは$appを使って直接eccubeのデータベースに作成される |
||
| 16 | * よってCodeceptionの設定によってコントロールされず、テスト後もデータベース内にこのデータは残る |
||
| 17 | * データの件数によって、作成するかどうか判定される |
||
| 18 | */ |
||
| 19 | if (file_exists($config['eccube_path'].'/vendor/autoload.php')) { |
||
| 20 | require_once $config['eccube_path'].'/vendor/autoload.php'; |
||
| 21 | } |
||
| 22 | if (file_exists(__DIR__.'/../../.env')) { |
||
| 23 | (new \Dotenv\Dotenv(__DIR__.'/../../'))->overload(); |
||
| 24 | } |
||
| 25 | $kernel = new Kernel('test', false); |
||
| 26 | $kernel->boot(); |
||
| 27 | |||
| 28 | $container = $kernel->getContainer(); |
||
| 29 | $entityManager = $container->get('doctrine')->getManager(); |
||
| 30 | Fixtures::add('entityManager', $entityManager); |
||
| 31 | |||
| 32 | // // この Fixture は Cest ではできるだけ使用せず, 用途に応じた Fixture を使用すること |
||
| 33 | // Fixtures::add('app', $app); |
||
| 34 | |||
| 35 | $faker = Faker::create('ja_JP'); |
||
| 36 | Fixtures::add('faker', $faker); |
||
| 37 | |||
| 38 | $progress = (function () |
||
| 39 | { |
||
| 40 | $current = ''; |
||
| 41 | return function ($key) use (&$current) { |
||
| 42 | if ($current !== $key) { |
||
| 43 | if ($current !== '') { |
||
| 44 | echo PHP_EOL; |
||
| 45 | } |
||
| 46 | echo $key.' '; |
||
| 47 | $current = $key; |
||
| 48 | } |
||
| 49 | echo '.'; |
||
| 50 | }; |
||
| 51 | })(); |
||
| 52 | |||
| 53 | $num = $entityManager->getRepository('Eccube\Entity\Customer') |
||
| 54 | ->createQueryBuilder('o') |
||
| 55 | ->select('count(o.id)') |
||
| 56 | ->getQuery() |
||
| 57 | ->getSingleScalarResult(); |
||
| 58 | if ($num < $config['fixture_customer_num']) { |
||
| 59 | $num = $config['fixture_customer_num'] - $num; |
||
| 60 | for ($i = 0; $i < $num; $i++) { |
||
| 61 | $email = microtime(true).'.'.$faker->safeEmail; |
||
| 62 | $progress('Generating Customers'); |
||
| 63 | $customer = createCustomer($container, $email); |
||
| 64 | } |
||
| 65 | $progress('Generating Customers'); |
||
| 66 | createCustomer($container, null, false); // non-active member |
||
| 67 | } |
||
| 68 | |||
| 69 | $num = $entityManager->getRepository('Eccube\Entity\Product') |
||
| 70 | ->createQueryBuilder('o') |
||
| 71 | ->select('count(o.id)') |
||
| 72 | ->getQuery() |
||
| 73 | ->getSingleScalarResult(); |
||
| 74 | // 受注生成件数 + 初期データの商品が生成されているはず |
||
| 75 | if ($num < ($config['fixture_product_num'] + 2)) { |
||
| 76 | // 規格なしも含め $config['fixture_product_num'] の分だけ生成する |
||
| 77 | for ($i = 0; $i < $config['fixture_product_num'] - 1; $i++) { |
||
| 78 | $progress('Generating Products'); |
||
| 79 | createProduct($container); |
||
| 80 | } |
||
| 81 | $progress('Generating Products'); |
||
| 82 | createProduct($container, '規格なし商品', 0); |
||
| 83 | } |
||
| 84 | |||
| 85 | $Customers = $entityManager->getRepository('Eccube\Entity\Customer')->findAll(); |
||
| 86 | $Products = $entityManager->getRepository('Eccube\Entity\Product')->findAll(); |
||
| 87 | $Deliveries = $entityManager->getRepository('Eccube\Entity\Delivery')->findAll(); |
||
| 88 | |||
| 89 | $allOrderCount = $entityManager->getRepository('Eccube\Entity\Order') |
||
| 90 | ->createQueryBuilder('o') |
||
| 91 | ->select('count(o.id)') |
||
| 92 | ->getQuery() |
||
| 93 | ->getSingleScalarResult(); |
||
| 94 | |||
| 95 | if ($allOrderCount < $config['fixture_order_num']) { |
||
| 96 | foreach ($Customers as $Customer) { |
||
| 97 | $Delivery = $Deliveries[$faker->numberBetween(0, count($Deliveries) - 1)]; |
||
| 98 | $Product = $Products[$faker->numberBetween(0, count($Products) - 1)]; |
||
| 99 | $charge = $faker->randomNumber(4); |
||
| 100 | $discount = $faker->randomNumber(4); |
||
| 101 | |||
| 102 | $orderCountPerCustomer = $entityManager->getRepository('Eccube\Entity\Order') |
||
| 103 | ->createQueryBuilder('o') |
||
| 104 | ->select('count(o.id)') |
||
| 105 | ->where('o.Customer = :Customer') |
||
| 106 | ->setParameter('Customer', $Customer) |
||
| 107 | ->getQuery() |
||
| 108 | ->getSingleScalarResult(); |
||
| 109 | for ($i = $orderCountPerCustomer; $i < $config['fixture_order_num'] / count($Customers); $i++) { |
||
| 110 | $Status = $entityManager->getRepository('Eccube\Entity\Master\OrderStatus')->find($faker->numberBetween(1, 8)); |
||
| 111 | $OrderDate = $faker->dateTimeThisYear(); |
||
| 112 | $progress('Generating Orders'); |
||
| 113 | createOrder($container, $Customer, $Product->getProductClasses()->toArray(), $Delivery, $charge, $discount, $Status, $OrderDate); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | function createCustomer($container, $email = null, $active = true) |
||
| 119 | { |
||
| 120 | $entityManager = $container->get('doctrine')->getManager(); |
||
| 121 | $generator = $container->get('Eccube\Tests\Fixture\Generator'); |
||
| 122 | |||
| 123 | $Customer = $generator->createCustomer($email); |
||
| 124 | if ($active) { |
||
| 125 | $Status = $entityManager->getRepository('Eccube\Entity\Master\CustomerStatus')->find(CustomerStatus::ACTIVE); |
||
|
0 ignored issues
–
show
|
|||
| 126 | } else { |
||
| 127 | $Status = $entityManager->getRepository('Eccube\Entity\Master\CustomerStatus')->find(CustomerStatus::NONACTIVE); |
||
|
0 ignored issues
–
show
|
|||
| 128 | } |
||
| 129 | $Customer->setStatus($Status); |
||
| 130 | $entityManager->flush($Customer); |
||
| 131 | return $Customer; |
||
| 132 | } |
||
| 133 | |||
| 134 | function createProduct($container, $product_name = null, $product_class_num = 3) |
||
| 135 | { |
||
| 136 | $generator = $container->get('Eccube\Tests\Fixture\Generator'); |
||
| 137 | return $generator->createProduct($product_name, $product_class_num); |
||
| 138 | } |
||
| 139 | |||
| 140 | function createOrder($container, Customer $Customer, array $ProductClasses, $Delivery, $charge, $discount, $Status, $OrderDate) |
||
| 141 | { |
||
| 142 | $entityManager = $container->get('doctrine')->getManager(); |
||
| 143 | $generator = $container->get('Eccube\Tests\Fixture\Generator'); |
||
| 144 | |||
| 145 | $Order = $generator->createOrder($Customer, $ProductClasses, $Delivery, $charge, $discount); |
||
| 146 | $Order->setOrderStatus($Status); |
||
| 147 | $Order->setOrderDate($OrderDate); |
||
| 148 | $Order->setOrderNo(\Eccube\Util\StringUtil::random(6)); |
||
| 149 | $entityManager->flush($Order); |
||
| 150 | return $Order; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * fixtureとして、対象eccubeのconfigおよびデータベースからデータを取得する |
||
| 155 | * [codeception path]/tests/acceptance/config.iniに対象eccubeのpathを記述すること |
||
| 156 | * つまり、対象eccubeとcodeception作業ディレクトリはファイルシステム上で同一マシンにある(様にみえる)ことが必要 |
||
| 157 | * fixtureをテスト内で利用する場合は、Codeception\Util\Fixtures::getメソッドを使う |
||
| 158 | * ちなみに、Fixturesとは関係なく、CodeceptionのDbモジュールで直接データベースを利用する場合は、 |
||
| 159 | * [codeception path]/codeception.ymlのDbセクションに対象eccubeで利用しているデータベースへの接続情報を記述して利用する |
||
| 160 | */ |
||
| 161 | |||
| 162 | /** 管理画面アカウント情報. */ |
||
| 163 | Fixtures::add('admin_account', array( |
||
| 164 | 'member' => $config['admin_user'], |
||
| 165 | 'password' => $config['admin_password'], |
||
| 166 | )); |
||
| 167 | /** $app['config'] 情報. */ |
||
| 168 | Fixtures::add('config', $container->get(EccubeConfig::class)); |
||
| 169 | |||
| 170 | /** config.ini 情報. */ |
||
| 171 | Fixtures::add('test_config', $config); |
||
| 172 | |||
| 173 | $baseinfo = $entityManager->getRepository('Eccube\Entity\BaseInfo')->get(); |
||
| 174 | /** BaseInfo. */ |
||
| 175 | Fixtures::add('baseinfo', $baseinfo); |
||
| 176 | |||
| 177 | $categories = $entityManager->getRepository('Eccube\Entity\Category') |
||
| 178 | ->createQueryBuilder('o') |
||
| 179 | ->getQuery() |
||
| 180 | ->getResult(); |
||
| 181 | /** カテゴリ一覧の配列. */ |
||
| 182 | Fixtures::add('categories', $categories); |
||
| 183 | |||
| 184 | $findOrders = function () use ($entityManager) { |
||
| 185 | return $entityManager->getRepository('Eccube\Entity\Order') |
||
| 186 | ->createQueryBuilder('o') |
||
| 187 | ->getQuery() |
||
| 188 | ->getResult(); |
||
| 189 | }; |
||
| 190 | /** 受注を検索するクロージャ. */ |
||
| 191 | Fixtures::add('findOrders', $findOrders); |
||
| 192 | |||
| 193 | $findShippings = function () use ($entityManager) { |
||
| 194 | return $entityManager->getRepository('Eccube\Entity\Shipping') |
||
| 195 | ->createQueryBuilder('o') |
||
| 196 | ->getQuery() |
||
| 197 | ->getResult(); |
||
| 198 | }; |
||
| 199 | /** 出荷を検索するクロージャ. */ |
||
| 200 | Fixtures::add('findShippings', $findShippings); |
||
| 201 | |||
| 202 | View Code Duplication | $resetShippingDate = function () use ($entityManager) { |
|
| 203 | $Shippings = $entityManager->getRepository('Eccube\Entity\Shipping') |
||
| 204 | ->findAll(); |
||
| 205 | foreach ($Shippings as $Shipping) { |
||
| 206 | $Shipping->setShippingDate(null); |
||
| 207 | } |
||
| 208 | $entityManager->flush(); |
||
| 209 | return true; |
||
| 210 | }; |
||
| 211 | /** 出荷準備中に更新するクロージャ. */ |
||
| 212 | Fixtures::add('resetShippingDate', $resetShippingDate); |
||
| 213 | |||
| 214 | View Code Duplication | $setShippingDate = function () use ($entityManager) { |
|
| 215 | $Shippings = $entityManager->getRepository('Eccube\Entity\Shipping') |
||
| 216 | ->findAll(); |
||
| 217 | foreach ($Shippings as $Shipping) { |
||
| 218 | $Shipping->setShippingDate(new \DateTime()); |
||
| 219 | } |
||
| 220 | $entityManager->flush(); |
||
| 221 | return true; |
||
| 222 | }; |
||
| 223 | /** 出荷済みに更新するクロージャ. */ |
||
| 224 | Fixtures::add('setShippingDate', $setShippingDate); |
||
| 225 | |||
| 226 | $deleteShippingNotExistsOfItem = function () use ($entityManager) { |
||
| 227 | |||
| 228 | $Shippings = $entityManager->getRepository('Eccube\Entity\Shipping')->findAll(); |
||
| 229 | |||
| 230 | if ($Shippings) { |
||
| 231 | foreach ($Shippings as $Shipping) { |
||
| 232 | if ($Shipping->getOrderItems()->isEmpty()) { |
||
| 233 | $entityManager->remove($Shipping); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | $entityManager->flush(); |
||
| 237 | } |
||
| 238 | |||
| 239 | return true; |
||
| 240 | }; |
||
| 241 | /** OrderItemの存在しない出荷を削除するクロージャ. */ |
||
| 242 | Fixtures::add('deleteShippingNotExistsOfItem', $deleteShippingNotExistsOfItem); |
||
| 243 | |||
| 244 | $findProducts = function () use ($entityManager) { |
||
| 245 | return $entityManager->getRepository('Eccube\Entity\Product') |
||
| 246 | ->createQueryBuilder('p') |
||
| 247 | ->getQuery() |
||
| 248 | ->getResult(); |
||
| 249 | }; |
||
| 250 | /** 商品を検索するクロージャ. */ |
||
| 251 | Fixtures::add('findProducts', $findProducts); |
||
| 252 | |||
| 253 | $createProduct = function ($product_name = null, $product_class_num = 3) use ($container) { |
||
| 254 | return createProduct($container, $product_name, $product_class_num); |
||
| 255 | }; |
||
| 256 | Fixtures::add('createProduct', $createProduct); |
||
| 257 | |||
| 258 | $createCustomer = function ($email = null, $active = true) use ($container, $faker) { |
||
| 259 | if (is_null($email)) { |
||
| 260 | $email = microtime(true).'.'.$faker->safeEmail; |
||
| 261 | } |
||
| 262 | return createCustomer($container, $email, $active); |
||
| 263 | }; |
||
| 264 | /** 会員を生成するクロージャ. */ |
||
| 265 | Fixtures::add('createCustomer', $createCustomer); |
||
| 266 | |||
| 267 | $createOrders = function ($Customer, $numberOfOrders = 5, $ProductClasses = array(), $Status = null) use ($container, $entityManager, $faker) { |
||
| 268 | $generator = $container->get('Eccube\Tests\Fixture\Generator'); |
||
| 269 | $Orders = array(); |
||
| 270 | for ($i = 0; $i < $numberOfOrders; $i++) { |
||
| 271 | $Order = $generator->createOrder($Customer, $ProductClasses); |
||
| 272 | $Status = $Status |
||
| 273 | ? $entityManager->getRepository('Eccube\Entity\Master\OrderStatus')->find($Status) |
||
| 274 | : $entityManager->getRepository('Eccube\Entity\Master\OrderStatus')->find($faker->numberBetween(1, 7)); |
||
| 275 | $OrderDate = $faker->dateTimeThisYear(); |
||
| 276 | $Order->setOrderStatus($Status); |
||
| 277 | $Order->setOrderDate($OrderDate); |
||
| 278 | $Order->setOrderNo(\Eccube\Util\StringUtil::random(6)); |
||
| 279 | $entityManager->flush($Order); |
||
| 280 | $Orders[] = $Order; |
||
| 281 | } |
||
| 282 | return $Orders; |
||
| 283 | }; |
||
| 284 | /** 受注を生成するクロージャ. */ |
||
| 285 | Fixtures::add('createOrders', $createOrders); |
||
| 286 | |||
| 287 | $findPlugins = function () use ($entityManager) { |
||
| 288 | return $entityManager->getRepository('Eccube\Entity\Plugin')->findAll(); |
||
| 289 | }; |
||
| 290 | /** プラグインを検索するクロージャ */ |
||
| 291 | Fixtures::add('findPlugins', $findPlugins); |
||
| 292 | |||
| 293 | $findPluginByCode = function ($code = null) use ($entityManager) { |
||
| 294 | return $entityManager->getRepository('Eccube\Entity\Plugin')->findOneBy(['code' => $code]); |
||
| 295 | }; |
||
| 296 | /** プラグインを検索するクロージャ */ |
||
| 297 | Fixtures::add('findPluginByCode', $findPluginByCode); |
||
| 298 | |||
| 299 | $findCustomers = function () use ($entityManager) { |
||
| 300 | return $entityManager->getRepository('Eccube\Entity\Customer') |
||
| 301 | ->createQueryBuilder('c') |
||
| 302 | ->getQuery() |
||
| 303 | ->getResult(); |
||
| 304 | }; |
||
| 305 | /** 会員を検索するクロージャ */ |
||
| 306 | Fixtures::add('findCustomers', $findCustomers); |
||
| 307 | |||
| 308 | /** 新着情報を検索するクロージャ */ |
||
| 309 | Fixtures::add('findNews', function () use ($entityManager) { |
||
| 310 | return $entityManager->getRepository(\Eccube\Entity\News::class) |
||
| 311 | ->findBy([], ['sort_no' => 'DESC']); |
||
| 312 | }); |
||
| 313 | |||
| 314 | /** 新着情報を登録するクロージャ */ |
||
| 315 | Fixtures::add('createNews', function ($publishDate, $title, $description, $url = null) use ($entityManager) { |
||
| 316 | $TopNews = $entityManager->getRepository(\Eccube\Entity\News::class) |
||
| 317 | ->findOneBy([], ['sort_no' => 'DESC']); |
||
| 318 | $sortNo = $TopNews ? $TopNews->getSortNo() + 1 : 1; |
||
| 319 | $News = new \Eccube\Entity\News(); |
||
| 320 | $News->setPublishDate($publishDate); |
||
| 321 | $News->setTitle($title); |
||
| 322 | $News->setDescription($description); |
||
| 323 | $News->setUrl($url); |
||
| 324 | $News->setSortNo($sortNo); |
||
| 325 | |||
| 326 | $entityManager->persist($News); |
||
| 327 | $entityManager->flush($News); |
||
| 328 | |||
| 329 | return $News; |
||
| 330 | }); |
||
| 331 |
This class constant has been deprecated.