From a196b7ac02059d2c5f979af8e02c6c34e8dda2c6 Mon Sep 17 00:00:00 2001 From: Annika Wolff Date: Thu, 11 Jun 2020 10:51:40 +0200 Subject: [PATCH 1/2] extend order item and cart item factories --- .../Database/Factories/CartItemFactory.php | 24 ++++++++++++++++++- .../Database/Factories/OrderItemFactory.php | 23 +++++++++--------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php b/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php index 7c916f194..c43f66dbd 100644 --- a/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php +++ b/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php @@ -3,12 +3,34 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ use Faker\Generator as Faker; +use Webkul\Checkout\Models\Cart; use Webkul\Checkout\Models\CartItem; +use Webkul\Product\Models\Product; -$factory->define(CartItem::class, function (Faker $faker) { +$factory->define(CartItem::class, function (Faker $faker, array $attributes) { $now = date("Y-m-d H:i:s"); + if (isset($attributes['product_id'])) { + $product = Product::where('id', $attributes['product_id'])->first(); + } else { + $product = factory(Product::class)->create(); + } + + $fallbackPrice = $faker->randomFloat(4, 0, 1000); + return [ + 'quantity' => 1, + 'sku' => $product->sku, + 'type' => $product->type, + 'name' => $product->name, + 'price' => $product->price ?? $fallbackPrice, + 'base_price' => $product->price ?? $fallbackPrice, + 'total' => $product->price ?? $fallbackPrice, + 'base_total' => $product->price ?? $fallbackPrice, + 'product_id' => $product->id, + 'cart_id' => function () { + return factory(Cart::class)->create()->id; + }, 'created_at' => $now, 'updated_at' => $now, ]; diff --git a/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php b/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php index 104f95831..b8e1f72a8 100644 --- a/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php @@ -3,8 +3,8 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ use Faker\Generator as Faker; -use Webkul\Product\Models\Product; use Webkul\Sales\Models\Order; +use Webkul\Product\Models\Product; use Webkul\Sales\Models\OrderItem; $factory->define(OrderItem::class, function (Faker $faker, array $attributes) { @@ -16,14 +16,16 @@ $factory->define(OrderItem::class, function (Faker $faker, array $attributes) { $product = factory(Product::class)->create(); } + $fallbackPrice = $faker->randomFloat(4, 0, 1000); + return [ - 'sku' => $product->sku, - 'type' => $product->type, - 'name' => $product->name, - 'price' => $product->price, - 'base_price' => $product->price, - 'total' => $product->price, - 'base_total' => $product->price, + 'sku' => $product->sku, + 'type' => $product->type, + 'name' => $product->name, + 'price' => $product->price ?? $fallbackPrice, + 'base_price' => $product->price ?? $fallbackPrice, + 'total' => $product->price ?? $fallbackPrice, + 'base_total' => $product->price ?? $fallbackPrice, 'product_id' => $product->id, 'qty_ordered' => 1, 'qty_shipped' => 0, @@ -37,7 +39,4 @@ $factory->define(OrderItem::class, function (Faker $faker, array $attributes) { 'updated_at' => $now, 'product_type' => Product::class, ]; -}); - - - +}); \ No newline at end of file From 1c1d99b6af5b5d9af955c1c99184f39f09235bf0 Mon Sep 17 00:00:00 2001 From: Annika Wolff Date: Thu, 11 Jun 2020 10:56:08 +0200 Subject: [PATCH 2/2] autoformat --- .../src/Database/Factories/OrderItemFactory.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php b/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php index b8e1f72a8..895e3e209 100644 --- a/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php @@ -19,13 +19,13 @@ $factory->define(OrderItem::class, function (Faker $faker, array $attributes) { $fallbackPrice = $faker->randomFloat(4, 0, 1000); return [ - 'sku' => $product->sku, - 'type' => $product->type, - 'name' => $product->name, - 'price' => $product->price ?? $fallbackPrice, - 'base_price' => $product->price ?? $fallbackPrice, - 'total' => $product->price ?? $fallbackPrice, - 'base_total' => $product->price ?? $fallbackPrice, + 'sku' => $product->sku, + 'type' => $product->type, + 'name' => $product->name, + 'price' => $product->price ?? $fallbackPrice, + 'base_price' => $product->price ?? $fallbackPrice, + 'total' => $product->price ?? $fallbackPrice, + 'base_total' => $product->price ?? $fallbackPrice, 'product_id' => $product->id, 'qty_ordered' => 1, 'qty_shipped' => 0,