Skip to content

[TruongBT] - UT Unit 2 - #25

Closed
truongbt-0855 wants to merge 7 commits into
sun-unit-test-training:masterfrom
truongbt-0855:master
Closed

[TruongBT] - UT Unit 2#25
truongbt-0855 wants to merge 7 commits into
sun-unit-test-training:masterfrom
truongbt-0855:master

Conversation

@truongbt-0855

@truongbt-0855 truongbt-0855 commented May 18, 2021

Copy link
Copy Markdown

Purpose/Notes

Screenshot

Ex 03
image

Ex 04
image

Ex 05
image

Ex 07
image

Ex 06
image

Checklist (*)

  • Pull request has been self-reviewed
  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All CI builds passed successfully (all builds are green)

@truongbt-0855 truongbt-0855 changed the title [WIP] - UT Unit 2 [TruongBT] - UT Unit 2 May 19, 2021
Comment on lines +23 to +45
public function test_index_return_view_success()
{
$products = ['name' => 'test_product'];
$this->productService->shouldReceive('getAllProducts')->andReturn($products);
$response = $this->productController->index();
$this->assertEquals('exercise03::index', $response->getName());
$this->assertEquals(compact('products'), $response->getData());
}

public function test_checkout_return_view_success()
{
$discount = 10;
$paramRequest = [
1 => 5,
2 => 6,
];
$this->productService->shouldReceive('calculateDiscount')->with($paramRequest)->andReturn($discount);

$url = action([ProductController::class, 'checkout']);
$response = $this->post($url, ['total_products' => $paramRequest]);
$response->assertOk();
$response->assertJsonPath('discount', $discount);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cùng test controller mà dùng cả 2 cách trong cùng 1 class có gây khó hiểu không a?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

không nha =))
anh cố tình viết vậy để hiểu hơn thôi

$this->assertEquals(compact('products'), $response->getData());
}

public function test_checkout_return_view_success()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method này có thể nói cụ thể hơn là nó return json, ko phải return view

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Đây đang test method checkout mà anh

Comment thread Modules/Exercise03/Tests/Unit/Http/Requests/CheckoutRequestTest.php
Comment on lines +24 to +27
$dataAll = ['abc' => 'def'];
$this->productModel->shouldReceive('all')->andReturn($dataAll);

$this->assertEquals($dataAll, $this->productRepository->all());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK 100% code coverage. Nhưng function này expect return mảng Product Model thì tốt hơn, expect nó return như kia sẽ gây hiểu nhầm về cấu trúc data trả về

Theo quan điểm của em ở đây "unit test" là ko cần thiết, ko mang lại nhiều ý nghĩa, mà nên integration test với db.


public function test_get_all_products()
{
$dataAll = ['abc' => 'def'];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Như trên, dummy data thì cũng cho nó giống thật tí, service này nó không bao giờ trả về data dạng này, có chăng chỉ cùng kiểu là array

Comment on lines +40 to +67
public function test_calculate_discount_white_shirt()
{
$discount = $this->productService->calculateDiscount([
Product::CRAVAT_TYPE => 2,
Product::WHITE_SHIRT_TYPE => 2,
Product::OTHER_TYPE => 1,
]);

$this->assertEquals(ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT, $discount);
}

public function test_calculate_discount_with_shirt_quantity()
{
$expected = ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT + ProductService::QUANTITY_DISCOUNT;
$discount = $this->productService->calculateDiscount([
Product::CRAVAT_TYPE => 2,
Product::WHITE_SHIRT_TYPE => 2,
Product::OTHER_TYPE => 10,
]);

$this->assertEquals($expected, $discount);
}

public function test_calculate_empty_input()
{
$discount = $this->productService->calculateDiscount([]);
$this->assertEquals(0, $discount);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thiếu case anh nhé, case chỉ áp dụng qty discount. Cụ thể thì e có comment 1 vài cách xác định case ở đây => #26 (comment)

Comment on lines +20 to +46
/**
* @dataProvider provider_get_date_class
*/
public function test_get_date_class($date, $class)
{
$response =$this->calendarService->getDateClass($date, ['2021-05-14']);

$this->assertEquals($class, $response);
}

function provider_get_date_class()
{
return [
[
Carbon::createFromFormat('Y-m-d', '2021-05-16'),
CalendarService::COLOR_RED,
],
[
Carbon::createFromFormat('Y-m-d', '2021-05-15'),
CalendarService::COLOR_BLUE,
],
[
Carbon::createFromFormat('Y-m-d', '2021-05-14'),
CalendarService::COLOR_RED,
],
];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nên có case để thể hiện màu đỏ ưu tiên hơn màu xanh không a? Tức là ngày lễ trùng vào thứ 7

Và thiếu case cho ngày bình thường

$this->assertEquals(compact('optionCoupons', 'optionReceives'), $response->getData());
}

public function test_store()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +33 to +55
function provider_test_validation_fail()
{
return [
[
[],
[
'price' => '',
'option_receive' => '',
'option_coupon' => '',
],
[
'price' => null,
'option_receive' => null,
'option_coupon' => null,
],
[
'price' => 'abc',
'option_receive' => 3,
'option_coupon' => 3,
],
]
];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Có gì đó sai sai? Provider này chỉ đang return 1 data set


public function test_index()
{
$this->calendarService->shouldReceive('getDateClass')->andReturn(1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Đoạn mock này mình không nên chỉ shouldReceiveandReturn luôn.
Mình nên làm để biết thêm là cái method getDateClass gọi bao nhiêu lần và những tham số truyền vào là gì, vì trong controller nó cũng có logic, nếu viết test kiểu này sẽ không test được logic trong controller

use Modules\Exercise05\Services\OrderService;
use Tests\TestCase;

class OrderServiceTest extends TestCase

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nên test thêm các case = nữa.
Hiện tại những case so sánh >= thì anh chỉ test case > nhưng case = thì hổng có test nha

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trong provider này nên thêm có 1 case nữa check độ chính xác tới 2 chữ số thập phân vì cái round nó có thêm tham số thứ 2 là 2

$infoBill['price'] = round(($detailOrder['price'] * 80) / 100, 2);

Cái này nice to have 😄

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list($minBill2, $freeTime2) = CalculateService::CASE_2;
$freeTime = CalculateService::FREE_TIME_FOR_MOVIE;

return [

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ở đây sẽ có 1 case, là giả sử không truyền vào has_watch thì kết quả sẽ ra sao.
Tất nhiên là không truyền vào has_watch thì default nó là false thì nó sẽ trùng 1 trong các số test của mình. Nhưng về cơ bản thì vẫn phải có case là không truyền vào cái gì cả

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@tuanpt-0634 tuanpt-0634 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
PHPUnit C1 is 100%, C2 is 50% => still can improve

Code Coverage Report:       
  2021-06-22 09:58:30       
                            
 Summary:                   
  Classes: 100.00% (16/16)  
  Methods: 100.00% (28/28)  
  Paths:   50.00% (45/90)   
  Branches:   100.00% (71/71)
  Lines:   100.00% (115/115)

Infection Mutation Testing MSI: 80%

Metrics:
         Mutation Score Indicator (MSI): 80%
         Mutation Code Coverage: 100%
         Covered Code MSI: 80%

Final result: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants