Skip to content

Commit 12fe4e7

Browse files
committed
HangCTT-exercise-unitTest
1 parent 88402d0 commit 12fe4e7

File tree

16 files changed

+848
-0
lines changed

16 files changed

+848
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
6+
7+
abstract class TestCase extends BaseTestCase
8+
{
9+
use CreatesApplication;
10+
}
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 Modules\Exercise02\Http\Controllers\Exercise02Controller;
6+
use Modules\Exercise02\Http\Requests\ATMRequest;
7+
use Modules\Exercise02\Services\ATMService;
8+
use Illuminate\Http\RedirectResponse;
9+
use Illuminate\View\View;
10+
use Mockery as m;
11+
use Tests\TestCase;
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+
return $this->assertInstanceOf(RedirectResponse::class, $result);
44+
}
45+
}
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 Modules\Exercise02\Database\Factories\ATMFactory;
6+
use Modules\Exercise02\Models\ATM;
7+
use Tests\TestCase;
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+
return $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\Exercise03\Tests\Unit\Http\Requests;
4+
5+
use Modules\Exercise02\Repositories\ATMRepository;
6+
use Modules\Exercise02\Models\ATM;
7+
use Tests\TestCase;
8+
use Mockery as m;
9+
10+
class ATMRepositoryTest extends TestCase
11+
{
12+
protected $atmRepo;
13+
protected $model;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->model = m::mock(ATM::class)->makePartial();
20+
$this->atmRepo = new ATMRepository($this->model);
21+
}
22+
23+
public function test_find()
24+
{
25+
$this->model->shouldReceive('where->first')->andReturn([]);
26+
27+
$result = $this->atmRepo->find(1);
28+
return $this->assertEquals([], $result);
29+
}
30+
}
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_pass()
26+
{
27+
$rules = [
28+
'card_id' => 'required|exists:atms,card_id',
29+
];
30+
31+
$this->assertEquals($rules, $this->aTMRequest->rules());
32+
}
33+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Modules\Exercise02\Tests\Unit\Services;
4+
5+
use Modules\Exercise02\Repositories\ATMRepository;
6+
use Modules\Exercise02\Services\ATMService;
7+
use Modules\Exercise02\Models\ATM;
8+
use InvalidArgumentException;
9+
use Carbon\Carbon;
10+
use Mockery as m;
11+
use Tests\TestCase;
12+
13+
class ATMServiceTest extends TestCase
14+
{
15+
protected $atmService;
16+
protected $atmRepo;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->atmRepo = m::mock(ATMRepository::class);
23+
$this->atmService = new ATMService($this->atmRepo);
24+
}
25+
26+
public function test_has_not_card()
27+
{
28+
$this->atmRepo->shouldReceive('find')->andReturn([]);
29+
30+
$result = $this->atmService->calculate(1);
31+
32+
$this->expectExceptionMessage('Card ID is invalid!');
33+
return $this->assertInstanceOf(InvalidArgumentException::class, $result);
34+
}
35+
36+
public function test_card_is_vip()
37+
{
38+
$atm = m::mock(ATM::class)->makePartial();
39+
$atm->is_vip = true;
40+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
41+
42+
$result = $this->atmService->calculate(1);
43+
return $this->assertEquals(0, $result);
44+
}
45+
46+
public function test_day_is_holiday()
47+
{
48+
$atm = m::mock(ATM::class)->makePartial();
49+
Carbon::setTestNow(Carbon::parse('2021-04-29'));
50+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
51+
52+
$result = $this->atmService->calculate(1);
53+
return $this->assertEquals(110, $result);
54+
}
55+
56+
public function test_day_is_saturday()
57+
{
58+
$atm = m::mock(ATM::class)->makePartial();
59+
Carbon::setTestNow(Carbon::parse('2021-04-2'));
60+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
61+
62+
$result = $this->atmService->calculate(1);
63+
return $this->assertEquals(110, $result);
64+
}
65+
66+
public function test_day_is_sunday()
67+
{
68+
$atm = m::mock(ATM::class)->makePartial();
69+
Carbon::setTestNow(Carbon::parse('2021-04-25'));
70+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
71+
72+
$result = $this->atmService->calculate(1);
73+
return $this->assertEquals(110, $result);
74+
}
75+
76+
public function test_day_is_holiday_and_is_saturday()
77+
{
78+
$atm = m::mock(ATM::class)->makePartial();
79+
Carbon::setTestNow(Carbon::parse('2021-05-01'));
80+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
81+
82+
$result = $this->atmService->calculate(1);
83+
$this->assertEquals(110, $result);
84+
}
85+
86+
public function test_is_holiday_and_is_sunday()
87+
{
88+
$atm = m::mock(ATM::class)->makePartial();
89+
Carbon::setTestNow(Carbon::parse('2021-02-14'));
90+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
91+
92+
$result = $this->atmService->calculate(1);
93+
return $this->assertEquals(110, $result);
94+
}
95+
96+
public function test_is_normal_day_at_8h44 ()
97+
{
98+
$atm = m::mock(ATM::class)->makePartial();
99+
Carbon::setTestNow(Carbon::parse('2021-04-27 08:44:00'));
100+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
101+
102+
$result = $this->atmService->calculate(1);
103+
return $this->assertEquals(110, $result);
104+
}
105+
106+
public function test_is_normal_day_at_8h45 ()
107+
{
108+
$atm = m::mock(ATM::class)->makePartial();
109+
Carbon::setTestNow(Carbon::parse('2021-04-27 08:45:00'));
110+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
111+
112+
$result = $this->atmService->calculate(1);
113+
return $this->assertEquals(0, $result);
114+
}
115+
116+
public function test_is_normal_day_at_8h46 ()
117+
{
118+
$atm = m::mock(ATM::class)->makePartial();
119+
Carbon::setTestNow(Carbon::parse('2021-04-15 08:46:00'));
120+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
121+
122+
$result = $this->atmService->calculate(1);
123+
return $this->assertEquals(0, $result);
124+
}
125+
126+
public function test_is_normal_day_at_17h58 ()
127+
{
128+
$atm = m::mock(ATM::class)->makePartial();
129+
Carbon::setTestNow(Carbon::parse('2021-04-15 17:58:00'));
130+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
131+
132+
$result = $this->atmService->calculate(1);
133+
return $this->assertEquals(0, $result);
134+
}
135+
136+
public function test_is_normal_day_at_17h59 ()
137+
{
138+
$atm = m::mock(ATM::class)->makePartial();
139+
Carbon::setTestNow(Carbon::parse('2021-04-15 17:59:00'));
140+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
141+
142+
$result = $this->atmService->calculate(1);
143+
return $this->assertEquals(0, $result);
144+
}
145+
146+
public function test_is_normal_day_at_18h00 ()
147+
{
148+
$atm = m::mock(ATM::class)->makePartial();
149+
Carbon::setTestNow(Carbon::parse('2021-04-15 18:00:00'));
150+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
151+
152+
$result = $this->atmService->calculate(1);
153+
return $this->assertEquals(110, $result);
154+
}
155+
}
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 Modules\Exercise03\Http\Controllers\ProductController;
6+
use Modules\Exercise03\Http\Requests\CheckoutRequest;
7+
use Modules\Exercise03\Services\ProductService;
8+
use Illuminate\Http\JsonResponse;
9+
use Illuminate\View\View;
10+
use Mockery as m;
11+
use Tests\TestCase;
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+
return $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 Modules\Exercise03\Http\Requests\CheckoutRequest;
6+
use Illuminate\Support\Facades\Validator;
7+
use Tests\TestCase;
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+
return $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+
return $this->assertEquals($rules, $this->checkoutRequest->rules());
33+
}
34+
}

0 commit comments

Comments
 (0)