Added Unit Test

This commit is contained in:
Devansh 2022-04-18 19:37:56 +05:30
parent 56504d9fb9
commit 4138d0d1ca
3 changed files with 141 additions and 2 deletions

View File

@ -0,0 +1,31 @@
<?php
namespace Webkul\Product\Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Webkul\Product\Models\Product;
use Webkul\Product\Models\ProductOrderedInventory;
class ProductOrderedInventoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ProductOrderedInventory::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition(): array
{
return [
'qty' => $this->faker->numberBetween(100, 200),
'product_id' => Product::factory(),
'channel_id' => 1,
];
}
}

View File

@ -2,15 +2,29 @@
namespace Webkul\Product\Models;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Webkul\Inventory\Models\InventorySourceProxy;
use Webkul\Core\Models\ChannelProxy;
use Webkul\Product\Contracts\ProductOrderedInventory as ProductOrderedInventoryContract;
use Webkul\Product\Database\Factories\ProductOrderedInventoryFactory;
class ProductOrderedInventory extends Model implements ProductOrderedInventoryContract
{
use HasFactory;
/**
* Timestamps.
*
* @var bool
*/
public $timestamps = false;
/**
* Fillables.
*
* @var array
*/
protected $fillable = [
'qty',
'product_id',
@ -19,6 +33,8 @@ class ProductOrderedInventory extends Model implements ProductOrderedInventoryCo
/**
* Get the channel owns the inventory.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function channel()
{
@ -27,9 +43,21 @@ class ProductOrderedInventory extends Model implements ProductOrderedInventoryCo
/**
* Get the product that owns the product inventory.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product()
{
return $this->belongsTo(ProductProxy::modelClass());
}
}
/**
* Create a new factory instance for the model.
*
* @return Factory
*/
protected static function newFactory(): Factory
{
return ProductOrderedInventoryFactory::new ();
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace Tests\Unit\Sales\Order;
use Helper\Bagisto;
use UnitTester;
use Webkul\Product\Models\ProductOrderedInventory;
use Webkul\Sales\Models\OrderItem;
use Webkul\Sales\Repositories\OrderItemRepository;
class OrderItemRepositoryCest
{
/**
* Order item repository.
*
* @var Webkul\Sales\Repositories\OrderItemRepository
*/
private $repository;
public function _before()
{
$reflection = new \ReflectionClass(OrderItemRepository::class);
$property = $reflection->getProperty('model');
$property->setAccessible(true);
$this->repository = $reflection->newInstanceWithoutConstructor();
}
public function testUpdateProductOrderedInventories(UnitTester $I)
{
/**
* Having 10 quantity in inventory.
*/
$product = $I->haveProduct(Bagisto::SIMPLE_PRODUCT, [
'productInventory' => ['qty' => 10],
]);
/**
* 2 quantities are on hold.
*/
$productOrderedInventory = $I->have(ProductOrderedInventory::class, [
'product_id' => $product->id,
'qty' => 2,
]);
/**
* 5 quantities are shipped.
*/
$orderItem1 = $I->have(OrderItem::class, [
'product_id' => $product->id,
'qty_ordered' => 5,
'qty_shipped' => 5,
]);
/**
* 2 quantities are in queue.
*/
$orderItem2 = $I->have(OrderItem::class, [
'product_id' => $product->id,
'qty_ordered' => 2,
'qty_shipped' => 0,
]);
/**
* Now testing the repository method with shipped one cancelled.
*/
$this->repository->updateProductOrderedInventories($orderItem1);
$productOrderedInventory->refresh();
$I->assertNotEquals(0, $productOrderedInventory->qty);
$I->assertEquals(2, $productOrderedInventory->qty);
/**
* Now testing the repository method with pending one cancelled.
*/
$this->repository->updateProductOrderedInventories($orderItem2);
$productOrderedInventory->refresh();
$I->assertEquals(0, $productOrderedInventory->qty);
}
}