Skip to content

[CuongDV] Create UT for Ex3 -> Ex7 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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,49 @@
<?php

namespace Tests\Feature;

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

class ProductControllerTest extends TestCase
{
protected $controller;
protected $productService;

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

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

public function test_index_return_view_success()
{
$products = ['name' => 'name'];
$this->productService->shouldReceive('getAllProducts')
->andReturn($products);
$response = $this->controller->index();

$this->assertEquals('exercise03::index', $response->getName());
$this->assertEquals(compact('products'), $response->getData());
}

public function test_checkout_return_view_success()
{
$inputs = [
'total_products' => 1,
];

$request = CheckoutRequest::create('', 'post', $inputs);
$this->productService->shouldReceive('calculateDiscount')
->with($inputs['total_products'])
->andReturn(1);
$response = $this->controller->checkout($request);

$this->assertEquals(['discount' => 1], $response->getOriginalContent());
$this->assertEquals(200, $response->status());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\Feature;

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

class CheckoutRequestTest extends TestCase
{
protected $checkoutRequest;

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

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

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

$validator = Validator::make($input, $this->checkoutRequest->rules());
$this->assertTrue($validator->passes());
}

public function test_validate_with_totals_product_error()
{
$input = ['total_product' => 'fail'];
$validator = Validator::make($input, $this->checkoutRequest->rules());

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

namespace Modules\Exercise03\Tests\Feature\Models;

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

class ProductTest extends TestCase
{
use SetupDatabaseTrait;

public function test_product_new_factory()
{
$product = Product::factory()->make();

$this->assertInstanceOf(Product::class, $product);
}
}
31 changes: 31 additions & 0 deletions Modules/Exercise03/Tests/Unit/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Modules\Exercise03\Tests\Feature\Models;

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

class ProductModelTest extends TestCase
{
use SetupDatabaseTrait;

/**
* This test does not count coverage for model Product,
* because we are test for class property `fillable`, not method
*
* But it is added to ensure we initialize property correctly
*/
public function test_fields_are_fillable()
{
$inputs = [
'name' => 'name',
'type' => Product::CRAVAT_TYPE,
];

$product = Product::create($inputs);

$this->assertEquals($inputs['name'], $product->name);
$this->assertEquals($inputs['type'], $product->type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Modules\Exercise03\Tests\Unit\Repositories;

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

class ProductRepositoryTest extends TestCase
{
protected $repository;
protected $productModel;

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

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

public function test_all()
{
$response = $this->repository->all();

$this->assertInstanceOf(Collection::class, $response);
}
}
74 changes: 74 additions & 0 deletions Modules/Exercise03/Tests/Unit/Services/ProductServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Tests\Unit\Services;

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

class ProductServiceTest extends TestCase
{
protected $productRepository;
protected $service;

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

$this->productRepository = $this->mock(ProductRepository::class);
$this->service = new ProductService($this->productRepository);

}

public function test_get_all_products()
{
$this->productRepository->shouldReceive('all')
->andReturn([]);
$products = $this->service->getAllProducts();

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

/**
* @param $totalProducts
* @param $expectedValue
* @dataProvider provide_total_products_data
* */
public function test_calculate_discount_with_valid_data($totalProducts, $expectedValue)
{
$discount = $this->service->calculateDiscount($totalProducts);
$this->assertEquals($expectedValue, $discount);
}

public function provide_total_products_data()
{
return [
[
[
Product::CRAVAT_TYPE => 1,
Product::WHITE_SHIRT_TYPE => 2,
Product::OTHER_TYPE => 0,
], ProductService::CRAVAT_WHITE_SHIRT_DISCOUNT
],
[
[
Product::CRAVAT_TYPE => 2,
Product::WHITE_SHIRT_TYPE => 3,
Product::OTHER_TYPE => 5,
], 12
],
];
}

public function test_calculate_discount_throw_exception()
{
$this->expectException(InvalidArgumentException::class);
$this->service->calculateDiscount([
Product::CRAVAT_TYPE => -1,
Product::WHITE_SHIRT_TYPE => -2,
Product::OTHER_TYPE => -3,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Feature\Http\Controllers;

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

class CalendarControllerTest extends TestCase
{
protected $controller;
protected $calendarService;

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

$this->calendarService = $this->mock(CalendarService::class);
$this->controller = new CalendarController($this->calendarService);
}

public function test_index()
{
$this->calendarService->shouldReceive('getDateClass')
->andReturn(CalendarService::COLOR_BLACK);
$response = $this->controller->index();

$this->assertEquals('exercise04::calendar', $response->getName());
$this->assertArrayHasKey('calendars', $response->getData());
}
}
51 changes: 51 additions & 0 deletions Modules/Exercise04/Tests/Unit/Services/CalendarServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Unit\Services;

use Carbon\Carbon;
use Modules\Exercise04\Services\CalendarService;
use PHPUnit\Framework\TestCase;

class CalendarServiceTest extends TestCase
{
protected $calendarService;

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

$this->calendarService = new CalendarService();
}

/**
* @dataProvider provider_date_input_data
*/
public function test_get_date_class($date, $expected)
{
$response = $this->calendarService->getDateClass($date, ['2021-04-30']);

$this->assertEquals($expected, $response);
}

public function provider_date_input_data()
{
return [
[
Carbon::createFromDate(2021, 5, 5),
CalendarService::COLOR_BLACK
],
[
Carbon::createFromDate(2021, 5, 23),
CalendarService::COLOR_RED
],
[
Carbon::createFromDate(2021, 5, 22),
CalendarService::COLOR_BLUE
],
[
Carbon::createFromDate(2021, 4, 30),
CalendarService::COLOR_RED
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Feature\Http\Controllers;

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

class Exercise05ControllerTest extends TestCase
{
protected $controller;
protected $orderService;

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

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

public function test_index_return_view_success()
{
$response = $this->controller->index();

$this->assertEquals('exercise05::index', $response->getName());
$this->assertArrayHasKey('optionCoupons', $response->getData());
$this->assertArrayHasKey('optionReceives', $response->getData());
}

public function test_store_success()
{
$inputs = [
'price' => 1,
'option_receive' => 1,
'option_coupon' => 1
];

$request = OrderRequest::create('', 'post', $inputs);
$this->orderService->shouldReceive('handleDiscount')
->with($inputs)
->andReturn(1);
$response = $this->controller->store($request);
$data = $response->getData();

$this->assertEquals('exercise05::detail', $response->getName());
$this->assertEquals(1, $data['resultOrder']);
$this->assertEquals($inputs, $data['detailOrder']);
}
}
Loading