[TruongBT] - UT Unit 2 - #25
Conversation
| 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); | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Method này có thể nói cụ thể hơn là nó return json, ko phải return view
There was a problem hiding this comment.
There was a problem hiding this comment.
Đây đang test method checkout mà anh
| $dataAll = ['abc' => 'def']; | ||
| $this->productModel->shouldReceive('all')->andReturn($dataAll); | ||
|
|
||
| $this->assertEquals($dataAll, $this->productRepository->all()); |
There was a problem hiding this comment.
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']; |
There was a problem hiding this comment.
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
| 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); | ||
| } |
There was a problem hiding this comment.
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)
| /** | ||
| * @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, | ||
| ], | ||
| ]; | ||
| } |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Expect là cần phải đặt tên "dài" hơn
https://sun-unit-test-training.github.io/php-testing-guideline/06-checklist/#1-self-describing-test-method
| 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, | ||
| ], | ||
| ] | ||
| ]; | ||
| } |
There was a problem hiding this comment.
Có gì đó sai sai? Provider này chỉ đang return 1 data set
|
|
||
| public function test_index() | ||
| { | ||
| $this->calendarService->shouldReceive('getDateClass')->andReturn(1); |
There was a problem hiding this comment.
Đoạn mock này mình không nên chỉ shouldReceive và andReturn 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
| list($minBill2, $freeTime2) = CalculateService::CASE_2; | ||
| $freeTime = CalculateService::FREE_TIME_FOR_MOVIE; | ||
|
|
||
| return [ |
There was a problem hiding this comment.
Ở đâ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ả
tuanpt-0634
left a comment
There was a problem hiding this comment.
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
Purpose/Notes
Screenshot
Ex 03

Ex 04

Ex 05

Ex 07

Ex 06

Checklist (*)