Skip to content

Commit 2a54b5b

Browse files
phan.nhan.datphan.nhan.dat
authored andcommitted
[EUV2][Datpn] Do Unit test homwork
1 parent 88402d0 commit 2a54b5b

File tree

19 files changed

+792
-4
lines changed

19 files changed

+792
-4
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Modules\Exercise03\Tests\Feature\Http\Controllers;
4+
5+
use Modules\Exercise03\Http\Controllers\ProductController;
6+
use Tests\TestCase;
7+
use Modules\Exercise03\Models\Product;
8+
use Modules\Exercise03\Services\ProductService;
9+
use Tests\SetupDatabaseTrait;
10+
11+
class ProductControllerTest extends TestCase
12+
{
13+
use SetupDatabaseTrait;
14+
15+
protected $productServiceMock;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->productServiceMock = $this->mock(ProductService::class);
22+
}
23+
24+
public function test_index_return_view()
25+
{
26+
$this->productServiceMock
27+
->shouldReceive('getAllProducts')
28+
->andReturn([]);
29+
30+
$url = action([ProductController::class, 'index']);
31+
$response = $this->get($url);
32+
33+
$response->assertViewIs('exercise03::index');
34+
$response->assertViewHas('products');
35+
}
36+
37+
public function test_checkout_success()
38+
{
39+
$url = action([ProductController::class, 'checkout']);
40+
$paramRequest = [
41+
1 => 7,
42+
2 => 9,
43+
];
44+
$this->productServiceMock->shouldReceive('calculateDiscount')->with($paramRequest)
45+
->andReturn(12);
46+
47+
$response = $this->post($url, ['total_products' => $paramRequest]);
48+
49+
$this->assertEquals($response->status(), 200);
50+
$this->assertEquals($response->getData(true), ['discount' => 12]);
51+
}
52+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Modules\Exercise03\Tests\Feature\Http\Requests;
4+
5+
use Tests\TestCase;
6+
use Illuminate\Support\Facades\Validator;
7+
use Modules\Exercise03\Http\Requests\CheckoutRequest;
8+
use Tests\SetupDatabaseTrait;
9+
use Illuminate\Support\Arr;
10+
11+
class CheckoutRequestTest extends TestCase
12+
{
13+
use SetupDatabaseTrait;
14+
15+
protected $checkoutRequest;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->checkoutRequest = new CheckoutRequest();
22+
}
23+
24+
public function test_it_contains_valid_rules()
25+
{
26+
$this->assertEquals([
27+
'total_products' => 'required|array',
28+
'total_products.*' => 'nullable|integer|min:0'
29+
], $this->checkoutRequest->rules());
30+
}
31+
32+
/**
33+
* @dataProvider provider_test_validation_invalid
34+
*/
35+
public function test_validation_fails_when_input_invalid($input)
36+
{
37+
$validator = Validator::make(['total_products' => $input['total_products']], $this->checkoutRequest->rules());
38+
39+
$this->assertTrue($validator->fails());
40+
}
41+
42+
function provider_test_validation_invalid()
43+
{
44+
return [
45+
[
46+
[
47+
'total_products' => null,
48+
]
49+
],
50+
[
51+
[
52+
'total_products' => [],
53+
]
54+
],
55+
[
56+
[
57+
'total_products' => [1 => -1],
58+
]
59+
],
60+
[
61+
[
62+
'total_products' => [1 => 5.23],
63+
]
64+
],
65+
[
66+
[
67+
'total_products' => [1 => 'xxxx'],
68+
]
69+
],
70+
];
71+
}
72+
73+
public function test_validation_success()
74+
{
75+
$request = new CheckoutRequest();
76+
$validator = Validator::make([
77+
'total_products' => [
78+
1 => 6,
79+
2 => 7,
80+
3 => 8,
81+
],
82+
], $request->rules());
83+
84+
$this->assertTrue($validator->passes());
85+
}
86+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tests\Unit\Models;
4+
5+
use Modules\Exercise03\Models\Product;
6+
use Tests\TestCase;
7+
use Modules\Exercise03\Database\Factories\ProductFactory;
8+
9+
class ProductTest extends TestCase
10+
{
11+
function test_new_factory()
12+
{
13+
$product = Product::newFactory();
14+
15+
$this->assertInstanceOf(ProductFactory::class, $product);
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tests\Unit\Repositories;
4+
5+
use Modules\Exercise03\Models\Product;
6+
use Modules\Exercise03\Repositories\ProductRepository;
7+
use Tests\TestCase;
8+
use Illuminate\Database\Eloquent\Collection;
9+
10+
class ProductRepositoryTest extends TestCase
11+
{
12+
public function test__construct()
13+
{
14+
$product = new Product();
15+
$repository = new ProductRepository($product);
16+
17+
$this->assertInstanceOf(ProductRepository::class, $repository);
18+
}
19+
20+
public function test_all()
21+
{
22+
$product = new Product();
23+
$repository = new ProductRepository($product);
24+
25+
$this->assertInstanceOf(Collection::class, $repository->all());
26+
}
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Tests\Unit\Services;
4+
5+
use Modules\Exercise03\Services\ProductService;
6+
use Modules\Exercise03\Repositories\ProductRepository;
7+
use Modules\Exercise03\Models\Product;
8+
use InvalidArgumentException;
9+
use Tests\SetupDatabaseTrait;
10+
use Tests\TestCase;
11+
12+
class ProductServiceTest extends TestCase
13+
{
14+
use SetupDatabaseTrait;
15+
16+
protected $productService;
17+
18+
protected $product;
19+
20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
$this->product = new Product();
24+
$repository = new ProductRepository($this->product);
25+
$this->productService = new ProductService($repository);
26+
}
27+
28+
public function test_it_throw_exception_when_product_type_does_not_exist()
29+
{
30+
$this->expectException(InvalidArgumentException::class);
31+
$this->productService->calculateDiscount([
32+
Product::WHITE_SHIRT_TYPE => -1111,
33+
]);
34+
}
35+
36+
public function test_case_calculate_discount_return_caravat_white_shirt()
37+
{
38+
$discount = $this->expectException(InvalidArgumentException::class);
39+
$this->productService->calculateDiscount([
40+
Product::CRAVAT_TYPE => 12,
41+
Product::WHITE_SHIRT_TYPE => 13,
42+
Product::OTHER_TYPE => -14,
43+
]);
44+
45+
$this->assertEquals(ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT, $discount);
46+
}
47+
48+
public function test_case_calculate_discount_return_quantity_discount()
49+
{
50+
$discount = $this->productService->calculateDiscount([
51+
Product::CRAVAT_TYPE => 5,
52+
Product::WHITE_SHIRT_TYPE => 4,
53+
Product::OTHER_TYPE => 0,
54+
]);
55+
56+
$this->assertEquals(12, $discount);
57+
}
58+
59+
public function test_get_all_products()
60+
{
61+
$product = Product::newFactory();
62+
63+
$this->cravat = $product->cravat()->create();
64+
$this->whiteShirt = $product->whiteShirt()->create();
65+
$this->other = $product->other()->create();
66+
67+
$products = $this->productService->getAllProducts();
68+
69+
$this->assertEquals(3, $products->count());
70+
$this->assertEquals($products[0]['name'], $this->cravat->name);
71+
$this->assertEquals($products[1]['name'], $this->whiteShirt->name);
72+
$this->assertEquals($products[2]['name'], $this->other->name);
73+
}
74+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use Modules\Exercise04\Http\Controllers\CalendarController;
6+
use Tests\TestCase;
7+
use Modules\Exercise04\Services\CalendarService;
8+
use Tests\SetupDatabaseTrait;
9+
10+
class CalendarControllerTest extends TestCase
11+
{
12+
use SetupDatabaseTrait;
13+
14+
protected $calendarServiceMock;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->calendarServiceMock = $this->mock(CalendarService::class);
21+
}
22+
23+
function test_it_index()
24+
{
25+
$url = action([CalendarController::class, 'index']);
26+
27+
$this->calendarServiceMock->shouldReceive('getDateClass')->andReturn(CalendarService::COLOR_BLACK);
28+
29+
$response = $this->get($url);
30+
31+
$response->assertViewIs('exercise04::calendar');
32+
$response->assertViewHas('calendars');
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Modules\Exercise04\Tests\Unit\Services;
4+
5+
use Carbon\Carbon;
6+
use Modules\Exercise04\Services\CalendarService;
7+
use Tests\TestCase;
8+
9+
class CalendarServiceTest extends TestCase
10+
{
11+
protected $calendarService;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setup();
16+
17+
$this->calendarService = new CalendarService();
18+
}
19+
20+
/**
21+
* @dataProvider provideData
22+
*/
23+
public function test_get_date_class($date, $class)
24+
{
25+
$response =$this->calendarService->getDateClass($date, ['2021-05-20']);
26+
27+
$this->assertEquals($class, $response);
28+
}
29+
30+
function provideData()
31+
{
32+
return [
33+
[
34+
Carbon::createFromFormat('Y-m-d', '2021-05-23'),
35+
CalendarService::COLOR_RED,
36+
],
37+
[
38+
Carbon::createFromFormat('Y-m-d', '2021-05-22'),
39+
CalendarService::COLOR_BLUE,
40+
],
41+
[
42+
Carbon::createFromFormat('Y-m-d', '2021-05-20'),
43+
CalendarService::COLOR_RED,
44+
],
45+
];
46+
}
47+
}

0 commit comments

Comments
 (0)