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