Skip to content

Unit Test #1

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 9 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
47 changes: 47 additions & 0 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Laravel

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

jobs:
laravel-tests:

runs-on: ubuntu-latest

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
ports:
- 3306

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: Update Dependencies
run: composer update --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: mysql
DB_HOST: 127.0.0.1
DB_PORT: ${{ job.services.mysql.ports[3306] }}
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_USERNAME: root
DB_PASSWORD: ""
run: php artisan test
2 changes: 1 addition & 1 deletion resources/views/products/layouts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel 10 CRUD Application Tutorial - AllPHPTricks.com</title>
<title>Group Tugas Ceria - CI/CD Test</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">

Expand Down
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);
}
}