mivita/tests/Unit/Services/AboHelperEnsureUserAboItemsFromLatestOrderTest.php
2026-04-10 17:15:27 +02:00

233 lines
6.5 KiB
PHP

<?php
use App\Models\Country;
use App\Models\Product;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\Models\UserAboOrder;
use App\Models\UserShop;
use App\Services\AboHelper;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
it('stellt user_abo_items aus der letzten Bestellung wieder her wenn die Tabelle leer ist', function () {
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create([
'name' => 'Standard',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'shop-owner-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TS'.substr(uniqid('', true), 0, 8),
'slug' => 'ts-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust-'.uniqid('', true).'@example.com',
]);
$now = now();
$productId = (int) DB::table('products')->insertGetId([
'name' => 'Test Abo Produkt',
'title' => 'Test Abo Produkt',
'active' => true,
'show_on' => json_encode(['12']),
'created_at' => $now,
'updated_at' => $now,
]);
$product = Product::query()->findOrFail($productId);
$userAbo = UserAbo::create([
'user_id' => null,
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'ot',
'email' => $shoppingUser->billing_email,
'payone_userid' => 999001,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 10,
'is_abo' => true,
'paid' => true,
'txaction' => 'paid',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
ShoppingOrderItem::create([
'shopping_order_id' => $shoppingOrder->id,
'row_id' => 'row-'.uniqid(),
'product_id' => $product->id,
'comp' => 0,
'qty' => 1,
'price' => 90,
'price_net' => 75,
'tax_rate' => 19,
'tax' => 15,
'slug' => $product->slug ?? 'test',
]);
UserAboOrder::create([
'user_abo_id' => $userAbo->id,
'shopping_order_id' => $shoppingOrder->id,
'status' => 2,
'paid' => true,
]);
expect($userAbo->user_abo_items()->count())->toBe(0);
expect(AboHelper::ensureUserAboItemsFromLatestOrder($userAbo))->toBeTrue();
$userAbo->refresh();
expect($userAbo->user_abo_items()->count())->toBe(1)
->and($userAbo->user_abo_items->first()->product_id)->toBe($product->id);
});
it('gibt true zurück wenn bereits user_abo_items existieren', function () {
$country = Country::create([
'code' => 'AT',
'phone' => '43',
'en' => 'Austria',
'de' => 'Österreich',
'es' => 'Austria',
'fr' => 'Autriche',
'it' => 'Austria',
'ru' => 'Австрия',
]);
$shipping = Shipping::create([
'name' => 'Std2',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'so-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TS2'.substr(uniqid('', true), 0, 6),
'slug' => 'ts2-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'co-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'cu-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'x-'.uniqid('', true).'@example.com',
]);
$now = now();
$productId = (int) DB::table('products')->insertGetId([
'name' => 'P2',
'title' => 'P2',
'active' => true,
'show_on' => json_encode(['12']),
'created_at' => $now,
'updated_at' => $now,
]);
$product = Product::query()->findOrFail($productId);
$userAbo = UserAbo::create([
'member_id' => $consultant->id,
'shopping_user_id' => $shoppingUser->id,
'is_for' => 'ot',
'email' => $shoppingUser->billing_email,
'payone_userid' => 999002,
'clearingtype' => 'cc',
'active' => true,
'status' => 2,
'abo_interval' => 1,
]);
$userAbo->user_abo_items()->create([
'product_id' => $product->id,
'comp' => 0,
'qty' => 1,
'status' => 1,
]);
expect(AboHelper::ensureUserAboItemsFromLatestOrder($userAbo))->toBeTrue();
expect($userAbo->user_abo_items()->count())->toBe(1);
});