Skip to content

Commit a203689

Browse files
committed
Exercise 05
1 parent fdc2b71 commit a203689

File tree

6 files changed

+280
-0
lines changed

6 files changed

+280
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
namespace Modules\Exercise05\Tests\Http\Controllers;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise05\Services\OrderService;
7+
use Modules\Exercise05\Http\Controllers\Exercise05Controller;
8+
use Illuminate\Contracts\Support\Renderable;
9+
use Modules\Exercise05\Http\Requests\OrderRequest;
10+
use Illuminate\View\View;
11+
use Illuminate\Http\JsonResponse;
12+
13+
class Exercise05ControllerTest extends TestCase
14+
{
15+
public function testConstruct()
16+
{
17+
$service = new OrderService;
18+
$controller = new Exercise05Controller($service);
19+
20+
$this->assertInstanceOf(Exercise05Controller::class, $controller);
21+
}
22+
23+
public function testIndex()
24+
{
25+
$service = new OrderService;
26+
$controller = new Exercise05Controller($service);
27+
28+
$this->assertInstanceOf(View::class, $controller->index());
29+
}
30+
31+
public function testStore()
32+
{
33+
$input = [
34+
'price' => 1500,
35+
'option_receive' => '2',
36+
'option_coupon' => '1',
37+
];
38+
$response = [
39+
'price' => 1200,
40+
'discount_pizza' => null,
41+
'discount_potato' => null,
42+
];
43+
44+
$request = Mockery::mock(OrderRequest::class);
45+
$request->shouldReceive('only')->once()->andReturn($input);
46+
47+
$service = Mockery::mock(OrderService::class);
48+
$service->shouldReceive('handleDiscount')->once()->with($input)->andReturn($response);
49+
50+
$controller = new Exercise05Controller($service);
51+
52+
$this->assertInstanceOf(View::class, $controller->store($request));
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Modules\Exercise05\Tests\Http\Requests;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Tests\TestValidation;
7+
use Modules\Exercise05\Http\Requests\OrderRequest;
8+
9+
class OrderRequestTest extends TestCase
10+
{
11+
use TestValidation;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->rules = (new OrderRequest())->rules();
17+
$this->validator = $this->app['validator'];
18+
}
19+
20+
public function testPrice()
21+
{
22+
$this->assertTrue($this->validateField('price', 1200));
23+
}
24+
25+
public function testOptionReceive()
26+
{
27+
$this->assertTrue($this->validateField('option_receive', '1'));
28+
}
29+
30+
public function testOptionCoupoin()
31+
{
32+
$this->assertTrue($this->validateField('option_coupon', '1'));
33+
}
34+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace App\Modules\Exercise05\Tests\Services;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise05\Services\OrderService;
7+
use Illuminate\Support\Carbon;
8+
9+
class OrderServiceTest extends TestCase
10+
{
11+
12+
/**
13+
* @dataProvider providerTestHandleDiscount
14+
*/
15+
public function testHandleDiscount($input, $expect)
16+
{
17+
$service = new OrderService;
18+
$response = $service->handleDiscount($input);
19+
20+
$this->assertEquals($expect['price'], $response['price']);
21+
$this->assertEquals($expect['discount_pizza'], $response['discount_pizza']);
22+
$this->assertEquals($expect['discount_potato'], $response['discount_potato']);
23+
}
24+
25+
public function providerTestHandleDiscount()
26+
{
27+
return [
28+
[
29+
[
30+
'price' => 1500,
31+
'option_receive' => '2',
32+
'option_coupon' => '1',
33+
],
34+
[
35+
'price' => 1200,
36+
'discount_pizza' => null,
37+
'discount_potato' => null,
38+
],
39+
],
40+
[
41+
[
42+
'price' => 1600,
43+
'option_receive' => '2',
44+
'option_coupon' => '1',
45+
],
46+
[
47+
'price' => 1280,
48+
'discount_pizza' => null,
49+
'discount_potato' => 'Miễn phí khoai tây',
50+
],
51+
],
52+
[
53+
[
54+
'price' => 1600,
55+
'option_receive' => '1',
56+
'option_coupon' => '1',
57+
],
58+
[
59+
'price' => 1600,
60+
'discount_pizza' => 'Khuyến mại pizza thứ 2',
61+
'discount_potato' => 'Miễn phí khoai tây',
62+
],
63+
],
64+
];
65+
}
66+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace Modules\Exercise06\Tests\Http\Controllers;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise06\Services\CalculateService;
7+
use Modules\Exercise06\Http\Controllers\Exercise06Controller;
8+
use Illuminate\Contracts\Support\Renderable;
9+
use Modules\Exercise06\Http\Requests\Exercise06Request;
10+
use Illuminate\View\View;
11+
use Illuminate\Http\JsonResponse;
12+
use Illuminate\Http\RedirectResponse;
13+
14+
class Exercise06ControllerTest extends TestCase
15+
{
16+
public function testConstruct()
17+
{
18+
$service = new CalculateService;
19+
$controller = new Exercise06Controller($service);
20+
21+
$this->assertInstanceOf(Exercise06Controller::class, $controller);
22+
}
23+
24+
public function testIndex()
25+
{
26+
$service = new CalculateService;
27+
$controller = new Exercise06Controller($service);
28+
29+
$this->assertInstanceOf(View::class, $controller->index());
30+
}
31+
32+
public function testStore()
33+
{
34+
$input = [
35+
'bill' => 1500,
36+
'has_watch' => true,
37+
];
38+
$response = 60;
39+
40+
$request = Mockery::mock(Exercise06Request::class);
41+
$request->shouldReceive('validated')->once()->andReturn($input);
42+
43+
$service = Mockery::mock(CalculateService::class);
44+
$service->shouldReceive('calculate')->once()->with(1500, true)->andReturn($response);
45+
46+
$controller = new Exercise06Controller($service);
47+
48+
$this->assertInstanceOf(RedirectResponse::class, $controller->calculate($request));
49+
}
50+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Modules\Exercise06\Tests\Http\Requests;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Tests\TestValidation;
7+
use Modules\Exercise06\Http\Requests\Exercise06Request;
8+
9+
class Exercise06RequestTest extends TestCase
10+
{
11+
use TestValidation;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->rules = (new Exercise06Request())->rules();
17+
$this->validator = $this->app['validator'];
18+
}
19+
20+
public function testBill()
21+
{
22+
$this->assertTrue($this->validateField('bill', 200));
23+
}
24+
25+
public function testHasWatch()
26+
{
27+
$this->assertTrue($this->validateField('has_watch', true));
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace App\Modules\Exercise06\Tests\Services;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise06\Services\CalculateService;
7+
use Illuminate\Support\Carbon;
8+
use InvalidArgumentException;
9+
10+
class CalculateServiceTest extends TestCase
11+
{
12+
public function testCalculateException()
13+
{
14+
$this->expectException(InvalidArgumentException::class);
15+
$service = new CalculateService;
16+
$service->calculate(0);
17+
}
18+
19+
/**
20+
* @dataProvider providerTestCalculatePass
21+
*/
22+
public function testCalculatePass($input, $expect)
23+
{
24+
$service = new CalculateService;
25+
$response = $service->calculate($input, true);
26+
27+
$this->assertEquals($response, $expect);
28+
}
29+
30+
public function providerTestCalculatePass()
31+
{
32+
return [
33+
[
34+
120,
35+
180,
36+
],
37+
[
38+
2200,
39+
240,
40+
],
41+
[
42+
5100,
43+
300,
44+
],
45+
];
46+
}
47+
}

0 commit comments

Comments
 (0)