Skip to content

Commit 7129299

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

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
13+
class ProductControllerTest extends TestCase
14+
{
15+
public function testConstruct()
16+
{
17+
$service = $this->app->make(ProductService::class);
18+
$controller = new ProductController($service);
19+
20+
$this->assertInstanceOf(ProductController::class, $controller);
21+
}
22+
23+
public function testIndex()
24+
{
25+
$collection = Mockery::mock(Collection::class);
26+
27+
$service = Mockery::mock(ProductService::class);
28+
$service->shouldReceive('getAllProducts')->once()->andReturn($collection);
29+
30+
$controller = new ProductController($service);
31+
32+
$this->assertInstanceOf(View::class, $controller->index());
33+
}
34+
35+
public function testCheckout()
36+
{
37+
$input = [
38+
1 => 1,
39+
2 => 2,
40+
3 => 3,
41+
];
42+
$response = 5;
43+
44+
$request = Mockery::mock(CheckoutRequest::class);
45+
$request->shouldReceive('input')->once()->with('total_products')->andReturn($input);
46+
47+
$service = Mockery::mock(ProductService::class);
48+
$service->shouldReceive('calculateDiscount')->once()->with($input)->andReturn($response);
49+
50+
$controller = new ProductController($service);
51+
52+
$this->assertInstanceOf(JsonResponse::class, $controller->checkout($request));
53+
}
54+
}
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
$repository = $this->app->make(ProductRepository::class);
24+
$this->assertInstanceOf(Collection::class, $repository->all());
25+
}
26+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
namespace App\Modules\Exercise01\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+
$service = $this->app->make(ProductService::class);
29+
$response = $service->calculateDiscount($input);
30+
31+
$this->assertEquals($expect, $response);
32+
}
33+
34+
public function testCalculateDiscountResponseIsException()
35+
{
36+
37+
$this->expectException(InvalidArgumentException::class);
38+
$input = [
39+
1 => -1,
40+
2 => 1,
41+
3 => 2,
42+
];
43+
$service = $this->app->make(ProductService::class);
44+
$service->calculateDiscount($input);
45+
}
46+
47+
public function testGetAllProducts()
48+
{
49+
$service = $this->app->make(ProductService::class);
50+
$this->assertInstanceOf(Collection::class, $service->getAllProducts());
51+
}
52+
53+
public function providerTestCalculateDiscount()
54+
{
55+
return [
56+
$this->exampleData(1, 3, 2, ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT),
57+
$this->exampleData(5, 5, 5, ProductService::QUANTITY_DISCOUNT + ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT),
58+
];
59+
}
60+
61+
public function exampleData($cravat, $whiteShirt, $others, $expert)
62+
{
63+
return [
64+
[
65+
1 => $cravat,
66+
2 => $whiteShirt,
67+
3 => $others,
68+
],
69+
$expert,
70+
];
71+
}
72+
}

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)