|
| 1 | +<?php |
| 2 | + |
| 3 | +use Eclipse\Catalogue\Models\PriceList; |
| 4 | +use Eclipse\Catalogue\Models\Product; |
| 5 | +use Eclipse\Catalogue\Models\Product\Price as ProductPrice; |
| 6 | +use Eclipse\World\Models\Currency; |
| 7 | +use Illuminate\Database\QueryException; |
| 8 | + |
| 9 | +uses(\Illuminate\Foundation\Testing\RefreshDatabase::class); |
| 10 | + |
| 11 | +beforeEach(function () { |
| 12 | + Currency::create(['id' => 'USD', 'name' => 'US Dollar', 'is_active' => true]); |
| 13 | + Currency::create(['id' => 'EUR', 'name' => 'Euro', 'is_active' => true]); |
| 14 | +}); |
| 15 | + |
| 16 | +function makeProduct(): Product |
| 17 | +{ |
| 18 | + return Product::factory()->create(); |
| 19 | +} |
| 20 | + |
| 21 | +function makePriceList(array $attrs = []): PriceList |
| 22 | +{ |
| 23 | + return PriceList::factory()->create(array_merge([ |
| 24 | + 'currency_id' => 'USD', |
| 25 | + 'tax_included' => false, |
| 26 | + ], $attrs)); |
| 27 | +} |
| 28 | + |
| 29 | +test('can create a product price', function () { |
| 30 | + $product = makeProduct(); |
| 31 | + $priceList = makePriceList(); |
| 32 | + |
| 33 | + $price = ProductPrice::create([ |
| 34 | + 'product_id' => $product->id, |
| 35 | + 'price_list_id' => $priceList->id, |
| 36 | + 'valid_from' => '2025-08-18', |
| 37 | + 'valid_to' => null, |
| 38 | + 'price' => 12.34567, |
| 39 | + 'tax_included' => true, |
| 40 | + ]); |
| 41 | + |
| 42 | + expect($price)->toBeInstanceOf(ProductPrice::class); |
| 43 | + expect($price->price)->toBeFloat(); |
| 44 | + expect($price->tax_included)->toBeTrue(); |
| 45 | + expect($price->valid_from)->toBeInstanceOf(\Illuminate\Support\Carbon::class); |
| 46 | + expect($price->valid_to)->toBeNull(); |
| 47 | +}); |
| 48 | + |
| 49 | +test('price has product and price list relations', function () { |
| 50 | + $product = makeProduct(); |
| 51 | + $priceList = makePriceList(); |
| 52 | + |
| 53 | + $price = ProductPrice::create([ |
| 54 | + 'product_id' => $product->id, |
| 55 | + 'price_list_id' => $priceList->id, |
| 56 | + 'valid_from' => '2025-08-18', |
| 57 | + 'price' => 9.99, |
| 58 | + 'tax_included' => false, |
| 59 | + ]); |
| 60 | + |
| 61 | + expect($price->product)->toBeInstanceOf(Product::class) |
| 62 | + ->and($price->priceList)->toBeInstanceOf(PriceList::class); |
| 63 | +}); |
| 64 | + |
| 65 | +test('cannot create duplicate price for same product, price list and date', function () { |
| 66 | + $product = makeProduct(); |
| 67 | + $priceList = makePriceList(); |
| 68 | + |
| 69 | + ProductPrice::create([ |
| 70 | + 'product_id' => $product->id, |
| 71 | + 'price_list_id' => $priceList->id, |
| 72 | + 'valid_from' => '2025-08-18', |
| 73 | + 'price' => 10, |
| 74 | + 'tax_included' => false, |
| 75 | + ]); |
| 76 | + |
| 77 | + // 2nd insert with the same triple key must fail at DB level |
| 78 | + expect(fn () => ProductPrice::create([ |
| 79 | + 'product_id' => $product->id, |
| 80 | + 'price_list_id' => $priceList->id, |
| 81 | + 'valid_from' => '2025-08-18', |
| 82 | + 'price' => 11, |
| 83 | + 'tax_included' => true, |
| 84 | + ]))->toThrow(QueryException::class); |
| 85 | +}); |
| 86 | + |
| 87 | +test('same date is allowed on different price lists', function () { |
| 88 | + $product = makeProduct(); |
| 89 | + $pl1 = makePriceList(['currency_id' => 'USD']); |
| 90 | + $pl2 = makePriceList(['currency_id' => 'USD']); |
| 91 | + |
| 92 | + ProductPrice::create([ |
| 93 | + 'product_id' => $product->id, |
| 94 | + 'price_list_id' => $pl1->id, |
| 95 | + 'valid_from' => '2025-08-18', |
| 96 | + 'price' => 10, |
| 97 | + 'tax_included' => false, |
| 98 | + ]); |
| 99 | + |
| 100 | + $p2 = ProductPrice::create([ |
| 101 | + 'product_id' => $product->id, |
| 102 | + 'price_list_id' => $pl2->id, |
| 103 | + 'valid_from' => '2025-08-18', |
| 104 | + 'price' => 12, |
| 105 | + 'tax_included' => false, |
| 106 | + ]); |
| 107 | + |
| 108 | + expect($p2->exists)->toBeTrue(); |
| 109 | +}); |
| 110 | + |
| 111 | +test('same date is allowed on same price list for different products', function () { |
| 112 | + $p1 = makeProduct(); |
| 113 | + $p2 = makeProduct(); |
| 114 | + $priceList = makePriceList(); |
| 115 | + |
| 116 | + ProductPrice::create([ |
| 117 | + 'product_id' => $p1->id, |
| 118 | + 'price_list_id' => $priceList->id, |
| 119 | + 'valid_from' => '2025-08-18', |
| 120 | + 'price' => 10, |
| 121 | + 'tax_included' => false, |
| 122 | + ]); |
| 123 | + |
| 124 | + $ok = ProductPrice::create([ |
| 125 | + 'product_id' => $p2->id, |
| 126 | + 'price_list_id' => $priceList->id, |
| 127 | + 'valid_from' => '2025-08-18', |
| 128 | + 'price' => 13, |
| 129 | + 'tax_included' => true, |
| 130 | + ]); |
| 131 | + |
| 132 | + expect($ok->exists)->toBeTrue(); |
| 133 | +}); |
| 134 | + |
| 135 | +test('stores price with up to 5 decimal places', function () { |
| 136 | + $product = makeProduct(); |
| 137 | + $priceList = makePriceList(); |
| 138 | + |
| 139 | + $p = ProductPrice::create([ |
| 140 | + 'product_id' => $product->id, |
| 141 | + 'price_list_id' => $priceList->id, |
| 142 | + 'valid_from' => '2025-08-18', |
| 143 | + 'price' => 123.45678, |
| 144 | + 'tax_included' => true, |
| 145 | + ])->fresh(); |
| 146 | + |
| 147 | + expect($p->price)->toEqual(123.45678); |
| 148 | +}); |
| 149 | + |
| 150 | +test('(soft) deleting product does not remove prices', function () { |
| 151 | + $product = makeProduct(); |
| 152 | + $priceList = makePriceList(); |
| 153 | + |
| 154 | + $price = ProductPrice::create([ |
| 155 | + 'product_id' => $product->id, |
| 156 | + 'price_list_id' => $priceList->id, |
| 157 | + 'valid_from' => '2025-08-18', |
| 158 | + 'price' => 5, |
| 159 | + 'tax_included' => false, |
| 160 | + ]); |
| 161 | + |
| 162 | + $product->delete(); |
| 163 | + |
| 164 | + expect(ProductPrice::find($price->id))->not->toBeNull(); |
| 165 | +}); |
0 commit comments