gruene-seele/tests/Feature/ProductPhase4Test.php

88 lines
2.7 KiB
PHP

<?php
use App\Models\PackagingItem;
use App\Models\PackagingMaterial;
use App\Models\Product;
use App\Models\Supplier;
use App\Repositories\ProductRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
test('product saves packaging bom quantities and order', function () {
$product = Product::create([
'name' => 'Creme',
'title' => 'Creme',
'active' => true,
]);
$material = PackagingMaterial::factory()->create(['name' => 'Glas-P4']);
$supplier = Supplier::factory()->create();
$a = PackagingItem::factory()->create([
'packaging_material_id' => $material->id,
'supplier_id' => $supplier->id,
'name' => 'Flasche 30 ml',
'category' => 'packaging',
'weight_grams' => 40,
]);
$b = PackagingItem::factory()->create([
'packaging_material_id' => $material->id,
'supplier_id' => $supplier->id,
'name' => 'Etikett',
'category' => 'label',
'weight_grams' => 1.5,
]);
$repo = new ProductRepository($product);
$repo->update([
'id' => (string) $product->id,
'name' => $product->name,
'title' => $product->title,
'active' => '1',
'product_inci_sync_sent' => '1',
'pp_packaging_item_id' => [(string) $b->id, (string) $a->id],
'pp_quantity' => ['2', '1,5'],
]);
$product->refresh();
$rows = $product->packagings()->orderByPivot('pos')->get();
expect($rows)->toHaveCount(2);
expect($rows[0]->id)->toBe($b->id);
expect((float) $rows[0]->pivot->quantity)->toBe(2.0);
expect((int) $rows[0]->pivot->pos)->toBe(0);
expect($rows[1]->id)->toBe($a->id);
expect((float) $rows[1]->pivot->quantity)->toBe(1.5);
expect((int) $rows[1]->pivot->pos)->toBe(1);
});
test('product copy preserves packaging bom', function () {
$product = Product::create([
'name' => 'Serum',
'title' => 'Serum',
'active' => true,
]);
$material = PackagingMaterial::factory()->create();
$supplier = Supplier::factory()->create();
$pk = PackagingItem::factory()->create([
'packaging_material_id' => $material->id,
'supplier_id' => $supplier->id,
'name' => 'Pumpflasche',
'category' => 'packaging',
]);
$product->packagings()->sync([
$pk->id => ['quantity' => 3,
'pos' => 0,
],
]);
$repo = new ProductRepository(new Product);
$copy = $repo->copy($product->fresh());
$copy->load('packagings');
expect($copy->packagings)->toHaveCount(1);
expect($copy->packagings->first()->id)->toBe($pk->id);
expect((float) $copy->packagings->first()->pivot->quantity)->toBe(3.0);
});