Skip to content

Commit f6105d7

Browse files
committed
Tran Thanh Son - Exercise 03
1 parent 88402d0 commit f6105d7

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

Modules/Exercise03/Database/Factories/ProductFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function definition()
2424
return [
2525
'name' => $this->faker->name,
2626
'thumbnail' => $this->faker->image(),
27+
'type' => $this->cravat(),
2728
];
2829
}
2930

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace Modules\Exercise03\Tests\Http\Requests;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise03\Services\ProductService;
7+
use Modules\Exercise03\Http\Controllers\ProductController;
8+
use Illuminate\Contracts\Support\Renderable;
9+
use Illuminate\View\View;
10+
use Modules\Exercise03\Http\Requests\CheckoutRequest;
11+
use Illuminate\Http\JsonResponse;
12+
use Modules\Exercise03\Models\Product;
13+
use Modules\Exercise03\Repositories\ProductRepository;
14+
15+
class ProductControllerTest extends TestCase
16+
{
17+
public function testConstruct()
18+
{
19+
$product = new Product;
20+
$repository = new ProductRepository($product);
21+
$service = new ProductService($repository);
22+
$controller = new ProductController($service);
23+
24+
$this->assertInstanceOf(ProductController::class, $controller);
25+
}
26+
27+
public function testIndex()
28+
{
29+
$collection = Mockery::mock(Collection::class);
30+
31+
$service = Mockery::mock(ProductService::class);
32+
$service->shouldReceive('getAllProducts')->once()->andReturn($collection);
33+
34+
$controller = new ProductController($service);
35+
36+
$this->assertInstanceOf(View::class, $controller->index());
37+
}
38+
39+
public function testCheckout()
40+
{
41+
$input = [
42+
1 => 1,
43+
2 => 2,
44+
3 => 3,
45+
];
46+
$response = 5;
47+
48+
$request = Mockery::mock(CheckoutRequest::class);
49+
$request->shouldReceive('input')->once()->with('total_products')->andReturn($input);
50+
51+
$service = Mockery::mock(ProductService::class);
52+
$service->shouldReceive('calculateDiscount')->once()->with($input)->andReturn($response);
53+
54+
$controller = new ProductController($service);
55+
56+
$this->assertInstanceOf(JsonResponse::class, $controller->checkout($request));
57+
}
58+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Modules\Exercise03\Tests\Http\Requests;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Tests\TestValidation;
7+
use Modules\Exercise03\Http\Requests\CheckoutRequest;
8+
9+
class CheckoutRequestTest extends TestCase
10+
{
11+
use TestValidation;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->rules = (new CheckoutRequest())->rules();
17+
$this->validator = $this->app['validator'];
18+
}
19+
20+
public function testTotalProducts()
21+
{
22+
$this->assertTrue($this->validateField('total_products.*', [
23+
1,
24+
2,
25+
3,
26+
])
27+
);
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Modules\Exercise03\Tests\Models;
3+
4+
use Tests\TestCase;
5+
use Modules\Exercise03\Models\Product;
6+
use Modules\Exercise03\Database\Factories\ProductFactory;
7+
8+
class ProductTest extends TestCase
9+
{
10+
public function testNewFactory()
11+
{
12+
$product = Product::newFactory();
13+
14+
$this->assertInstanceOf(ProductFactory::class, $product);
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace Modules\Exercise03\Tests\Repositories;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise03\Models\Product;
7+
use Modules\Exercise03\Repositories\ProductRepository;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Support\Collection;
10+
11+
class PriceRepositoryTest extends TestCase
12+
{
13+
public function testConstruct()
14+
{
15+
$product = new Product;
16+
$repository = new ProductRepository($product);
17+
18+
$this->assertInstanceOf(ProductRepository::class, $repository);
19+
}
20+
21+
public function testAll()
22+
{
23+
$product = new Product;
24+
$repository = new ProductRepository($product);
25+
$this->assertInstanceOf(Collection::class, $repository->all());
26+
}
27+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace App\Modules\Exercise03\Tests\Services;
3+
4+
use Mockery;
5+
use Tests\TestCase;
6+
use Modules\Exercise03\Models\Product;
7+
use Modules\Exercise03\Repositories\ProductRepository;
8+
use Modules\Exercise03\Services\ProductService;
9+
use Illuminate\Support\Collection;
10+
use InvalidArgumentException;
11+
12+
class PriceServiceTest extends TestCase
13+
{
14+
public function testConstruct()
15+
{
16+
$product = new Product;
17+
$productRepository = new ProductRepository($product);
18+
$service = new ProductService($productRepository);
19+
20+
$this->assertInstanceOf(ProductService::class, $service);
21+
}
22+
23+
/**
24+
* @dataProvider providerTestCalculateDiscount
25+
*/
26+
public function testCalculateDiscount($input, $expect)
27+
{
28+
$product = new Product;
29+
$repository = new ProductRepository($product);
30+
$service = new ProductService($repository);
31+
$response = $service->calculateDiscount($input);
32+
33+
$this->assertEquals($expect, $response);
34+
}
35+
36+
public function testCalculateDiscountResponseIsException()
37+
{
38+
$this->expectException(InvalidArgumentException::class);
39+
$input = [
40+
1 => -1,
41+
2 => 1,
42+
3 => 2,
43+
];
44+
$product = new Product;
45+
$repository = new ProductRepository($product);
46+
$service = new ProductService($repository);
47+
$service->calculateDiscount($input);
48+
}
49+
50+
public function testGetAllProducts()
51+
{
52+
$product = new Product;
53+
$repository = new ProductRepository($product);
54+
$service = new ProductService($repository);
55+
56+
$this->assertInstanceOf(Collection::class, $service->getAllProducts());
57+
}
58+
59+
public function providerTestCalculateDiscount()
60+
{
61+
return [
62+
$this->exampleData(1, 3, 2, ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT),
63+
$this->exampleData(5, 5, 5, ProductService::QUANTITY_DISCOUNT + ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT),
64+
];
65+
}
66+
67+
public function exampleData($cravat, $whiteShirt, $others, $expert)
68+
{
69+
return [
70+
[
71+
1 => $cravat,
72+
2 => $whiteShirt,
73+
3 => $others,
74+
],
75+
$expert,
76+
];
77+
}
78+
}

tests/TestValidation.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
trait TestValidation
6+
{
7+
protected function getFieldValidator($field, $value)
8+
{
9+
return $this->validator->make(
10+
[$field => $value],
11+
[$field => $this->rules[$field]]
12+
);
13+
}
14+
15+
protected function validateField($field, $value)
16+
{
17+
return $this->getFieldValidator($field, $value)->passes();
18+
}
19+
}

0 commit comments

Comments
 (0)