-
Notifications
You must be signed in to change notification settings - Fork 59
[EUV2][TuanVH] Create UT for [Exercise03 ~ Exercise07] #40
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise03\Tests\Feature\Http\Controllers; | ||
|
||
use Illuminate\View\View; | ||
use Modules\Exercise03\Http\Controllers\ProductController; | ||
use Modules\Exercise03\Http\Requests\CheckoutRequest; | ||
use Modules\Exercise03\Services\ProductService; | ||
use Tests\TestCase; | ||
|
||
class ProductControllerTest extends TestCase | ||
{ | ||
private $productService; | ||
private $productController; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->productService = \Mockery::mock(ProductService::class); | ||
$this->productController = new ProductController( | ||
$this->productService | ||
); | ||
} | ||
|
||
/** | ||
* A basic feature test example. | ||
* | ||
* @return void | ||
*/ | ||
public function testIndex() | ||
{ | ||
$products = [ | ||
'name' => 'name', | ||
'status' => 1 | ||
]; | ||
|
||
$this->productService->shouldReceive('getAllProducts') | ||
->andReturn($products); | ||
$response = $this->productController->index(); | ||
$this->assertInstanceOf(View::class, $response); | ||
$this->assertEquals('exercise03::index', $response->getName()); | ||
} | ||
|
||
public function testCheckout() { | ||
$input['total_products'] = [ | ||
1 => 1, | ||
2 => 2, | ||
3 => 3 | ||
]; | ||
|
||
$this->productService->shouldReceive('calculateDiscount') | ||
->with($input) | ||
->andReturn(5); | ||
$mockRequest = \Mockery::mock(CheckoutRequest::class); | ||
$mockRequest->shouldReceive('input')->andReturn($input); | ||
$response = $this->productController->checkout($mockRequest); | ||
$this->assertEquals(['discount' => 5], $response->getOriginalContent()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise03\Tests\Feature\Http\Requests; | ||
|
||
use Illuminate\Support\Facades\Validator; | ||
use Modules\Exercise03\Http\Requests\CheckoutRequest; | ||
use Tests\TestCase; | ||
|
||
class CheckoutRequestTest extends TestCase | ||
{ | ||
private $request; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->request = new CheckoutRequest(); | ||
} | ||
|
||
public function testRules() | ||
{ | ||
$this->assertEquals([ | ||
'total_products' => 'required|array', | ||
'total_products.*' => 'nullable|integer|min:0', | ||
], | ||
$this->request->rules() | ||
); | ||
} | ||
|
||
public function testInvalidData() | ||
{ | ||
$validator = Validator::make([], $this->request->rules()); | ||
$this->assertTrue($validator->fails()); | ||
} | ||
|
||
public function testValidData() | ||
{ | ||
$data = [ | ||
'total_products' => [0, 1, 2, 3], | ||
]; | ||
|
||
$validator = Validator::make($data, $this->request->rules()); | ||
$this->assertTrue($validator->passes()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise03\Tests\Feature\Model; | ||
|
||
use Modules\Exercise03\Models\Product; | ||
use Tests\TestCase; | ||
|
||
class ProductTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testNewFactory() | ||
{ | ||
$product = Product::factory()->make(); | ||
$this->assertInstanceOf(Product::class, $product); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise03\Tests\Feature\Repositories; | ||
|
||
use Illuminate\Support\Collection; | ||
use Modules\Exercise03\Models\Product; | ||
use Modules\Exercise03\Repositories\ProductRepository; | ||
use Tests\TestCase; | ||
|
||
class ProductRepositoryTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testAll() | ||
{ | ||
$repository = new ProductRepository(new Product()); | ||
$getAll = $repository->all(); | ||
$this->assertInstanceOf(Collection::class, $getAll); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise03\Tests\Feature\Services; | ||
|
||
use InvalidArgumentException; | ||
use Illuminate\Support\Collection; | ||
use Modules\Exercise03\Models\Product; | ||
use Modules\Exercise03\Repositories\ProductRepository; | ||
use Modules\Exercise03\Services\ProductService; | ||
use Tests\TestCase; | ||
|
||
class ProductServiceTest extends TestCase | ||
{ | ||
private $productService; | ||
private $mockProductRepository; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->mockProductRepository = $this->mock(ProductRepository::class); | ||
$this->productService = new ProductService($this->mockProductRepository); | ||
} | ||
|
||
/** | ||
* @dataProvider provideData | ||
*/ | ||
|
||
public function testCalculateDiscount($totalProducts, $expectValue, $testCase = 'OK') | ||
{ | ||
if ($testCase === 'NG') { | ||
$this->expectException(InvalidArgumentException::class); | ||
} | ||
|
||
$response = $this->productService->calculateDiscount($totalProducts); | ||
$this->assertEquals($response, $expectValue); | ||
} | ||
|
||
public function provideData() | ||
{ | ||
return [ | ||
[ | ||
[ | ||
1 => 0, | ||
2 => 0, | ||
3 => 0 | ||
], | ||
0 | ||
], | ||
[ | ||
[ | ||
1 => 1, | ||
2 => 2, | ||
3 => 2 | ||
], | ||
5 | ||
], | ||
[ | ||
[ | ||
1 => 1, | ||
2 => 0, | ||
3 => 7 | ||
], | ||
7 | ||
], | ||
[ | ||
[ | ||
1 => 1, | ||
2 => 2, | ||
3 => 5 | ||
], | ||
12 | ||
], | ||
[ | ||
[ | ||
1 => -1, | ||
2 => 2, | ||
3 => 9 | ||
], | ||
0, | ||
'NG' | ||
], | ||
[ | ||
[ | ||
1 => 1, | ||
2 => -2, | ||
3 => 2 | ||
], | ||
0, | ||
'NG' | ||
], | ||
]; | ||
} | ||
|
||
public function testGetAllProducts() | ||
{ | ||
$this->mockProductRepository | ||
->shouldReceive('all') | ||
->andReturn([]); | ||
|
||
$this->assertEquals($this->productService->getAllProducts(), []); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise04\Tests\Feature\Http\Controllers; | ||
|
||
use Illuminate\View\View; | ||
use Modules\Exercise04\Http\Controllers\CalendarController; | ||
use Modules\Exercise04\Services\CalendarService; | ||
use Tests\TestCase; | ||
|
||
class CalendarControllerTest extends TestCase | ||
{ | ||
private $calendarController; | ||
private $mockCalendarService; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->mockCalendarService = $this->mock(CalendarService::class); | ||
$this->calendarController = new CalendarController($this->mockCalendarService); | ||
} | ||
|
||
public function testIndex() | ||
{ | ||
$this->mockCalendarService | ||
->shouldReceive('getDateClass') | ||
->andReturn(CalendarService::COLOR_BLUE); | ||
|
||
$response = $this->calendarController->index(); | ||
$this->assertInstanceOf(View::class, $response); | ||
$this->assertEquals('exercise04::calendar', $response->getName()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise03\Tests\Feature\Http\Services; | ||
|
||
use Carbon\Carbon; | ||
use Modules\Exercise04\Services\CalendarService; | ||
use Tests\TestCase; | ||
|
||
class CalendarServiceTest extends TestCase | ||
{ | ||
private $calendarService; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->calendarService = new CalendarService(); | ||
} | ||
|
||
/** | ||
* @param $date | ||
* @dataProvider provideData | ||
*/ | ||
public function testGetDateClass($date, $expectValue) | ||
{ | ||
$holiday = ['2021-05-30']; | ||
|
||
$class = $this->calendarService->getDateClass($date, $holiday); | ||
$this->assertEquals($class, $expectValue); | ||
} | ||
|
||
public function provideData() | ||
{ | ||
return [ | ||
[ | ||
Carbon::createFromDate(2021, 5, 23), | ||
CalendarService::COLOR_RED, | ||
], | ||
[ | ||
Carbon::createFromDate(2021, 5, 29), | ||
CalendarService::COLOR_BLUE, | ||
], | ||
[ | ||
Carbon::createFromDate(2021, 5, 30), | ||
CalendarService::COLOR_RED, | ||
], | ||
|
||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Modules\Tests\Exercise05\Tests\Feature\Http\Controllers; | ||
|
||
use Illuminate\View\View; | ||
use Modules\Exercise05\Http\Controllers\Exercise05Controller; | ||
use Modules\Exercise05\Http\Requests\OrderRequest; | ||
use Modules\Exercise05\Services\OrderService; | ||
use Tests\TestCase; | ||
|
||
class Exercise05ControllerTest extends TestCase | ||
{ | ||
private $controller; | ||
private $mockOrderService; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->mockOrderService = $this->mock(OrderService::class); | ||
$this->controller = new Exercise05Controller($this->mockOrderService); | ||
} | ||
|
||
public function testIndex() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tên function nên thể hiện được a test case nào và expect của test case của test case a nhé There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a thấy k cần thiết sửa đâu, ở đây mình đang test theo file, theo method mà, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Việc đặt tên nó khá quan trọng ạ . Đây là case đơn giản thì mình có thể viết ngẵn gọn được
|
||
{ | ||
$response = $this->controller->index(); | ||
$this->assertInstanceOf(View::class, $response); | ||
$this->assertEquals('exercise05::index', $response->getName()); | ||
} | ||
|
||
public function testStore() | ||
{ | ||
$detailOrder = [ | ||
'price' => 100, | ||
'option_receive' => 1, | ||
'option_coupon' => 1 | ||
]; | ||
|
||
$mockRequest = $this->mock(OrderRequest::class); | ||
$mockRequest->shouldReceive('only') | ||
->andReturn($detailOrder); | ||
|
||
$this->mockOrderService | ||
->shouldReceive('handleDiscount') | ||
->andReturn(1); | ||
|
||
$res = $this->controller->store($mockRequest); | ||
$this->assertEquals('exercise05::detail', $res->getName()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anh ơi với bài này thì em nghĩ nên tách mỗi case ra một hàm để còn xem là với trường hợp ngày lễ có đúng màu đỏ, với ngày cn có đúng màu đỏ, .v.v. Ý là cho meaning hơn ấy ạ.
Với anh thiếu case ngày thường color là BLACK ạ
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.