-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the
backorder
field to products & variants
- Loading branch information
1 parent
e2f2c23
commit 17dff7d
Showing
10 changed files
with
314 additions
and
7 deletions.
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
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
154 changes: 154 additions & 0 deletions
154
src/MasterProduct/Tests/Unit/MasterProductVariantStockTest.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,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Contains the MasterProductVariantStockTest class. | ||
* | ||
* @copyright Copyright (c) 2023 Vanilo UG | ||
* @author Attila Fulop | ||
* @license MIT | ||
* @since 2023-11-23 | ||
* | ||
*/ | ||
|
||
namespace Unit; | ||
|
||
use Vanilo\MasterProduct\Models\MasterProduct; | ||
use Vanilo\MasterProduct\Models\MasterProductVariant; | ||
use Vanilo\MasterProduct\Tests\TestCase; | ||
|
||
class MasterProductVariantStockTest extends TestCase | ||
{ | ||
protected MasterProduct $master; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->master = MasterProduct::create(['name' => 'Yokka Magnitude Laptop']); | ||
} | ||
|
||
/** @test */ | ||
public function the_stock_can_be_set() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Magnitude YM34 Laptop', | ||
'sku' => '73781', | ||
'stock' => 50, | ||
]); | ||
|
||
$this->assertEquals(50, $product->stock); | ||
} | ||
|
||
/** @test */ | ||
public function the_stock_field_value_returns_a_numeric_value() | ||
{ | ||
$createdProduct = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Magnitude YM34 Laptop', | ||
'sku' => '73781', | ||
'stock' => 73.5, | ||
]); | ||
|
||
$product = MasterProductVariant::find($createdProduct->id); | ||
|
||
$this->assertTrue(\is_numeric($product->stock)); | ||
} | ||
|
||
/** @test */ | ||
public function stock_field_value_defaults_to_zero() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Magnitude YM34 Laptop', | ||
'sku' => '73781', | ||
]); | ||
|
||
$this->assertEquals(0, $product->stock); | ||
} | ||
|
||
/** @test */ | ||
public function is_on_stock_returns_false_if_the_stock_is_equal_to_zero() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Magnitude YM34 Laptop', | ||
'sku' => '73781', | ||
'stock' => 0, | ||
]); | ||
|
||
$this->assertFalse($product->isOnStock()); | ||
} | ||
|
||
/** @test */ | ||
public function is_on_stock_returns_false_if_the_stock_is_less_than_zero() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Magnitude YM34 Laptop', | ||
'sku' => '73781', | ||
'stock' => -8, | ||
]); | ||
|
||
$this->assertFalse($product->isOnStock()); | ||
} | ||
|
||
/** @test */ | ||
public function backorder_value_can_be_specified() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Mokka Screen 14', | ||
'sku' => 'YMSCR1', | ||
'backorder' => 16, | ||
]); | ||
|
||
$this->assertEquals(16, $product->backorder); | ||
} | ||
|
||
/** @test */ | ||
public function backorder_is_null_by_default() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Mokka Screen 15', | ||
'sku' => 'YMSCR2', | ||
'backorder' => null, | ||
]); | ||
|
||
$this->assertNull($product->backorder); | ||
} | ||
|
||
/** @test */ | ||
public function it_implements_the_stockable_interface() | ||
{ | ||
$product = MasterProductVariant::create([ | ||
'master_product_id' => $this->master->id, | ||
'name' => 'Yokka Mokka Screen 16', | ||
'sku' => 'YMSCR3', | ||
'stock' => 3, | ||
'backorder' => null, | ||
]); | ||
|
||
$this->assertTrue($product->isOnStock()); | ||
$this->assertEquals(3, $product->onStockQuantity()); | ||
$this->assertTrue($product->isBackorderUnrestricted()); | ||
$this->assertNull($product->backorderQuantity()); | ||
$this->assertEquals(3, $product->totalAvailableQuantity()); | ||
|
||
$backOrderProduct = MasterProductVariant::create([ | ||
'name' => 'Yokka Mokka Screen 17', | ||
'sku' => 'YMSCR4', | ||
'stock' => -1, | ||
'backorder' => 4, | ||
]); | ||
|
||
$this->assertFalse($backOrderProduct->isOnStock()); | ||
$this->assertEquals(-1, $backOrderProduct->onStockQuantity()); | ||
$this->assertFalse($backOrderProduct->isBackorderUnrestricted()); | ||
$this->assertEquals(4, $backOrderProduct->backorderQuantity()); | ||
$this->assertEquals(3, $backOrderProduct->totalAvailableQuantity()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../database/migrations/2023_11_23_152437_add_backorder_to_master_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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::table('master_product_variants', function (Blueprint $table) { | ||
$table->decimal('backorder', 15, 4, true)->nullable(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('master_product_variants', function (Blueprint $table) { | ||
$table->dropColumn('backorder'); | ||
}); | ||
} | ||
}; |
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
Oops, something went wrong.