Skip to content

Commit 271825f

Browse files
committed
[U3][PATTC] - Exercises2-3-8
1 parent 88402d0 commit 271825f

File tree

15 files changed

+856
-1
lines changed

15 files changed

+856
-1
lines changed
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 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 $atmService;
16+
protected $exercise02Controller;
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+
$atmRequest = m::mock(ATMRequest::class);
35+
36+
$atmRequest->shouldReceive('validated')->andreturn([
37+
'card_id' => 10,
38+
]);
39+
$this->atmService->shouldReceive('calculate')->andreturn(0);
40+
41+
$result = $this->exercise02Controller->takeATMFee($atmRequest);
42+
$this->assertInstanceOf(RedirectResponse::class, $result);
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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_rules()
20+
{
21+
$rules = [
22+
'card_id' => 'required|exists:atms,card_id',
23+
];
24+
25+
$this->assertEquals($rules, $this->atmRequest->rules());
26+
}
27+
28+
public function test_validate_fails()
29+
{
30+
$validator = Validator::make([], $this->atmRequest->rules());
31+
32+
$this->assertTrue($validator->fails());
33+
}
34+
35+
}
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 $atmmodel;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->atmmodel = m::mock(ATM::class)->makePartial();
20+
$this->atmRepository = new ATMRepository($this->atmmodel);
21+
}
22+
23+
public function test_find()
24+
{
25+
$this->atmmodel->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-17'));
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-18'));
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-04-30'));
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-05-1'));
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-03-25 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-03-25 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-03-25 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-03-25 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-03-25 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-03-25 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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Http\JsonResponse;
11+
use Illuminate\View\View;
12+
13+
class ProductControllerTest extends TestCase
14+
{
15+
protected $productService;
16+
protected $productController;
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 = new CheckoutRequest();
38+
$request->total_products = 10;
39+
40+
$this->productService->shouldReceive('calculateDiscount')->andreturn([]);
41+
42+
$result = $this->productController->checkout($request);
43+
$this->assertInstanceOf(JsonResponse::class, $result);
44+
}
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_rules()
20+
{
21+
$rules = [
22+
'total_products' => 'required|array',
23+
'total_products.*' => 'nullable|integer|min:0',
24+
];
25+
26+
$this->assertEquals($rules, $this->checkoutRequest->rules());
27+
}
28+
}

0 commit comments

Comments
 (0)