Skip to content
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
Binary file modified .DS_Store
Binary file not shown.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Calculator

เว็บแอปพลิเคชันเพื่อแสดงตัวอย่างการเขียนกรณีทดสอบ โดยใช้ `Laravel 5.4` และ `PHPUnit` เพื่อการทดสอบ

## ความต้องการเบื้องต้น
| รายการซอฟต์แวร์ | รุ่นขั้นต่ำ | รุ่นที่แนะนำ | รุ่นที่ใช้พัฒนา |
|---------------|:------:|:--------:|:---------:|
| PHP | >=5.7 | 5.7 | 7 |
| composer | 1.4.2 | 1.4.2 | 1.4.2 |
| phpunit | 5.7 | 5.7 | 6.2.2 |

## ขั้นตอนการทดสอบ

1. ดาวน์โหลด `we-inc/calculator` ด้วยคำสั่ง
```bash
git clone https://github.com/we-inc/calculator
```

1. อัพเดต lib ด้วยคำสั่ง `composer`
```bash
cd calculator && \
composer update
```

1. กรณีทดสอบจะอยู่อยู่ภายในโฟลเดอร์
```
+ tests
+ Features
| + AddControllerTest.php
| + MinusControllerTest.php
+ Unit
```

1. สร้างกรณีทดสอบโดยใช้คำสั่งด้านล่าง โดยคำสั่งนี้จะสร้างคลาสซึ่งเป็น Unit Test ชื่อ `SomeTestCaseTest` (โดยที่กรณีทดสอบนั้นต้องลงท้ายชื่อด้วย `Test`)
```bash
php artisan make:test SomeTestCaseTest
```
เพื่อสร้างกรณีทดสอบสำหรับหน้าที่การใช้งานตัวไป และ

```bash
php artisan make:test SomeControllerTest --unit
```
สำหรับกรณีทดสอบสำหรับคลาสใดคลาสหนึ่ง

1. ภายในคลาส `SomeTestCaseTest` ที่สร้างขึ้นมานั้น จะมีซอร์สโค้ดดังตัวอย่างด้านล่าง
```php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class SomeTestCaseTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
```
ด้านในจะปรากฏเมธอด `testExample()` ซึ่งเมธอดที่มีชื่อขึ้นต้นด้วย `test` นั้นจะบรรจุกรณีทดสอบ ซึ่งจะนำมาใช้ทดสอบคุณสมบัติต่าง ๆ ของซอฟต์แวร์ตามที่ต้องการ

1. ทดสอบซอฟต์แวร์ด้วยกรณีทดสอบที่สร้างขึ้นด้วยคำสั่ง `phpunit` ณ โฟลเดอร์ชั้นบนสุดของโปรเจค
```php
phpunit
```

เมื่อผลการทดสอบเสร็จสิ้น จะมีผลลัพธ์การดำเนินการแสดงให้ทราบ

## ข้อมูลเพิ่มเติม

ข้อมูลเพิ่มเติมสำหรับกรณีทดสอบ
- [Laravel: Getting Started](https://laravel.com/docs/master/testing)
- [Illuminate: TestCase API](https://laravel.com/api/master/Illuminate/Foundation/Testing/TestCase.html)
Binary file modified app/.DS_Store
Binary file not shown.
Binary file modified app/Http/.DS_Store
Binary file not shown.
28 changes: 0 additions & 28 deletions app/Http/Controllers/AddController.php

This file was deleted.

28 changes: 0 additions & 28 deletions app/Http/Controllers/DivideController.php

This file was deleted.

89 changes: 89 additions & 0 deletions app/Http/Controllers/InputController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Module\Add;
use App\Module\Minus;
use App\Module\Multiply;
use App\Module\Divide;

class InputController extends Controller{

public function add(){

$add = new Add();

$answer = null;
if(Input::has('num1') && Input::has('num2')){
if(is_numeric(Input::get('num1')) && is_numeric(Input::get('num2'))){
$answer = $add->calculate(Input::get('num1'),Input::get('num2'));
}
else{
$answer = "Numbers are required";
}
}

return view('add', array('answer' => $answer));
}

public function minus(){

$minus = new Minus();

$answer = null;
if(Input::has('num1') && Input::has('num2')){
if(is_numeric(Input::get('num1')) && is_numeric(Input::get('num2'))){
$answer = $minus->calculate(Input::get('num1'),Input::get('num2'));
}
else{
$answer = "Numbers are required";
}
}

return view('minus', array('answer' => $answer));
}

public function multiply(){

$multiply = new Multiply();

$answer = null;
if(Input::has('num1') && Input::has('num2')){
if(is_numeric(Input::get('num1')) && is_numeric(Input::get('num2'))){
$answer = $multiply->calculate(Input::get('num1'),Input::get('num2'));
}
else{
$answer = "Numbers are required";
}
}

return view('multiply', array('answer' => $answer));
}

public function divide(){

$divide = new Divide();

$answer = null;
if(Input::has('num1') && Input::has('num2')){
if(is_numeric(Input::get('num1')) && is_numeric(Input::get('num2'))){
if(Input::get('num2') != 0)
$answer = $divide->calculate(Input::get('num1'),Input::get('num2'));
else
$answer = "Cannot divided by zero";
}
else{
$answer = "Numbers are required";
}
}

return view('divide', array('answer' => $answer));
}

}


?>
28 changes: 0 additions & 28 deletions app/Http/Controllers/MinusController.php

This file was deleted.

28 changes: 0 additions & 28 deletions app/Http/Controllers/MultiplyController.php

This file was deleted.

13 changes: 13 additions & 0 deletions app/Module/Add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Module;

class Add
{

public function calculate($num1,$num2){
return $num1 + $num2;
}
}

?>
12 changes: 12 additions & 0 deletions app/Module/Divide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Module;

class Divide
{
public function calculate($num1,$num2){
return $num1 / $num2;
}
}

?>
13 changes: 13 additions & 0 deletions app/Module/Minus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Module;

class Minus
{

public function calculate($num1,$num2){
return $num1 - $num2;
}
}

?>
12 changes: 12 additions & 0 deletions app/Module/Multiply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Module;

class Multiply
{
public function calculate($num1,$num2){
return $num1 * $num2;
}
}

?>
Loading