141 lines
4.9 KiB
PHP
141 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Purchase;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Catalog\Models\Product;
|
|
use App\Domains\Catalog\Models\ProductVariant;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class StorePurchaseTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_creates_a_purchase_with_customer_details_and_clears_the_cart(): void
|
|
{
|
|
// 1. Setup Tenant
|
|
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
|
|
|
// 2. Setup User
|
|
$user = User::factory()->create([
|
|
'email' => 'buyer@example.com'
|
|
]);
|
|
|
|
// 3. Setup Product & Variant
|
|
$category = \App\Domains\Catalog\Models\Category::query()->create([
|
|
'tenant_code' => 'sonder',
|
|
'nombre' => 'Test Category',
|
|
]);
|
|
|
|
$product = Product::query()->create([
|
|
'tenant_codigo' => 'sonder',
|
|
'categoria_id' => $category->id,
|
|
'slug' => 'test-product',
|
|
'nombre' => 'Test Product',
|
|
'descripcion' => 'Test',
|
|
'precio' => '50.00',
|
|
]);
|
|
|
|
$variant = ProductVariant::query()->create([
|
|
'producto_id' => $product->id,
|
|
'slug' => 'test-variant',
|
|
'nombre' => 'Test Variant',
|
|
'stock' => 10,
|
|
'precio' => '50.00',
|
|
]);
|
|
|
|
// 4. Add item to user's cart
|
|
$this->actingAs($user, 'sanctum')
|
|
->postJson('/api/tenants/sonder/cart/items', [
|
|
'product_variant_id' => $variant->id,
|
|
'cantidad' => 2,
|
|
])
|
|
->assertOk();
|
|
|
|
// Check cart exists in DB
|
|
$this->assertDatabaseHas('carritos', [
|
|
'tenant_codigo' => 'sonder',
|
|
'user_id' => $user->id,
|
|
]);
|
|
|
|
// 5. Submit Purchase with payment_method at root
|
|
$response = $this->actingAs($user, 'sanctum')
|
|
->postJson('/api/tenants/sonder/compras', [
|
|
'dni' => '987654321',
|
|
'telefono' => '+54 9 341 555-4321',
|
|
'nombre_apellido' => 'Juan Perez',
|
|
'email' => 'juan.perez@example.com',
|
|
'payment_method' => 'transferencia',
|
|
'items' => [
|
|
[
|
|
'producto_variante_id' => $variant->id,
|
|
'cantidad' => 2,
|
|
]
|
|
]
|
|
]);
|
|
|
|
// 6. Assertions
|
|
$response->assertCreated();
|
|
$response->assertJsonPath('data.dni', '987654321');
|
|
$response->assertJsonPath('data.telefono', '+54 9 341 555-4321');
|
|
$response->assertJsonPath('data.nombre_apellido', 'Juan Perez');
|
|
$response->assertJsonPath('data.email', 'juan.perez@example.com');
|
|
$response->assertJsonPath('data.payment_method', 'transferencia');
|
|
$response->assertJsonPath('data.tenant_codigo', 'sonder');
|
|
|
|
// Assert Purchase saved in DB
|
|
$this->assertDatabaseHas('compras', [
|
|
'tenant_codigo' => 'sonder',
|
|
'user_id' => $user->id,
|
|
'dni' => '987654321',
|
|
'telefono' => '+54 9 341 555-4321',
|
|
'nombre_apellido' => 'Juan Perez',
|
|
'email' => 'juan.perez@example.com',
|
|
'payment_method' => 'transferencia',
|
|
]);
|
|
|
|
// Assert Cart was cleared (deleted)
|
|
$this->assertDatabaseMissing('carritos', [
|
|
'tenant_codigo' => 'sonder',
|
|
'user_id' => $user->id,
|
|
]);
|
|
}
|
|
|
|
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
|
{
|
|
$hdrKey = (string) \Illuminate\Support\Str::uuid();
|
|
$ftrKey = (string) \Illuminate\Support\Str::uuid();
|
|
|
|
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
|
'key' => $hdrKey,
|
|
'path' => 'tenants/' . $hdrKey . '.png',
|
|
'filename' => 'logo_header.png',
|
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
|
'key' => $ftrKey,
|
|
'path' => 'tenants/' . $ftrKey . '.png',
|
|
'filename' => 'logo_footer.png',
|
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
|
|
return Tenant::create([
|
|
'codigo' => $codigo,
|
|
'nombre' => $nombre,
|
|
'dominio' => $dominio,
|
|
'primary_color' => '#111111',
|
|
'secondary_color' => '#222222',
|
|
'danger_color' => '#333333',
|
|
'success_color' => '#28a745',
|
|
'header_bg_color' => '#444444',
|
|
'footer_bg_color' => '#444444',
|
|
'header_logo_id' => $headerAttachment->id,
|
|
'footer_logo_id' => $footerAttachment->id,
|
|
]);
|
|
}
|
|
}
|