Skip to content

Commit 3341afd

Browse files
ngo.thi.thuy.hoango.thi.thuy.hoa
authored andcommitted
[U3][HoaNTT] Exercise 2, Exercise 3, Exercise 8
1 parent 88402d0 commit 3341afd

File tree

15 files changed

+910
-1
lines changed

15 files changed

+910
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Modules\Exercise02\Tests\Unit\Http\Controllers;
4+
5+
use Tests\TestCase;
6+
use Modules\Exercise02\Http\Controllers\Exercise02Controller;
7+
use Mockery as m;
8+
use Modules\Exercise02\Services\ATMService;
9+
use Illuminate\View\View;
10+
use Modules\Exercise02\Http\Requests\ATMRequest;
11+
use Illuminate\Http\RedirectResponse;
12+
13+
class Exercise02ControllerTest extends TestCase
14+
{
15+
protected $exercise02Controller;
16+
protected $atmService;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
$this->atmService = m::mock(ATMService::class);
22+
23+
$this->exercise02Controller = new Exercise02Controller($this->atmService);
24+
}
25+
26+
public function test_index()
27+
{
28+
$result = $this->exercise02Controller->index();
29+
$this->assertInstanceOf(View::class, $result);
30+
$this->assertEquals('exercise02::index', $result->getName());
31+
}
32+
33+
public function test_take_atm_fee()
34+
{
35+
$request = m::mock(ATMRequest::class);
36+
37+
$request->shouldReceive('validated')->andreturn([
38+
'card_id' => 1,
39+
]);
40+
$this->atmService->shouldReceive('calculate')->andreturn(0);
41+
42+
$result = $this->exercise02Controller->takeATMFee($request);
43+
$this->assertInstanceOf(RedirectResponse::class, $result);
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Modules\Exercise02\Tests\Unit\Http\Requests;
4+
5+
use Illuminate\Support\Facades\Validator;
6+
use Tests\TestCase;
7+
use Modules\Exercise02\Http\Requests\ATMRequest;
8+
9+
class ATMRequestTest extends TestCase
10+
{
11+
protected $aTMRequest;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->aTMRequest = new ATMRequest();
17+
}
18+
19+
public function test_validate_fails()
20+
{
21+
$validator = Validator::make([], $this->aTMRequest->rules());
22+
$this->assertTrue($validator->fails());
23+
}
24+
25+
public function test_rules()
26+
{
27+
$rules = [
28+
'card_id' => 'required|exists:atms,card_id',
29+
];
30+
31+
$this->assertEquals($rules, $this->aTMRequest->rules());
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Modules\Exercise02\Tests\Unit\Models;
4+
5+
use Tests\ModelTestCase as TestCase;
6+
use Modules\Exercise02\Database\Factories\ATMFactory;
7+
use Modules\Exercise02\Models\ATM;
8+
use Mockery as m;
9+
10+
class ATMTest extends TestCase
11+
{
12+
public function test_model_configuration()
13+
{
14+
$this->runConfigurationAssertions(new ATM(), [
15+
'fillable' => [
16+
'card_id',
17+
'is_vip'
18+
],
19+
'casts' => [
20+
'is_vip' => 'boolean',
21+
'id' => 'int'
22+
],
23+
]);
24+
}
25+
26+
public function test_new_factory()
27+
{
28+
$model = m::Mock(ATM::class);
29+
$this->assertInstanceOf(ATMFactory::class, $model->newFactory());
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Modules\Exercise02\Tests\Unit\Http\Requests;
4+
5+
use Tests\TestCase;
6+
use Modules\Exercise02\Repositories\ATMRepository;
7+
use Mockery as m;
8+
use Modules\Exercise02\Models\ATM;
9+
10+
class ATMRepositoryTest extends TestCase
11+
{
12+
protected $atmRepository;
13+
protected $model;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->model = m::mock(ATM::class)->makePartial();
20+
$this->atmRepository = new ATMRepository($this->model);
21+
}
22+
23+
public function test_find()
24+
{
25+
$this->model->shouldReceive('where->first')->andreturn([]);
26+
27+
$result = $this->atmRepository->find(1);
28+
$this->assertEquals([], $result);
29+
}
30+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Modules\Exercise02\Tests\Unit\Services;
4+
5+
use Tests\TestCase;
6+
use Modules\Exercise02\Services\ATMService;
7+
use Modules\Exercise02\Repositories\ATMRepository;
8+
use Mockery as m;
9+
use Modules\Exercise02\Models\ATM;
10+
use InvalidArgumentException;
11+
use Carbon\Carbon;
12+
13+
class ATMRepositoryTest extends TestCase
14+
{
15+
protected $atmService;
16+
protected $atmRepository;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->atmRepository = m::mock(ATMRepository::class);
23+
$this->atmService = new ATMService($this->atmRepository);
24+
}
25+
26+
public function test_without_card()
27+
{
28+
$this->atmRepository->shouldReceive('find')->andreturn([]);
29+
$this->expectExceptionMessage('Card ID is invalid!');
30+
$this->expectException(InvalidArgumentException::class);
31+
32+
$this->atmService->calculate(1);
33+
}
34+
35+
public function test_is_vip_customer()
36+
{
37+
$atm = m::mock(ATM::class)->makePartial();
38+
$atm->is_vip = true;
39+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
40+
41+
$result = $this->atmService->calculate(1);
42+
$this->assertEquals(0, $result);
43+
}
44+
45+
public function test_is_holiday()
46+
{
47+
$atm = m::mock(ATM::class)->makePartial();
48+
Carbon::setTestNow(Carbon::parse('2021-01-01'));
49+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
50+
51+
$result = $this->atmService->calculate(1);
52+
$this->assertEquals(110, $result);
53+
}
54+
55+
public function test_is_saturday()
56+
{
57+
$atm = m::mock(ATM::class)->makePartial();
58+
Carbon::setTestNow(Carbon::parse('2021-04-24'));
59+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
60+
61+
$result = $this->atmService->calculate(1);
62+
$this->assertEquals(110, $result);
63+
}
64+
65+
public function test_is_sunday()
66+
{
67+
$atm = m::mock(ATM::class)->makePartial();
68+
Carbon::setTestNow(Carbon::parse('2021-04-25'));
69+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
70+
71+
$result = $this->atmService->calculate(1);
72+
$this->assertEquals(110, $result);
73+
}
74+
75+
public function test_is_holiday_and_is_saturday()
76+
{
77+
$atm = m::mock(ATM::class)->makePartial();
78+
Carbon::setTestNow(Carbon::parse('2021-05-01'));
79+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
80+
81+
$result = $this->atmService->calculate(1);
82+
$this->assertEquals(110, $result);
83+
}
84+
85+
public function test_is_holiday_and_is_sunday()
86+
{
87+
$atm = m::mock(ATM::class)->makePartial();
88+
Carbon::setTestNow(Carbon::parse('2017-04-30'));
89+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
90+
91+
$result = $this->atmService->calculate(1);
92+
$this->assertEquals(110, $result);
93+
}
94+
95+
public function test_normal_day_at_8h44 ()
96+
{
97+
$atm = m::mock(ATM::class)->makePartial();
98+
Carbon::setTestNow(Carbon::parse('2021-04-15 08:44:00'));
99+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
100+
101+
$result = $this->atmService->calculate(1);
102+
$this->assertEquals(110, $result);
103+
}
104+
105+
public function test_normal_day_at_8h45 ()
106+
{
107+
$atm = m::mock(ATM::class)->makePartial();
108+
Carbon::setTestNow(Carbon::parse('2021-04-15 08:45:00'));
109+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
110+
111+
$result = $this->atmService->calculate(1);
112+
$this->assertEquals(0, $result);
113+
}
114+
115+
public function test_normal_day_at_8h46 ()
116+
{
117+
$atm = m::mock(ATM::class)->makePartial();
118+
Carbon::setTestNow(Carbon::parse('2021-04-15 08:46:00'));
119+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
120+
121+
$result = $this->atmService->calculate(1);
122+
$this->assertEquals(0, $result);
123+
}
124+
125+
public function test_normal_day_at_17h58 ()
126+
{
127+
$atm = m::mock(ATM::class)->makePartial();
128+
Carbon::setTestNow(Carbon::parse('2021-04-15 17:58:00'));
129+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
130+
131+
$result = $this->atmService->calculate(1);
132+
$this->assertEquals(0, $result);
133+
}
134+
135+
public function test_normal_day_at_17h59 ()
136+
{
137+
$atm = m::mock(ATM::class)->makePartial();
138+
Carbon::setTestNow(Carbon::parse('2021-04-15 17:59:00'));
139+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
140+
141+
$result = $this->atmService->calculate(1);
142+
$this->assertEquals(0, $result);
143+
}
144+
145+
public function test_normal_day_at_18h00 ()
146+
{
147+
$atm = m::mock(ATM::class)->makePartial();
148+
Carbon::setTestNow(Carbon::parse('2021-04-15 18:00:00'));
149+
$this->atmRepository->shouldReceive('find')->andreturn($atm);
150+
151+
$result = $this->atmService->calculate(1);
152+
$this->assertEquals(110, $result);
153+
}
154+
}

Modules/Exercise03/Services/ProductService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function calculateDiscount($totalProducts)
4343
$others = $totalProducts[Product::OTHER_TYPE] ?? 0;
4444
$discount = 0;
4545

46-
if ($cravat < 0 || $whiteShirt < 0 || $others < 0) {
46+
if ($cravat <= 0 && $whiteShirt <= 0 && $others <= 0) {
4747
throw new InvalidArgumentException();
4848
}
4949

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Modules\Exercise03\Tests\Unit\Http\Controllers;
4+
5+
use Tests\TestCase;
6+
use Modules\Exercise03\Http\Controllers\ProductController;
7+
use Mockery as m;
8+
use Modules\Exercise03\Services\ProductService;
9+
use Modules\Exercise03\Http\Requests\CheckoutRequest;
10+
use Illuminate\View\View;
11+
use Illuminate\Http\JsonResponse;
12+
13+
class ProductControllerTest extends TestCase
14+
{
15+
protected $productController;
16+
protected $productService;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->productService = m::mock(ProductService::class);
23+
$this->productController = new ProductController($this->productService);
24+
}
25+
26+
public function test_index()
27+
{
28+
$this->productService->shouldReceive('getAllProducts')->andreturn([]);
29+
30+
$result = $this->productController->index();
31+
$this->assertInstanceOf(View::class, $result);
32+
$this->assertEquals('exercise03::index', $result->getName());
33+
}
34+
35+
public function test_checkout()
36+
{
37+
$request = m::mock(CheckoutRequest::class);
38+
39+
$request->shouldReceive('input')->andreturn([
40+
'total_products' => 1,
41+
]);
42+
$this->productService->shouldReceive('calculateDiscount')->andreturn([]);
43+
44+
$result = $this->productController->checkout($request);
45+
$this->assertInstanceOf(JsonResponse::class, $result);
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Modules\Exercise03\Tests\Unit\Http\Requests;
4+
5+
use Illuminate\Support\Facades\Validator;
6+
use Tests\TestCase;
7+
use Modules\Exercise03\Http\Requests\CheckoutRequest;
8+
9+
class CheckoutRequestTest extends TestCase
10+
{
11+
protected $checkoutRequest;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->checkoutRequest = new CheckoutRequest();
17+
}
18+
19+
public function test_validate_fails()
20+
{
21+
$validator = Validator::make([], $this->checkoutRequest->rules());
22+
$this->assertTrue($validator->fails());
23+
}
24+
25+
public function test_rules()
26+
{
27+
$rules = [
28+
'total_products' => 'required|array',
29+
'total_products.*' => 'nullable|integer|min:0',
30+
];
31+
32+
$this->assertEquals($rules, $this->checkoutRequest->rules());
33+
}
34+
}

0 commit comments

Comments
 (0)