-
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
Open
tuanvh-0174
wants to merge
7
commits into
sun-unit-test-training:master
Choose a base branch
from
tuanvh-0174:vu.huy.tuan_unittest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
Modules/Exercise03/Tests/Feature/Http/Controllers/ProductControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
Modules/Exercise03/Tests/Feature/Http/Requests/CheckoutRequestTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
Modules/Exercise03/Tests/Feature/Repositories/ProductRepositoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
104 changes: 104 additions & 0 deletions
104
Modules/Exercise03/Tests/Feature/Services/ProductServiceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), []); | ||
| } | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
Modules/Exercise04/Tests/Feature/Http/Controllers/CalendarControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
|
|
||
| } |
49 changes: 49 additions & 0 deletions
49
Modules/Exercise04/Tests/Feature/Http/Services/CalendarServiceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ], | ||
|
|
||
| ]; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.