Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Modules\Tests\Exercise03\Tests\Feature\Http\Controllers;

use Illuminate\View\View;
use Modules\Exercise03\Http\Controllers\ProductController;
use Modules\Exercise03\Http\Requests\CheckoutRequest;
use Modules\Exercise03\Services\ProductService;
use Tests\TestCase;

class ProductControllerTest extends TestCase
{
private $productService;
private $productController;

public function setUp(): void
{
parent::setUp();

$this->productService = \Mockery::mock(ProductService::class);
$this->productController = new ProductController(
$this->productService
);
}

/**
* A basic feature test example.
*
* @return void
*/
public function testIndex()
{
$products = [
'name' => 'name',
'status' => 1
];

$this->productService->shouldReceive('getAllProducts')
->andReturn($products);
$response = $this->productController->index();
$this->assertInstanceOf(View::class, $response);
$this->assertEquals('exercise03::index', $response->getName());
}

public function testCheckout() {
$input['total_products'] = [
1 => 1,
2 => 2,
3 => 3
];

$this->productService->shouldReceive('calculateDiscount')
->with($input)
->andReturn(5);
$mockRequest = \Mockery::mock(CheckoutRequest::class);
$mockRequest->shouldReceive('input')->andReturn($input);
$response = $this->productController->checkout($mockRequest);
$this->assertEquals(['discount' => 5], $response->getOriginalContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Modules\Tests\Exercise03\Tests\Feature\Http\Requests;

use Illuminate\Support\Facades\Validator;
use Modules\Exercise03\Http\Requests\CheckoutRequest;
use Tests\TestCase;

class CheckoutRequestTest extends TestCase
{
private $request;

public function setUp(): void
{
parent::setUp();

$this->request = new CheckoutRequest();
}

public function testRules()
{
$this->assertEquals([
'total_products' => 'required|array',
'total_products.*' => 'nullable|integer|min:0',
],
$this->request->rules()
);
}

public function testInvalidData()
{
$validator = Validator::make([], $this->request->rules());
$this->assertTrue($validator->fails());
}

public function testValidData()
{
$data = [
'total_products' => [0, 1, 2, 3],
];

$validator = Validator::make($data, $this->request->rules());
$this->assertTrue($validator->passes());
}
}
20 changes: 20 additions & 0 deletions Modules/Exercise03/Tests/Feature/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Modules\Tests\Exercise03\Tests\Feature\Model;

use Modules\Exercise03\Models\Product;
use Tests\TestCase;

class ProductTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

public function testNewFactory()
{
$product = Product::factory()->make();
$this->assertInstanceOf(Product::class, $product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Modules\Tests\Exercise03\Tests\Feature\Repositories;

use Illuminate\Support\Collection;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;
use Tests\TestCase;

class ProductRepositoryTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

public function testAll()
{
$repository = new ProductRepository(new Product());
$getAll = $repository->all();
$this->assertInstanceOf(Collection::class, $getAll);
}
}
104 changes: 104 additions & 0 deletions Modules/Exercise03/Tests/Feature/Services/ProductServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Modules\Tests\Exercise03\Tests\Feature\Services;

use InvalidArgumentException;
use Illuminate\Support\Collection;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;
use Modules\Exercise03\Services\ProductService;
use Tests\TestCase;

class ProductServiceTest extends TestCase
{
private $productService;
private $mockProductRepository;

public function setUp(): void
{
parent::setUp();

$this->mockProductRepository = $this->mock(ProductRepository::class);
$this->productService = new ProductService($this->mockProductRepository);
}

/**
* @dataProvider provideData
*/

public function testCalculateDiscount($totalProducts, $expectValue, $testCase = 'OK')
{
if ($testCase === 'NG') {
$this->expectException(InvalidArgumentException::class);
}

$response = $this->productService->calculateDiscount($totalProducts);
$this->assertEquals($response, $expectValue);
}

public function provideData()
{
return [
[
[
1 => 0,
2 => 0,
3 => 0
],
0
],
[
[
1 => 1,
2 => 2,
3 => 2
],
5
],
[
[
1 => 1,
2 => 0,
3 => 7
],
7
],
[
[
1 => 1,
2 => 2,
3 => 5
],
12
],
[
[
1 => -1,
2 => 2,
3 => 9
],
0,
'NG'
],
[
[
1 => 1,
2 => -2,
3 => 2
],
0,
'NG'
],
];
}

public function testGetAllProducts()
{
$this->mockProductRepository
->shouldReceive('all')
->andReturn([]);

$this->assertEquals($this->productService->getAllProducts(), []);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Modules\Tests\Exercise04\Tests\Feature\Http\Controllers;

use Illuminate\View\View;
use Modules\Exercise04\Http\Controllers\CalendarController;
use Modules\Exercise04\Services\CalendarService;
use Tests\TestCase;

class CalendarControllerTest extends TestCase
{
private $calendarController;
private $mockCalendarService;

public function setUp(): void
{
parent::setUp();
$this->mockCalendarService = $this->mock(CalendarService::class);
$this->calendarController = new CalendarController($this->mockCalendarService);
}

public function testIndex()
{
$this->mockCalendarService
->shouldReceive('getDateClass')
->andReturn(CalendarService::COLOR_BLUE);

$response = $this->calendarController->index();
$this->assertInstanceOf(View::class, $response);
$this->assertEquals('exercise04::calendar', $response->getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Modules\Tests\Exercise03\Tests\Feature\Http\Services;

use Carbon\Carbon;
use Modules\Exercise04\Services\CalendarService;
use Tests\TestCase;

class CalendarServiceTest extends TestCase
{
private $calendarService;

public function setUp(): void
{
parent::setUp();
$this->calendarService = new CalendarService();
}

/**
* @param $date
* @dataProvider provideData
*/
public function testGetDateClass($date, $expectValue)
{
$holiday = ['2021-05-30'];

$class = $this->calendarService->getDateClass($date, $holiday);
$this->assertEquals($class, $expectValue);
}

public function provideData()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anh ơi với bài này thì em nghĩ nên tách mỗi case ra một hàm để còn xem là với trường hợp ngày lễ có đúng màu đỏ, với ngày cn có đúng màu đỏ, .v.v. Ý là cho meaning hơn ấy ạ.
Với anh thiếu case ngày thường color là BLACK ạ

Copy link
Author

@tuanvh-0174 tuanvh-0174 Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • trong provideData có $expectValue rồi, a nghĩ cũng k cần thiết phải tách ra nhiều func đâu
  • case: color Black a update rồi nhé

{
return [
[
Carbon::createFromDate(2021, 5, 23),
CalendarService::COLOR_RED,
],
[
Carbon::createFromDate(2021, 5, 29),
CalendarService::COLOR_BLUE,
],
[
Carbon::createFromDate(2021, 5, 30),
CalendarService::COLOR_RED,
],

];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Modules\Tests\Exercise05\Tests\Feature\Http\Controllers;

use Illuminate\View\View;
use Modules\Exercise05\Http\Controllers\Exercise05Controller;
use Modules\Exercise05\Http\Requests\OrderRequest;
use Modules\Exercise05\Services\OrderService;
use Tests\TestCase;

class Exercise05ControllerTest extends TestCase
{
private $controller;
private $mockOrderService;

public function setUp(): void
{
parent::setUp();

$this->mockOrderService = $this->mock(OrderService::class);
$this->controller = new Exercise05Controller($this->mockOrderService);
}

public function testIndex()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://sun-unit-test-training.github.io/php-testing-guideline/06-checklist/#1-self-describing-test-method

Tên function nên thể hiện được a test case nào và expect của test case của test case a nhé

Copy link
Author

@tuanvh-0174 tuanvh-0174 Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a thấy k cần thiết sửa đâu, ở đây mình đang test theo file, theo method mà,
Sau muốn update file test, vào file test mình thấy rõ hơn đang đã viết test cho những method nào rồi

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Việc đặt tên nó khá quan trọng ạ . Đây là case đơn giản thì mình có thể viết ngẵn gọn được
Nhưng với nhiều case phức tạp thì cách đặt tên nó sẽ giúp bản thân mình đọc code dễ dàng hơn
trong case này e thấy tên ntn là hợp lý ạ

test_index_return_view_success

{
$response = $this->controller->index();
$this->assertInstanceOf(View::class, $response);
$this->assertEquals('exercise05::index', $response->getName());
}

public function testStore()
{
$detailOrder = [
'price' => 100,
'option_receive' => 1,
'option_coupon' => 1
];

$mockRequest = $this->mock(OrderRequest::class);
$mockRequest->shouldReceive('only')
->andReturn($detailOrder);

$this->mockOrderService
->shouldReceive('handleDiscount')
->andReturn(1);

$res = $this->controller->store($mockRequest);
$this->assertEquals('exercise05::detail', $res->getName());
}
}
Loading