'DE', 'phone' => '49', 'en' => 'Germany', 'de' => 'Deutschland', 'es' => 'Alemania', 'fr' => 'Allemagne', 'it' => 'Germania', 'ru' => 'Германия', ]); $shipping = Shipping::create(['name' => 'DEUTSCHLAND', 'active' => true, 'free' => $freeThreshold ?? '']); ShippingPrice::create([ 'shipping_id' => $shipping->id, 'price' => 4.90, 'price_comp' => 0, 'num_comp' => 0, 'tax_rate' => 19, 'weight_from' => 1, 'weight_to' => 1000, ]); $shippingCountry = ShippingCountry::create(['shipping_id' => $shipping->id, 'country_id' => $country->id]); return [$country, $shippingCountry]; } function makeApiOrderEnv(?int $freeThreshold = 80): ShoppingUser { [$country] = makeApiShopEnv($freeThreshold); $account = UserAccount::create(['m_first_name' => 'Mia', 'm_last_name' => 'Vita', 'country_id' => $country->id]); $member = User::forceCreate([ 'email' => 'member-'.uniqid('', true).'@example.com', 'password' => bcrypt('secret'), 'lang' => 'de', 'account_id' => $account->id, ]); UserShop::create(['user_id' => $member->id, 'name' => 'Shop-'.uniqid('', true), 'active' => true]); $apiUser = User::forceCreate([ 'email' => 'api-'.uniqid('', true).'@example.com', 'password' => bcrypt('secret'), 'lang' => 'de', 'm_sponsor' => $member->id, ]); test()->actingAs($apiUser); return ShoppingUser::create([ 'member_id' => $member->id, 'auth_user_id' => 0, 'number' => 1, 'billing_firstname' => 'Max', 'billing_lastname' => 'Muster', 'billing_address' => 'Musterstr. 1', 'billing_zipcode' => '12345', 'billing_city' => 'Berlin', 'billing_country_id' => $country->id, 'billing_email' => 'kunde@example.com', 'shipping_country_id' => $country->id, 'same_as_billing' => true, 'wp_order_number' => 424242, 'mode' => 'dev', ]); } function makeApiProduct(float $price, int $weight = 500, bool $noFreeShipping = false): Product { $now = now(); $id = (int) DB::table('products')->insertGetId([ 'name' => 'P-'.uniqid('', true), 'title' => 'Produkt', 'active' => true, 'price' => $price, 'tax' => 19, 'weight' => $weight, 'points' => 7, 'wp_number' => random_int(10000, 99999999), 'no_free_shipping' => $noFreeShipping, 'show_on' => json_encode(['2']), 'created_at' => $now, 'updated_at' => $now, ]); return Product::findOrFail($id); } function callApiPrepareOrder(ShoppingUser $shoppingUser, array $items): void { Mail::fake(); $wpOrder = array_map(fn (Product $product) => [ 'article' => $product->wp_number, 'qty' => 1, 'price' => (int) round($product->price * 100), ], $items); $method = new ReflectionMethod(ShoppingUserController::class, 'prepareOrder'); $method->invoke(new ShoppingUserController, $wpOrder, $shoppingUser, null, []); } describe('API-Bestellung Versandkostenfrei-Schwellenwert', function () { it('berechnet keine Versandkosten, wenn der Schwellenwert (DE 80 €) erreicht ist', function () { $shoppingUser = makeApiOrderEnv(80); $product = makeApiProduct(80.00); callApiPrepareOrder($shoppingUser, [$product]); $order = $shoppingUser->shopping_orders()->first(); expect((float) $order->shipping)->toBe(0.0) ->and((float) $order->total_shipping)->toBe(80.0); }); it('berechnet Versandkosten, wenn der Schwellenwert nicht erreicht ist', function () { $shoppingUser = makeApiOrderEnv(80); $product = makeApiProduct(79.00); callApiPrepareOrder($shoppingUser, [$product]); $order = $shoppingUser->shopping_orders()->first(); expect((float) $order->shipping)->toBe(4.90) ->and((float) $order->total_shipping)->toBe(83.90); }); it('berechnet Versandkosten, wenn kein Schwellenwert konfiguriert ist', function () { $shoppingUser = makeApiOrderEnv(null); $product = makeApiProduct(200.00); callApiPrepareOrder($shoppingUser, [$product]); $order = $shoppingUser->shopping_orders()->first(); expect((float) $order->shipping)->toBe(4.90); }); it('berechnet Versandkosten nach Gewicht der no_free_shipping-Produkte trotz erreichtem Schwellenwert', function () { $shoppingUser = makeApiOrderEnv(80); $normal = makeApiProduct(80.00); $excluded = makeApiProduct(20.00, 500, true); callApiPrepareOrder($shoppingUser, [$normal, $excluded]); $order = $shoppingUser->shopping_orders()->first(); expect((float) $order->shipping)->toBe(4.90); }); });