-
Notifications
You must be signed in to change notification settings - Fork 59
[ThuLT][U2]Do homework #22
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
thult-1460
wants to merge
6
commits into
sun-unit-test-training:master
Choose a base branch
from
thult-1460:dohomework
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
6 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Http\Controllers; | ||
|
|
||
| use Modules\Exercise03\Http\Controllers\ProductController; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Models\Product; | ||
| use Modules\Exercise03\Services\ProductService; | ||
| use Tests\SetupDatabaseTrait; | ||
|
|
||
| class ProductControllerTest extends TestCase | ||
| { | ||
| use SetupDatabaseTrait; | ||
|
|
||
| protected $productServiceMock; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Laravel helper: mock and bind to service container | ||
| $this->productServiceMock = $this->mock(ProductService::class); | ||
| } | ||
|
|
||
| function test_it_show_index() | ||
| { | ||
| $url = action([ProductController::class, 'index']); | ||
|
|
||
| $product = new Product(['name' => 'producttest', 'type' => 1]); | ||
|
|
||
| $this->productServiceMock->shouldReceive('getAllProducts')->andReturn([$product]); | ||
|
|
||
| $response = $this->get($url); | ||
|
|
||
| $response->assertViewIs('exercise03::index'); | ||
| $response->assertViewHas('products'); | ||
| } | ||
|
|
||
| public function test_checkout_success() | ||
| { | ||
| $url = action([ProductController::class, 'checkout']); | ||
| $totalProduct = [ | ||
| 'total_products' => [ | ||
| 1 => 5, | ||
| 2 => 5, | ||
| 3 => 5, | ||
| ], | ||
| ]; | ||
| $this->productServiceMock->shouldReceive('calculateDiscount')->with([ | ||
| 1 => 5, | ||
| 2 => 5, | ||
| 3 => 5, | ||
| ]) | ||
| ->andReturn(12); | ||
|
|
||
| $response = $this->post($url, $totalProduct); | ||
|
|
||
|
|
||
| $response->assertStatus(200) | ||
| ->assertJsonPath( | ||
| 'discount', | ||
| 12, | ||
| ); | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
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,78 @@ | ||
| <?php | ||
|
|
||
| namespace Modules\Exercise03\Tests\Feature\Http\Requests; | ||
|
|
||
| use Tests\TestCase; | ||
| use Illuminate\Support\Facades\Validator; | ||
| use Modules\Exercise03\Http\Requests\CheckoutRequest; | ||
| use Tests\SetupDatabaseTrait; | ||
|
|
||
| class CheckoutRequestTest extends TestCase | ||
| { | ||
| use SetupDatabaseTrait; | ||
|
|
||
| public function test_it_contain_default_rules() | ||
| { | ||
| $request = new CheckoutRequest(); | ||
|
|
||
| $this->assertEquals([ | ||
| 'total_products' => 'required|array', | ||
| 'total_products.*' => 'nullable|integer|min:0', | ||
| ], $request->rules()); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provider_test_validation_wrong | ||
| */ | ||
| public function test_validation_fails_when_data_wrong($input) | ||
| { | ||
| $request = new CheckoutRequest(); | ||
|
|
||
| $validator = Validator::make([ | ||
| 'total_products' => $input['total_products'], | ||
| ], $request->rules()); | ||
|
|
||
| $this->assertTrue($validator->fails()); | ||
| } | ||
|
|
||
| function provider_test_validation_wrong() | ||
| { | ||
| return [ | ||
| [ | ||
| [ | ||
| 'total_products' => null, | ||
| ] | ||
| ], | ||
| [ | ||
| [ | ||
| 'total_products' => [], | ||
| ] | ||
| ], | ||
| [ | ||
| [ | ||
| 'total_products' => [1 => -1], | ||
| ] | ||
| ], | ||
| [ | ||
| [ | ||
| 'total_products' => [1 => 5.5], | ||
| ] | ||
| ], | ||
| [ | ||
| [ | ||
| 'total_products' => [1 => 'aespa'], | ||
| ] | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function test_validation_success() | ||
| { | ||
| $request = new CheckoutRequest(); | ||
| $validator = Validator::make([ | ||
| 'total_products' => [1 => 10], | ||
| ], $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,17 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit\Models; | ||
|
|
||
| use Modules\Exercise03\Models\Product; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Database\Factories\ProductFactory; | ||
|
|
||
| class ProductTest extends TestCase | ||
| { | ||
| function test_create_new_factory() | ||
| { | ||
| $product = Product::newFactory(); | ||
|
|
||
| $this->assertInstanceOf(ProductFactory::class, $product); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
Modules/Exercise03/Tests/Unit/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,40 @@ | ||
| <?php | ||
|
|
||
| namespace Modules\Exercise03\Tests\Unit\Repositories; | ||
|
|
||
| use Tests\TestCase; | ||
| use Modules\Exercise03\Models\Product; | ||
| use Modules\Exercise03\Repositories\ProductRepository; | ||
| use Tests\SetupDatabaseTrait; | ||
|
|
||
| class ProductRepositoryTest extends TestCase | ||
| { | ||
| use SetupDatabaseTrait; | ||
|
|
||
| /** | ||
| * @var ProductRepository | ||
| */ | ||
| protected $repository; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->repository = new ProductRepository(new Product()); | ||
| } | ||
|
|
||
| public function test_all() | ||
| { | ||
| Product::factory()->cravat()->create([ | ||
| 'name' => 'Cà vạt', | ||
| 'thumbnail' => 'images/exercise03/cravat.jpg', | ||
| ]); | ||
|
|
||
| $product = new Product(); | ||
| $productRepository = new ProductRepository($product); | ||
|
|
||
| $products = $productRepository->all(); | ||
|
|
||
| $this->assertEquals($products->count(), 1); | ||
| } | ||
| } |
154 changes: 154 additions & 0 deletions
154
Modules/Exercise03/Tests/Unit/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,154 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit\Services; | ||
|
|
||
| use Modules\Exercise03\Services\ProductService; | ||
| use Modules\Exercise03\Repositories\ProductRepository; | ||
| use Modules\Exercise03\Models\Product; | ||
| use InvalidArgumentException; | ||
| use Tests\SetupDatabaseTrait; | ||
|
|
||
| use Tests\TestCase; | ||
|
|
||
| class ProductServiceTest extends TestCase | ||
| { | ||
| use SetupDatabaseTrait; | ||
|
|
||
| protected $calendarServiceMock; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->product = new Product(); | ||
| $repositoy = new ProductRepository($this->product); | ||
| $this->service = new ProductService($repositoy); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provider_test_alculate_discount | ||
| */ | ||
| public function test_calculate_discount($totalProducts, $discount) | ||
| { | ||
| $result = $this->service->calculateDiscount($totalProducts); | ||
|
|
||
| $this->assertEquals($result, $discount); | ||
| } | ||
|
|
||
| public function provider_test_alculate_discount() | ||
| { | ||
| return [ | ||
| [ | ||
| [ | ||
| 1 => 2, | ||
| 2 => 1, | ||
| 3 => 1, | ||
| ], 5 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 2, | ||
| 2 => 2, | ||
| 3 => 3, | ||
| ], 12 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 2, | ||
| 2 => 5, | ||
| 3 => 5, | ||
| ], 12 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 0, | ||
| 2 => 1, | ||
| 3 => 1, | ||
| ], 0 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 0, | ||
| 2 => 5, | ||
| 3 => 5, | ||
| ], 7 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 0, | ||
| 2 => 0, | ||
| 3 => 5, | ||
| ], 0 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 0, | ||
| 2 => 0, | ||
| 3 => 8, | ||
| ], 7 | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 0, | ||
| 2 => 0, | ||
| 3 => 0, | ||
| ], 0 | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provider_test_function_throw_exception | ||
| */ | ||
|
|
||
| public function test_function_throw_exception($totalProducts) | ||
| { | ||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->service->calculateDiscount($totalProducts); | ||
| } | ||
|
|
||
| public function provider_test_function_throw_exception() | ||
| { | ||
| return [ | ||
| [ | ||
| [ | ||
| 1 => -5, | ||
| 2 => 5, | ||
| 3 => 5, | ||
| ] | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => 5, | ||
| 2 => -5, | ||
| 3 => 5, | ||
| ] | ||
|
|
||
| ], | ||
| [ | ||
| [ | ||
| 1 => 5, | ||
| 2 => 5, | ||
| 3 => -5, | ||
| ] | ||
| ], | ||
| [ | ||
| [ | ||
| 1 => -5, | ||
| 2 => -5, | ||
| 3 => -5, | ||
| ] | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function test_get_all_products() | ||
| { | ||
| $this->product->create(['name' => 'product1', 'type' => 1]); | ||
| $this->product->create(['name' => 'product2', 'type' => 2]); | ||
| $this->product->create(['name' => 'product3', 'type' => 3]); | ||
|
|
||
| $products = $this->service->getAllProducts(); | ||
|
|
||
| $this->assertEquals($products->count(), 3); | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Http\Controllers; | ||
|
|
||
| use Modules\Exercise04\Http\Controllers\CalendarController; | ||
| use Tests\TestCase; | ||
| use Modules\Exercise04\Services\CalendarService; | ||
| use Tests\SetupDatabaseTrait; | ||
|
|
||
| class CalendarControllerTest extends TestCase | ||
| { | ||
| use SetupDatabaseTrait; | ||
|
|
||
| protected $calendarServiceMock; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Laravel helper: mock and bind to service container | ||
| $this->calendarServiceMock = $this->mock(CalendarService::class); | ||
| } | ||
|
|
||
| function test_it_index() | ||
| { | ||
| $url = action([CalendarController::class, 'index']); | ||
|
|
||
| $this->calendarServiceMock->shouldReceive('getDateClass')->andReturn('text-dark'); | ||
|
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. Đoạn mock này mình không nên chỉ |
||
|
|
||
| $response = $this->get($url); | ||
|
|
||
| $response->assertViewIs('exercise04::calendar'); | ||
| $response->assertViewHas('calendars'); | ||
| } | ||
| } | ||
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.
Ở đây chị thiếu miss mất 1 case.
Giả sử các trường hợp dưới đây không tồn tại thì ra sao?