Skip to content

Commit

Permalink
Update unit test order & product
Browse files Browse the repository at this point in the history
  • Loading branch information
shivishbrahma committed Jun 11, 2023
1 parent 683779c commit 9efb9ea
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 145 deletions.
136 changes: 62 additions & 74 deletions tests/Unit/OrderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,106 +5,94 @@
use App\Models\Address;
use App\Models\Order;
use App\Models\Product;
use App\Models\Seller;
use App\Models\User;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class OrderUnitTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
private $user;
use RefreshDatabase;

private $user, $product, $sellerUser, $seller, $address, $order;

protected function setUp(): void
{
parent::setUp();

$this->user = new User();
$this ->product=new Product();
$this->address=new Address();
$this->order=new Order();
$this->user = User::factory()->create();
$this->sellerUser = User::factory()->seller()->create();
$this->seller = Seller::factory()->create(['user_id' => $this->sellerUser->id]);
$this->product = Product::factory()->create(['seller_id' => $this->seller->id]);
$this->address = Address::factory()->create(['user_id' => $this->user->id]);
$this->order = Order::factory()->create([
'user_id' => $this->user->id,
'product_id' => $this->product->id,
'address_id' => $this->address->id
]);
}
public function testFillableAttributes()

public function testFillableAttributes()
{
$fillable = ['user_id',
'product_id',
'address_id',
'order_id',
'qty',
'price',
'discount',
'status',
'type',];
$fillable = [
'user_id',
'product_id',
'address_id',
'order_id',
'qty',
'price',
'discount',
'status',
'type',
];

$this->assertEquals($this->order->getFillable(), $fillable);
}

public function test_order_belongsto_user()
public function testOrderBelongstoUser()
{

$order=new Order();
$this->assertEquals($order->user_id, $this->user->id);

$this->assertEquals($this->order->user->id, $this->user->id);
}
public function test_order_belongsto_product()

public function testOrderBelongstoProduct()
{
$order=new Order();
$this->assertEquals($order->product_id, $this->product->id);

$this->assertEquals($this->order->product->id, $this->product->id);
}
public function test_order_belongsto_address()

public function testOrderBelongstoAddress()
{

$order=new Order();
$this->assertEquals($order->address_id, $this->address->id);
$this->assertEquals($this->order->address->id, $this->address->id);
}
public function test_get_Discounted_PriceAttribute()
{

$model = new Order();
$model->price = 10;

$model->discount = 0.3;
$output = $model->getDiscountedPriceAttribute($model);
$expect = new Order();
$expect->price = 10;

$expect->discount = 0.3;
$expect1 = $expect->price * (1 - $expect->discount);
$this->assertEquals($expect1, $output);
}
public function test_get_Total_PriceAttribute()
public function testGetDiscountedPriceAttribute()
{
//$this->price * $this->qty
$model = new Order();
$model->price = 10;
$this->assertEquals(
$this->order->discountedPrice,
$this->order->price * (1 - $this->order->discount)
);
}

$model->qty = 2;
$output = $model->getTotalPriceAttribute($model);
$expect = new Order();
$expect->price = 10;
public function testGetTotalPriceAttribute()
{
$this->assertEquals(
$this->order->totalPrice,
$this->order->price * $this->order->qty
);
}

$expect->qty = 2;
$expect1 = $expect->price * $expect->qty;
$this->assertEquals($expect1, $output);
public function testGetTotalDiscountedPriceAttribute()
{
$this->assertEquals(
$this->order->totalDiscountedPrice,
$this->order->price * (1 - $this->order->discount) * $this->order->qty
);
}

public function test_get_TotalDiscountedPriceAttribute()
public function testCategoryAttribute()
{
// $this->price * (1 - $this->discount) * $this->qty;
$model = new Order();
$model->price = 10;
$model->discount = 0.3;
$model->qty = 2;
$output = $model->getTotalDiscountedPriceAttribute($model);
$expect = new Order();
$expect->price = 10;

$expect->discount = 0.3;
$expect->qty = 2;
$expect1 = $expect->price * (1 - $expect->discount) * $expect->qty;
$this->assertEquals($expect1, $output);
$this->assertEquals(
$this->order->category,
$this->order::$categories[$this->order->type]
);
}
}
135 changes: 64 additions & 71 deletions tests/Unit/ProductUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,95 @@
use App\Models\Product;
use App\Models\Seller;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ProductUnitTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/

use RefreshDatabase;


private $user, $product, $sellerUser, $seller, $fileimage;

protected function setUp(): void
{
parent::setUp();


$this->user=new User();
$this->product=new Product();
$this->seller=new Seller();
$this->fileimage=new FileImage();

$this->user = User::factory()->create();
$this->sellerUser = User::factory()->seller()->create();
$this->seller = Seller::factory()->create(['user_id' => $this->sellerUser->id]);
$this->product = Product::factory()->create(['seller_id' => $this->seller->id]);
// $this->fileimage = FileImage::factory()->create();
}



public function testFillableAttributes()
{
$fillable = [ 'type',
'seller_id',
'desc',
'price',
'name',
'unit',
'quantity',
'slug',
'discount',];
$fillable = [
'type',
'seller_id',
'desc',
'price',
'name',
'unit',
'quantity',
'slug',
'discount',
'active',
];

$this->assertEquals($this->product->getFillable(), $fillable);
}

public function test_product_belongsto_seller()
public function testProductBelongstoSeller()
{
$product=new product();
$this->assertEquals($product->seller_id, $this->seller->id);
$this->assertEquals(
$this->product->seller->id,
$this->seller->id
);
}
public function test_product_hasmany_coverphoto()
{

$product = new product();
$user=new User();
$coverphoto=new FileImage(['ref_id'=>array('type','product')]);
$coverphoto1=new FileImage(['ref_id'=>array('type','product')]);
$testArray = array($coverphoto,$coverphoto1);
$expectedCount = 2;
$this->assertCount(
$expectedCount,
$testArray
);
}
public function testgetPriceAfterDiscountWithDiscount()
public function testProductHasManyCoverPhotos()
{
$model = new Product();
$model->price = 10;
$coverphotos[] = FileImage::factory()->create([
'type' => 'products',
'ref_id' => $this->product->id
]);
$coverphotos[] = FileImage::factory()->create([
'type' => 'products', 'ref_id' => $this->product->id
]);
$coverphotos = collect($coverphotos);

$model->discount = 0.3;
$output = $model->getDiscountedPriceAttribute($model);
$expect = new Product();
$expect->price = 10;
$this->assertCount(
2,
$this->product->coverPhotos
);

$expect->discount = 0.3;
$expect1 = $expect->price * (1 - $expect->discount);
$this->assertEquals($expect1, $output);
$this->assertEquals(
$this->product->coverPhotos->pluck('id'),
$coverphotos->pluck('id')
);
}
public function testgetcategory()

public function testGetPriceAfterDiscountWithDiscount()
{
$model = new Product();
$model::$categories[$model->type] = 'Vegetable';
$output = $model->getCategoryAttribute($model);
$expect = new Product();
// $expect::$categories[$model->type]='Vegetable';
$expect = 'Vegetable';
$this->assertEquals($expect, $output);
$this->assertEquals(
$this->product->discountedPrice,
$this->product->price * (1 - $this->product->discount)
);
}
public function testgetproductunit()

public function testGetCategory()
{
$model = new Product();
$model::$units[$model->unit] = 'KGS';
$output = $model->getProductUnitAttribute($model);
$expect = new Product();
$this->assertEquals(
$this->product->category,
$this->product::$categories[$this->product->type]
);
}

$expect = 'KGS';
$this->assertEquals($expect, $output);
public function testGetProductUnit()
{
$this->assertEquals(
$this->product->productUnit,
$this->product::$units[$this->product->unit]
);
}


}
}

0 comments on commit 9efb9ea

Please sign in to comment.