Skip to content
35 changes: 35 additions & 0 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Laravel

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
laravel-tests:

runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.1'
- uses: actions/checkout@v4
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit/Pest
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: php artisan test
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions resources/views/products/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
<td>{{ $product->quantity }}</td>
<td>{{ $product->price }}</td>
<td>
<form action="{{ route('products.destroy', $product->id) }}" method="post">
<form action="{{ route('products.destroy', $product->id) }}" method="post" id="deleteForm-{{ $product->id }}">
@csrf
@method('DELETE')

<a href="{{ route('products.show', $product->id) }}" class="btn btn-warning btn-sm"><i class="bi bi-eye"></i> Show</a>

<a href="{{ route('products.edit', $product->id) }}" class="btn btn-primary btn-sm"><i class="bi bi-pencil-square"></i> Edit</a>

<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Do you want to delete this product?');"><i class="bi bi-trash"></i> Delete</button>
<button type="button" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#deleteModal-{{ $product->id }}"><i class="bi bi-trash"></i> Delete</button>
</form>
</td>
</tr>
Expand All @@ -63,5 +63,26 @@
</div>
</div>
</div>

@endsection

@forelse ($products as $product)
<div class="modal fade" id="deleteModal-{{ $product->id }}" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Confirm Deletion</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete this product?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger" form="deleteForm-{{ $product->id }}">Delete</button>
</div>
</div>
</div>
</div>
@empty
@endforelse

@endsection
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');
}
}
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' => 'H0012',
'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);
}
}