forked from lunarphp/lunar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature - Minimum quantity and quantity increment (lunarphp#1392)
Closes lunarphp#1366
- Loading branch information
1 parent
72b7374
commit 392448c
Showing
7 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...tions/2023_12_18_100000_add_quantity_increment_min_quantity_to_product_variants_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Lunar\Base\Migration; | ||
|
||
class AddQuantityIncrementMinQuantityToProductVariantsTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::table($this->prefix.'product_variants', function (Blueprint $table) { | ||
$table->integer('quantity_increment')->after('unit_quantity')->unsigned()->default(1)->index(); | ||
$table->integer('min_quantity')->after('unit_quantity')->unsigned()->default(1)->index(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::table($this->prefix.'product_variants', function ($table) { | ||
$table->dropColumn('quantity_increment'); | ||
}); | ||
Schema::table($this->prefix.'product_variants', function ($table) { | ||
$table->dropColumn('min_quantity'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
tests/core/Unit/Validation/CartLine/CartLineQuantityTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
uses(\Lunar\Tests\Core\TestCase::class) | ||
->group('validation.cart_line'); | ||
|
||
use Lunar\Exceptions\Carts\CartException; | ||
use Lunar\Models\Cart; | ||
use Lunar\Models\Currency; | ||
use Lunar\Validation\CartLine\CartLineQuantity; | ||
|
||
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class); | ||
|
||
test('can validate zero quantity', function () { | ||
$currency = Currency::factory()->create(); | ||
|
||
$cart = Cart::factory()->create([ | ||
'currency_id' => $currency->id, | ||
]); | ||
|
||
$purchasable = \Lunar\Models\ProductVariant::factory()->create(); | ||
|
||
$validator = (new CartLineQuantity)->using( | ||
cart: $cart, | ||
purchasable: $purchasable, | ||
quantity: 0, | ||
meta: [] | ||
); | ||
|
||
expect(fn () => $validator->validate()) | ||
->toThrow(CartException::class, __('lunar::exceptions.invalid_cart_line_quantity', ['quantity' => 0])); | ||
}); | ||
|
||
test('can validate excessive quantity', function () { | ||
$currency = Currency::factory()->create(); | ||
|
||
$cart = Cart::factory()->create([ | ||
'currency_id' => $currency->id, | ||
]); | ||
|
||
$purchasable = \Lunar\Models\ProductVariant::factory()->create(); | ||
|
||
$quantity = 1000001; | ||
|
||
$validator = (new CartLineQuantity)->using( | ||
cart: $cart, | ||
purchasable: $purchasable, | ||
quantity: $quantity, | ||
meta: [] | ||
); | ||
|
||
expect(fn () => $validator->validate()) | ||
->toThrow(CartException::class, __('lunar::exceptions.maximum_cart_line_quantity', ['quantity' => 1000000])); | ||
}); | ||
|
||
test('can validate minimum quantity', function () { | ||
$currency = Currency::factory()->create(); | ||
|
||
$cart = Cart::factory()->create([ | ||
'currency_id' => $currency->id, | ||
]); | ||
|
||
$purchasable = \Lunar\Models\ProductVariant::factory()->create([ | ||
'min_quantity' => 10, | ||
]); | ||
|
||
$quantity = 9; | ||
|
||
$validator = (new CartLineQuantity)->using( | ||
cart: $cart, | ||
purchasable: $purchasable, | ||
quantity: $quantity, | ||
meta: [] | ||
); | ||
|
||
expect(fn () => $validator->validate()) | ||
->toThrow(CartException::class, __('lunar::exceptions.minimum_quantity', ['quantity' => $purchasable->min_quantity])); | ||
}); | ||
|
||
test('can validate quantity increment quantity', function (array $quantities, int $increment) { | ||
$currency = Currency::factory()->create(); | ||
|
||
$cart = Cart::factory()->create([ | ||
'currency_id' => $currency->id, | ||
]); | ||
|
||
$purchasable = \Lunar\Models\ProductVariant::factory()->create([ | ||
'min_quantity' => 1, | ||
'quantity_increment' => $increment, | ||
]); | ||
|
||
foreach ($quantities as $quantity => $outcome) { | ||
$validator = (new CartLineQuantity)->using( | ||
cart: $cart, | ||
purchasable: $purchasable, | ||
quantity: $quantity, | ||
meta: [] | ||
); | ||
|
||
if ($outcome == 'fail') { | ||
expect(fn () => $validator->validate()) | ||
->toThrow( | ||
CartException::class, | ||
__('lunar::exceptions.quantity_increment', [ | ||
'increment' => $purchasable->quantity_increment, | ||
'quantity' => $quantity, | ||
]) | ||
); | ||
|
||
continue; | ||
} | ||
|
||
expect($validator->validate())->toBeTrue(); | ||
} | ||
})->with([ | ||
'1 increment' => [ | ||
'quantities' => [ | ||
1 => 'pass', | ||
2 => 'pass', | ||
3 => 'pass', | ||
4 => 'pass', | ||
5 => 'pass', | ||
6 => 'pass', | ||
7 => 'pass', | ||
8 => 'pass', | ||
9 => 'pass', | ||
10 => 'pass', | ||
20 => 'pass', | ||
30 => 'pass', | ||
40 => 'pass', | ||
50 => 'pass', | ||
100 => 'pass', | ||
], | ||
'increment' => 1, | ||
], | ||
'10 increment' => [ | ||
'quantities' => [ | ||
1 => 'fail', | ||
2 => 'fail', | ||
3 => 'fail', | ||
4 => 'fail', | ||
5 => 'fail', | ||
10 => 'pass', | ||
11 => 'fail', | ||
15 => 'fail', | ||
20 => 'pass', | ||
25 => 'fail', | ||
30 => 'pass', | ||
40 => 'pass', | ||
], | ||
'increment' => 10, | ||
], | ||
'14 increment' => [ | ||
'quantities' => [ | ||
1 => 'fail', | ||
2 => 'fail', | ||
3 => 'fail', | ||
7 => 'fail', | ||
14 => 'pass', | ||
16 => 'fail', | ||
28 => 'pass', | ||
36 => 'fail', | ||
56 => 'pass', | ||
], | ||
'increment' => 14, | ||
], | ||
]); |