-
Notifications
You must be signed in to change notification settings - Fork 59
Sontt - do homework #12
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
sontt-1215
wants to merge
4
commits into
sun-unit-test-training:master
Choose a base branch
from
sontt-1215:tran_thanh_son_do_homework
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 all commits
Commits
Show all changes
4 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
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
58 changes: 58 additions & 0 deletions
58
Modules/Exercise03/Tests/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,58 @@ | ||
| <?php | ||
| namespace Modules\Exercise03\Tests\Http\Controllers; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Services\ProductService; | ||
| use Modules\Exercise03\Http\Controllers\ProductController; | ||
| use Illuminate\Contracts\Support\Renderable; | ||
| use Illuminate\View\View; | ||
| use Modules\Exercise03\Http\Requests\CheckoutRequest; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Modules\Exercise03\Models\Product; | ||
| use Modules\Exercise03\Repositories\ProductRepository; | ||
|
|
||
| class ProductControllerTest extends TestCase | ||
| { | ||
| public function testConstruct() | ||
| { | ||
| $product = new Product; | ||
| $repository = new ProductRepository($product); | ||
| $service = new ProductService($repository); | ||
| $controller = new ProductController($service); | ||
|
|
||
| $this->assertInstanceOf(ProductController::class, $controller); | ||
| } | ||
|
|
||
| public function testIndex() | ||
| { | ||
| $collection = Mockery::mock(Collection::class); | ||
|
|
||
| $service = Mockery::mock(ProductService::class); | ||
| $service->shouldReceive('getAllProducts')->once()->andReturn($collection); | ||
|
|
||
| $controller = new ProductController($service); | ||
|
|
||
| $this->assertInstanceOf(View::class, $controller->index()); | ||
| } | ||
|
|
||
| public function testCheckout() | ||
| { | ||
| $input = [ | ||
| 1 => 1, | ||
| 2 => 2, | ||
| 3 => 3, | ||
| ]; | ||
| $response = 5; | ||
|
|
||
| $request = Mockery::mock(CheckoutRequest::class); | ||
| $request->shouldReceive('input')->once()->with('total_products')->andReturn($input); | ||
|
|
||
| $service = Mockery::mock(ProductService::class); | ||
| $service->shouldReceive('calculateDiscount')->once()->with($input)->andReturn($response); | ||
|
|
||
| $controller = new ProductController($service); | ||
|
|
||
| $this->assertInstanceOf(JsonResponse::class, $controller->checkout($request)); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
Modules/Exercise03/Tests/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,29 @@ | ||
| <?php | ||
| namespace Modules\Exercise03\Tests\Http\Requests; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Tests\TestValidation; | ||
| use Modules\Exercise03\Http\Requests\CheckoutRequest; | ||
|
|
||
| class CheckoutRequestTest extends TestCase | ||
| { | ||
| use TestValidation; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->rules = (new CheckoutRequest())->rules(); | ||
| $this->validator = $this->app['validator']; | ||
| } | ||
|
|
||
| public function testTotalProducts() | ||
| { | ||
| $this->assertTrue($this->validateField('total_products.*', [ | ||
| 1, | ||
| 2, | ||
| 3, | ||
| ]) | ||
| ); | ||
| } | ||
| } |
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,16 @@ | ||
| <?php | ||
| namespace Modules\Exercise03\Tests\Models; | ||
|
|
||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Models\Product; | ||
| use Modules\Exercise03\Database\Factories\ProductFactory; | ||
|
|
||
| class ProductTest extends TestCase | ||
| { | ||
| public function testNewFactory() | ||
| { | ||
| $product = Product::newFactory(); | ||
|
|
||
| $this->assertInstanceOf(ProductFactory::class, $product); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
Modules/Exercise03/Tests/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,27 @@ | ||
| <?php | ||
| namespace Modules\Exercise03\Tests\Repositories; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Models\Product; | ||
| use Modules\Exercise03\Repositories\ProductRepository; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Support\Collection; | ||
|
|
||
| class PriceRepositoryTest extends TestCase | ||
| { | ||
| public function testConstruct() | ||
| { | ||
| $product = new Product; | ||
| $repository = new ProductRepository($product); | ||
|
|
||
| $this->assertInstanceOf(ProductRepository::class, $repository); | ||
| } | ||
|
|
||
| public function testAll() | ||
| { | ||
| $product = new Product; | ||
| $repository = new ProductRepository($product); | ||
| $this->assertInstanceOf(Collection::class, $repository->all()); | ||
| } | ||
| } |
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,78 @@ | ||
| <?php | ||
| namespace App\Modules\Exercise03\Tests\Services; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Models\Product; | ||
| use Modules\Exercise03\Repositories\ProductRepository; | ||
| use Modules\Exercise03\Services\ProductService; | ||
| use Illuminate\Support\Collection; | ||
| use InvalidArgumentException; | ||
|
|
||
| class PriceServiceTest extends TestCase | ||
| { | ||
| public function testConstruct() | ||
| { | ||
| $product = new Product; | ||
| $productRepository = new ProductRepository($product); | ||
| $service = new ProductService($productRepository); | ||
|
|
||
| $this->assertInstanceOf(ProductService::class, $service); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider providerTestCalculateDiscount | ||
| */ | ||
| public function testCalculateDiscount($input, $expect) | ||
| { | ||
| $product = new Product; | ||
| $repository = new ProductRepository($product); | ||
| $service = new ProductService($repository); | ||
| $response = $service->calculateDiscount($input); | ||
|
|
||
| $this->assertEquals($expect, $response); | ||
| } | ||
|
|
||
| public function testCalculateDiscountResponseIsException() | ||
| { | ||
| $this->expectException(InvalidArgumentException::class); | ||
| $input = [ | ||
| 1 => -1, | ||
| 2 => 1, | ||
| 3 => 2, | ||
| ]; | ||
| $product = new Product; | ||
| $repository = new ProductRepository($product); | ||
| $service = new ProductService($repository); | ||
| $service->calculateDiscount($input); | ||
| } | ||
|
|
||
| public function testGetAllProducts() | ||
| { | ||
| $product = new Product; | ||
| $repository = new ProductRepository($product); | ||
| $service = new ProductService($repository); | ||
|
|
||
| $this->assertInstanceOf(Collection::class, $service->getAllProducts()); | ||
| } | ||
|
|
||
| public function providerTestCalculateDiscount() | ||
| { | ||
| return [ | ||
| $this->exampleData(1, 3, 2, ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT), | ||
| $this->exampleData(5, 5, 5, ProductService::QUANTITY_DISCOUNT + ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT), | ||
| ]; | ||
| } | ||
|
|
||
| public function exampleData($cravat, $whiteShirt, $others, $expert) | ||
| { | ||
| return [ | ||
| [ | ||
| 1 => $cravat, | ||
| 2 => $whiteShirt, | ||
| 3 => $others, | ||
| ], | ||
| $expert, | ||
| ]; | ||
| } | ||
| } |
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,30 @@ | ||
| <?php | ||
| namespace Modules\Exercise04\Tests\Http\Controllers; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise04\Services\CalendarService; | ||
| use Modules\Exercise04\Http\Controllers\CalendarController; | ||
| use Illuminate\Contracts\Support\Renderable; | ||
| use Illuminate\View\View; | ||
| use Illuminate\Http\JsonResponse; | ||
|
|
||
| class CalendarControllerTest extends TestCase | ||
| { | ||
| public function testConstruct() | ||
| { | ||
| $service = new CalendarService; | ||
| $controller = new CalendarController($service); | ||
|
|
||
| $this->assertInstanceOf(CalendarController::class, $controller); | ||
| } | ||
|
|
||
| public function testIndex() | ||
| { | ||
| $service = Mockery::mock(CalendarService::class); | ||
| $service->shouldReceive('getDateClass')->times(30)->andReturn('any_color'); | ||
|
|
||
| $controller = new CalendarController($service); | ||
| $this->assertInstanceOf(View::class, $controller->index()); | ||
| } | ||
| } |
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 App\Modules\Exercise04\Tests\Services; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise04\Services\CalendarService; | ||
| use Illuminate\Support\Carbon; | ||
|
|
||
| class CalendarServiceTest extends TestCase | ||
| { | ||
|
|
||
| /** | ||
| * @dataProvider providerTestGetDateClass | ||
| */ | ||
| public function testGetDateClass($input, $expect) | ||
| { | ||
| $holidays = ['2021-04-25']; | ||
| $service = new CalendarService; | ||
| $response = $service->getDateClass($input, $holidays); | ||
|
|
||
| $this->assertEquals($expect, $response); | ||
| } | ||
|
|
||
| public function providerTestGetDateClass() | ||
| { | ||
| return [ | ||
| [ | ||
| new Carbon('2021-04-21'), | ||
| CalendarService::COLOR_BLACK, | ||
| ], | ||
| [ | ||
| new Carbon('2021-04-25'), | ||
| CalendarService::COLOR_RED, | ||
| ], | ||
| [ | ||
| new Carbon('2021-04-24'), | ||
| CalendarService::COLOR_BLUE, | ||
| ], | ||
| [ | ||
| new Carbon('2021-04-25'), | ||
| CalendarService::COLOR_RED, | ||
| ], | ||
| ]; | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
Modules/Exercise05/Tests/Http/Controllers/Exercise05ControllerTest.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,54 @@ | ||
| <?php | ||
| namespace Modules\Exercise05\Tests\Http\Controllers; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise05\Services\OrderService; | ||
| use Modules\Exercise05\Http\Controllers\Exercise05Controller; | ||
| use Illuminate\Contracts\Support\Renderable; | ||
| use Modules\Exercise05\Http\Requests\OrderRequest; | ||
| use Illuminate\View\View; | ||
| use Illuminate\Http\JsonResponse; | ||
|
|
||
| class Exercise05ControllerTest extends TestCase | ||
| { | ||
| public function testConstruct() | ||
| { | ||
| $service = new OrderService; | ||
| $controller = new Exercise05Controller($service); | ||
|
|
||
| $this->assertInstanceOf(Exercise05Controller::class, $controller); | ||
| } | ||
|
|
||
| public function testIndex() | ||
| { | ||
| $service = new OrderService; | ||
| $controller = new Exercise05Controller($service); | ||
|
|
||
| $this->assertInstanceOf(View::class, $controller->index()); | ||
| } | ||
|
|
||
| public function testStore() | ||
| { | ||
| $input = [ | ||
| 'price' => 1500, | ||
| 'option_receive' => '2', | ||
| 'option_coupon' => '1', | ||
| ]; | ||
| $response = [ | ||
| 'price' => 1200, | ||
| 'discount_pizza' => null, | ||
| 'discount_potato' => null, | ||
| ]; | ||
|
|
||
| $request = Mockery::mock(OrderRequest::class); | ||
| $request->shouldReceive('only')->once()->andReturn($input); | ||
|
|
||
| $service = Mockery::mock(OrderService::class); | ||
| $service->shouldReceive('handleDiscount')->once()->with($input)->andReturn($response); | ||
|
|
||
| $controller = new Exercise05Controller($service); | ||
|
|
||
| $this->assertInstanceOf(View::class, $controller->store($request)); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
Modules/Exercise05/Tests/Http/Requests/OrderRequestTest.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,34 @@ | ||
| <?php | ||
| namespace Modules\Exercise05\Tests\Http\Requests; | ||
|
|
||
| use Mockery; | ||
| use Tests\TestCase; | ||
| use Tests\TestValidation; | ||
| use Modules\Exercise05\Http\Requests\OrderRequest; | ||
|
|
||
| class OrderRequestTest extends TestCase | ||
| { | ||
| use TestValidation; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->rules = (new OrderRequest())->rules(); | ||
| $this->validator = $this->app['validator']; | ||
| } | ||
|
|
||
| public function testPrice() | ||
| { | ||
| $this->assertTrue($this->validateField('price', 1200)); | ||
|
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. cần test cả những case fail nữa e nhé |
||
| } | ||
|
|
||
| public function testOptionReceive() | ||
| { | ||
| $this->assertTrue($this->validateField('option_receive', '1')); | ||
| } | ||
|
|
||
| public function testOptionCoupoin() | ||
|
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. testOptionCoupoin ==> sai chính tả này |
||
| { | ||
| $this->assertTrue($this->validateField('option_coupon', '1')); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
indent e ơi