Skip to content

Unit Test added #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/Feature/funcCreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class funcCreateTest extends TestCase
{
use RefreshDatabase;

/**
* Test the create function.
*
* @return void
*/
public function test_create_products()
{
$response = $this->get(route('products.create'));

$response->assertStatus(200);

$response->assertViewIs('products.create');
}
}
26 changes: 26 additions & 0 deletions tests/Feature/funcCreateUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class funcCreateUpdate extends TestCase
{
use RefreshDatabase, WithFaker;

/**
* Test the update function.
*
* @return void
*/
public function test_update_products()
{
$response = $this->get(route('products.create'));

$response->assertStatus(200);

$response->assertViewIs('products.create');
}
}
38 changes: 38 additions & 0 deletions tests/Feature/funcDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tests\Feature;

use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class funcDeleteTest extends TestCase
{
use RefreshDatabase;

/**
* Test the destroy method.
*/
public function test_delete_product(): void
{
// Create a product to delete
$product = Product::create([
'code' => 'P12345',
'name' => 'Product to be deleted',
'quantity' => 10,
'price' => 99.99,
'description' => 'Product description',
]);

// Send the delete request
$response = $this->delete(route('products.destroy', $product->id));

// Assert the response status
$response->assertRedirect(route('products.index'));
$response->assertSessionHas('success', 'Product is deleted successfully.');

// Assert the product was deleted
$this->assertDatabaseMissing('products', ['id' => $product->id]);
}
}
48 changes: 48 additions & 0 deletions tests/Feature/funcUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Feature;

use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class funcUpdateTest extends TestCase
{
use RefreshDatabase;

/**
* Test the update method.
*/
public function test_update_product(): void
{
$product = Product::create([
'code' => 'H001',
'name' => 'Kaveh',
'quantity' => 1,
'price' => 99.99,
'description' => 'Husbunya Ren',
]);

$updateData = [
'code' => 'H001',
'name' => 'Kaveh',
'quantity' => 1,
'price' => 199.99,
'description' => 'Malewife keduanya Ren',
];

$response = $this->put(route('products.update', $product->id), $updateData);

$response->assertRedirect();
$response->assertSessionHas('success', 'Product is updated successfully.');

$updatedProduct = Product::find($product->id);

$this->assertEquals($updateData['code'], $updatedProduct->code);
$this->assertEquals($updateData['name'], $updatedProduct->name);
$this->assertEquals($updateData['quantity'], $updatedProduct->quantity);
$this->assertEquals($updateData['price'], $updatedProduct->price);
$this->assertEquals($updateData['description'], $updatedProduct->description);
}
}
33 changes: 33 additions & 0 deletions tests/Feature/funcViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Feature;

use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class funcViewTest extends TestCase
{
use RefreshDatabase;

/**
* Test the show method.
*/
public function test_show_product(): void
{
$product = Product::create([
'code' => 'H001',
'name' => 'Kaaveh',
'quantity' => 1,
'price' => 99.99,
'description' => 'Husbunya Ren',
]);

$response = $this->get(route('products.show', $product->id));

$response->assertStatus(200);
$response->assertViewIs('products.show');
$response->assertViewHas('product', $product);
}
}
Loading