Skip to content
Open
Show file tree
Hide file tree
Changes from all 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,65 @@
<?php

namespace Tests\Feature\Http\Controllers;

use Modules\Exercise03\Http\Controllers\ProductController;
use Tests\TestCase;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Services\ProductService;
use Tests\SetupDatabaseTrait;

class ProductControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $productServiceMock;

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

// Laravel helper: mock and bind to service container
$this->productServiceMock = $this->mock(ProductService::class);
}

function test_it_show_index()
{
$url = action([ProductController::class, 'index']);

$product = new Product(['name' => 'producttest', 'type' => 1]);

$this->productServiceMock->shouldReceive('getAllProducts')->andReturn([$product]);

$response = $this->get($url);

$response->assertViewIs('exercise03::index');
$response->assertViewHas('products');
}

public function test_checkout_success()
{
$url = action([ProductController::class, 'checkout']);
$totalProduct = [
'total_products' => [
1 => 5,
2 => 5,
3 => 5,
],
];
$this->productServiceMock->shouldReceive('calculateDiscount')->with([
1 => 5,
2 => 5,
3 => 5,
])
->andReturn(12);

$response = $this->post($url, $totalProduct);


$response->assertStatus(200)
->assertJsonPath(
'discount',
12,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

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

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

class CheckoutRequestTest extends TestCase
{
use SetupDatabaseTrait;

public function test_it_contain_default_rules()
{
$request = new CheckoutRequest();

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

/**
* @dataProvider provider_test_validation_wrong
*/
public function test_validation_fails_when_data_wrong($input)
{
$request = new CheckoutRequest();

$validator = Validator::make([
'total_products' => $input['total_products'],
], $request->rules());

$this->assertTrue($validator->fails());
}

function provider_test_validation_wrong()
{
return [
[
[
'total_products' => null,
]
],
[
[
'total_products' => [],
]
],
[
[
'total_products' => [1 => -1],
]
],
[
[
'total_products' => [1 => 5.5],
]
],
[
[
'total_products' => [1 => 'aespa'],
]
],
];
}

public function test_validation_success()
{
$request = new CheckoutRequest();
$validator = Validator::make([
'total_products' => [1 => 10],
], $request->rules());

$this->assertTrue($validator->passes());
}
}
17 changes: 17 additions & 0 deletions Modules/Exercise03/Tests/Unit/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Unit\Models;

use Modules\Exercise03\Models\Product;
use Tests\TestCase;
use Modules\Exercise03\Database\Factories\ProductFactory;

class ProductTest extends TestCase
{
function test_create_new_factory()
{
$product = Product::newFactory();

$this->assertInstanceOf(ProductFactory::class, $product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Modules\Exercise03\Tests\Unit\Repositories;

use Tests\TestCase;
use Modules\Exercise03\Models\Product;
use Modules\Exercise03\Repositories\ProductRepository;
use Tests\SetupDatabaseTrait;

class ProductRepositoryTest extends TestCase
{
use SetupDatabaseTrait;

/**
* @var ProductRepository
*/
protected $repository;

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

$this->repository = new ProductRepository(new Product());
}

public function test_all()
{
Product::factory()->cravat()->create([
'name' => 'Cà vạt',
'thumbnail' => 'images/exercise03/cravat.jpg',
]);

$product = new Product();
$productRepository = new ProductRepository($product);

$products = $productRepository->all();

$this->assertEquals($products->count(), 1);
}
}
154 changes: 154 additions & 0 deletions Modules/Exercise03/Tests/Unit/Services/ProductServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Tests\Unit\Services;

use Modules\Exercise03\Services\ProductService;
use Modules\Exercise03\Repositories\ProductRepository;
use Modules\Exercise03\Models\Product;
use InvalidArgumentException;
use Tests\SetupDatabaseTrait;

use Tests\TestCase;

class ProductServiceTest extends TestCase
{
use SetupDatabaseTrait;

protected $calendarServiceMock;

public function setUp(): void
{
parent::setUp();
$this->product = new Product();
$repositoy = new ProductRepository($this->product);
$this->service = new ProductService($repositoy);
}

/**
* @dataProvider provider_test_alculate_discount
*/
public function test_calculate_discount($totalProducts, $discount)
{
$result = $this->service->calculateDiscount($totalProducts);

$this->assertEquals($result, $discount);
}

public function provider_test_alculate_discount()
Copy link

Choose a reason for hiding this comment

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

Ở đây chị thiếu miss mất 1 case.
Giả sử các trường hợp dưới đây không tồn tại thì ra sao?

$totalProducts[Product::CRAVAT_TYPE] 
$totalProducts[Product::OTHER_TYPE]
$totalProducts[Product::WHITE_SHIRT_TYPE]

{
return [
[
[
1 => 2,
2 => 1,
3 => 1,
], 5
],
[
[
1 => 2,
2 => 2,
3 => 3,
], 12
],
[
[
1 => 2,
2 => 5,
3 => 5,
], 12
],
[
[
1 => 0,
2 => 1,
3 => 1,
], 0
],
[
[
1 => 0,
2 => 5,
3 => 5,
], 7
],
[
[
1 => 0,
2 => 0,
3 => 5,
], 0
],
[
[
1 => 0,
2 => 0,
3 => 8,
], 7
],
[
[
1 => 0,
2 => 0,
3 => 0,
], 0
],
];
}

/**
* @dataProvider provider_test_function_throw_exception
*/

public function test_function_throw_exception($totalProducts)
{
$this->expectException(InvalidArgumentException::class);
$this->service->calculateDiscount($totalProducts);
}

public function provider_test_function_throw_exception()
{
return [
[
[
1 => -5,
2 => 5,
3 => 5,
]
],
[
[
1 => 5,
2 => -5,
3 => 5,
]

],
[
[
1 => 5,
2 => 5,
3 => -5,
]
],
[
[
1 => -5,
2 => -5,
3 => -5,
]
],
];
}

public function test_get_all_products()
{
$this->product->create(['name' => 'product1', 'type' => 1]);
$this->product->create(['name' => 'product2', 'type' => 2]);
$this->product->create(['name' => 'product3', 'type' => 3]);

$products = $this->service->getAllProducts();

$this->assertEquals($products->count(), 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\Feature\Http\Controllers;

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

class CalendarControllerTest extends TestCase
{
use SetupDatabaseTrait;

protected $calendarServiceMock;

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

// Laravel helper: mock and bind to service container
$this->calendarServiceMock = $this->mock(CalendarService::class);
}

function test_it_index()
{
$url = action([CalendarController::class, 'index']);

$this->calendarServiceMock->shouldReceive('getDateClass')->andReturn('text-dark');
Copy link

Choose a reason for hiding this comment

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

Đoạn mock này mình không nên chỉ shouldReceiveandReturn luôn.
Mình nên làm để biết thêm là cái method getDateClass gọi bao nhiêu lần và những tham số truyền vào là gì


$response = $this->get($url);

$response->assertViewIs('exercise04::calendar');
$response->assertViewHas('calendars');
}
}
Loading