Skip to content

Commit 0465782

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

File tree

17 files changed

+844
-1
lines changed

17 files changed

+844
-1
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}
31+
32+
public function test_take_atm_fee()
33+
{
34+
$request = m::mock(ATMRequest::class);
35+
36+
$request->shouldReceive('validated')->andreturn([
37+
'card_id' => 2,
38+
]);
39+
$this->atmService->shouldReceive('calculate')->andReturn(0);
40+
41+
$result = $this->exercise02Controller->takeATMFee($request);
42+
return $this->assertInstanceOf(RedirectResponse::class, $result);
43+
}
44+
}
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: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
$this->expectException(InvalidArgumentException::class);
34+
35+
return $this->assertInstanceOf(InvalidArgumentException::class, $result);
36+
}
37+
38+
public function test_card_is_vip()
39+
{
40+
$atm = m::mock(ATM::class)->makePartial();
41+
$atm->is_vip = true;
42+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
43+
44+
$result = $this->atmService->calculate(1);
45+
return $this->assertEquals(0, $result);
46+
}
47+
48+
public function test_day_is_holiday()
49+
{
50+
$atm = m::mock(ATM::class)->makePartial();
51+
Carbon::setTestNow(Carbon::parse('2021-04-29'));
52+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
53+
54+
$result = $this->atmService->calculate(1);
55+
return $this->assertEquals(110, $result);
56+
}
57+
58+
public function test_day_is_holiday_and_is_saturday()
59+
{
60+
$atm = m::mock(ATM::class)->makePartial();
61+
Carbon::setTestNow(Carbon::parse('2021-05-01'));
62+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
63+
64+
$result = $this->atmService->calculate(1);
65+
$this->assertEquals(110, $result);
66+
}
67+
68+
public function test_day_is_holiday_and_is_sunday()
69+
{
70+
$atm = m::mock(ATM::class)->makePartial();
71+
Carbon::setTestNow(Carbon::parse('2021-02-14'));
72+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
73+
74+
$result = $this->atmService->calculate(1);
75+
return $this->assertEquals(110, $result);
76+
}
77+
78+
public function test_day_is_saturday()
79+
{
80+
$atm = m::mock(ATM::class)->makePartial();
81+
Carbon::setTestNow(Carbon::parse('2021-04-2'));
82+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
83+
84+
$result = $this->atmService->calculate(1);
85+
return $this->assertEquals(110, $result);
86+
}
87+
88+
public function test_day_is_sunday()
89+
{
90+
$atm = m::mock(ATM::class)->makePartial();
91+
Carbon::setTestNow(Carbon::parse('2021-04-25'));
92+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
93+
94+
$result = $this->atmService->calculate(1);
95+
return $this->assertEquals(110, $result);
96+
}
97+
98+
public function test_normal_day_at_8h44 ()
99+
{
100+
$atm = m::mock(ATM::class)->makePartial();
101+
Carbon::setTestNow(Carbon::parse('2021-04-27 08:44:00'));
102+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
103+
104+
$result = $this->atmService->calculate(1);
105+
return $this->assertEquals(110, $result);
106+
}
107+
108+
public function test_normal_day_at_8h45 ()
109+
{
110+
$atm = m::mock(ATM::class)->makePartial();
111+
Carbon::setTestNow(Carbon::parse('2021-04-27 08:45:00'));
112+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
113+
114+
$result = $this->atmService->calculate(1);
115+
return $this->assertEquals(0, $result);
116+
}
117+
118+
public function test_normal_day_at_8h46 ()
119+
{
120+
$atm = m::mock(ATM::class)->makePartial();
121+
Carbon::setTestNow(Carbon::parse('2021-04-15 08:46:00'));
122+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
123+
124+
$result = $this->atmService->calculate(1);
125+
return $this->assertEquals(0, $result);
126+
}
127+
128+
public function test_normal_day_at_17h58 ()
129+
{
130+
$atm = m::mock(ATM::class)->makePartial();
131+
Carbon::setTestNow(Carbon::parse('2021-04-15 17:58:00'));
132+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
133+
134+
$result = $this->atmService->calculate(1);
135+
return $this->assertEquals(0, $result);
136+
}
137+
138+
public function test_normal_day_at_17h59 ()
139+
{
140+
$atm = m::mock(ATM::class)->makePartial();
141+
Carbon::setTestNow(Carbon::parse('2021-04-15 17:59:00'));
142+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
143+
144+
$result = $this->atmService->calculate(1);
145+
return $this->assertEquals(0, $result);
146+
}
147+
148+
public function test_normal_day_at_18h00 ()
149+
{
150+
$atm = m::mock(ATM::class)->makePartial();
151+
Carbon::setTestNow(Carbon::parse('2021-04-15 18:00:00'));
152+
$this->atmRepo->shouldReceive('find')->andReturn($atm);
153+
154+
$result = $this->atmService->calculate(1);
155+
return $this->assertEquals(110, $result);
156+
}
157+
}

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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}
33+
34+
public function test_checkout()
35+
{
36+
$request = m::mock(CheckoutRequest::class);
37+
38+
$request->shouldReceive('input')->andreturn([
39+
'total_products' => 1,
40+
]);
41+
$this->productService->shouldReceive('calculateDiscount')->andreturn([]);
42+
43+
$result = $this->productController->checkout($request);
44+
return $this->assertInstanceOf(JsonResponse::class, $result);
45+
}
46+
}

0 commit comments

Comments
 (0)